game_validator 0.4.0 → 0.6.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49e3f324917b46d542868516b19cae218850e8599a4c0ae32aabc2e8534bdf76
4
- data.tar.gz: e7b4b68f0bf8b4fe37421ed6ee3b8749bcb8f9a07f5344bf020b5632ce1b76c9
3
+ metadata.gz: f73953ca1bcfcc58ac40040a66c320acc8800e7b17dd6dcc32c8158ece1de8f0
4
+ data.tar.gz: 7c2a2ffd96f83f279ed0ccb439ebfde8c6ae61b5cd0f9725835e609f41019e51
5
5
  SHA512:
6
- metadata.gz: b01fb1013aae4f2cdbded8ced72af6a5f02f06026acaba11efd7d4bb1a1e85eb9609d03dd1bf37a71e2b6fa354a00876e246fd320cc079b0f25f7e886bff9632
7
- data.tar.gz: 1fd0f020305734494a12a61739f2acdf295755d93d65439895a94f0cf2143e148d589ca0751c2bb9253e433a3e73dfc7edb2ddff9c348eade0929c3a071ed2d2
6
+ metadata.gz: a816556fee3fbba5550cfa893727991a5898956902c73aabf099d1e88fbc3fa3e2a7fff78a58de503bd22a0111f4258f4b57bcbcc858fdc4cea61fa3e8ca0623
7
+ data.tar.gz: f1d5bd1b69fda219e77424d322c1c5f769fe53590df8cbb9fb0eebba4667bb81b5039ade87c4278bad35d50448360a20cca6c7c96b7460116ab1e2f8bd30a15b
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- game_validator (0.3.0)
4
+ game_validator (0.6.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- concurrent-ruby (1.1.7)
9
+ concurrent-ruby (1.1.8)
10
10
  diff-lcs (1.4.4)
11
- dry-configurable (0.12.0)
11
+ dry-configurable (0.12.1)
12
12
  concurrent-ruby (~> 1.0)
13
13
  dry-core (~> 0.5, >= 0.5.0)
14
14
  dry-container (0.7.2)
@@ -26,19 +26,17 @@ GEM
26
26
  concurrent-ruby (~> 1.0)
27
27
  dry-core (~> 0.4, >= 0.4.4)
28
28
  dry-equalizer
29
- dry-schema (1.5.6)
29
+ dry-schema (1.6.1)
30
30
  concurrent-ruby (~> 1.0)
31
31
  dry-configurable (~> 0.8, >= 0.8.3)
32
- dry-core (~> 0.4)
33
- dry-equalizer (~> 0.2)
32
+ dry-core (~> 0.5, >= 0.5)
34
33
  dry-initializer (~> 3.0)
35
34
  dry-logic (~> 1.0)
36
- dry-types (~> 1.4)
37
- dry-types (1.4.0)
35
+ dry-types (~> 1.5)
36
+ dry-types (1.5.1)
38
37
  concurrent-ruby (~> 1.0)
39
38
  dry-container (~> 0.3)
40
- dry-core (~> 0.4, >= 0.4.4)
41
- dry-equalizer (~> 0.3)
39
+ dry-core (~> 0.5, >= 0.5)
42
40
  dry-inflector (~> 0.1, >= 0.1.2)
43
41
  dry-logic (~> 1.0, >= 1.0.2)
44
42
  dry-validation (1.6.0)
@@ -59,10 +57,10 @@ GEM
59
57
  rspec-expectations (3.10.1)
60
58
  diff-lcs (>= 1.2.0, < 2.0)
61
59
  rspec-support (~> 3.10.0)
62
- rspec-mocks (3.10.1)
60
+ rspec-mocks (3.10.2)
63
61
  diff-lcs (>= 1.2.0, < 2.0)
64
62
  rspec-support (~> 3.10.0)
65
- rspec-support (3.10.1)
63
+ rspec-support (3.10.2)
66
64
 
67
65
  PLATFORMS
68
66
  ruby
@@ -79,4 +77,4 @@ DEPENDENCIES
79
77
  rspec (~> 3.0)
80
78
 
81
79
  BUNDLED WITH
82
- 2.0.2
80
+ 2.2.15
data/README.md CHANGED
@@ -79,9 +79,10 @@ full_validator = {
79
79
  In this fashion, you create the full validator as follows
80
80
  ```
81
81
  GameValidator::Validator::new(
82
- validate_player_action_and_user: GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1),
82
+ validate_player_action_and_user: GameValidator::Validator::Base::new(legal_options: ['run', 'hide'], next_player_id: 1, last_action_id: 1),
83
83
  full_validator_for: full_validator)
84
84
  ```
85
+ Note the last_action_id option, to make sure that you are validating against the most recent action in the game.
85
86
 
86
87
  You can also wrap successful results from the validator, using GameValidator::Validator::ValidateToAction and GameValidator::Validator::Result
87
88
  ```
@@ -99,7 +100,6 @@ GameValidator::Validator::ValidateToAction::new(
99
100
  execute: DoStuff::new)})
100
101
  ```
101
102
 
102
-
103
103
  ## Development
104
104
 
105
105
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -2,7 +2,7 @@ lib = File.expand_path("lib", __dir__)
2
2
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
3
  require "game_validator/version"
4
4
 
5
- Gem::Specification.new 'game_validator', '0.4.0' do |spec|
5
+ Gem::Specification.new 'game_validator', '0.6.1' do |spec|
6
6
  spec.name = 'game_validator'
7
7
  spec.version = GameValidator::VERSION
8
8
  spec.authors = ['Colin Horner']
@@ -3,6 +3,15 @@ require 'dry-types'
3
3
  module Types
4
4
  include Dry.Types()
5
5
  end
6
+ callable = Types.Interface(:call)
7
+ Types::Callable = Types.Constructor(Method) do |value|
8
+ callable.call(value) # raises Dry::Types::ConstraintError if object doesn't respond to #call
9
+ value.is_a?(Method) || value.is_a?(Proc) ? value : value.method(:call)
10
+ end
11
+ Types::ArrayOfCallable = Types::Array::of(Types::Callable)
12
+ Types::ArrayOfStrictInteger = Types::Array::of(Types::Strict::Integer)
13
+ Types::ArrayOfString = Types::Array::of(Types::String)
14
+ Types::CoercibleNonnegativeInteger = Types::Coercible::Integer.constrained(gteq: 0)
6
15
 
7
16
  require 'game_validator/version'
8
17
  require 'game_validator/validator'
@@ -6,9 +6,9 @@ require 'game_validator/validator/full_validator_coercer'
6
6
  module GameValidator
7
7
  class Validator
8
8
  extend Dry::Initializer
9
- option :validate_player_action_and_user, type: Types.Interface(:call)
9
+ option :validate_player_action_and_user, type: Types::Callable
10
10
  option :full_validator_for, type: Types::Hash
11
- option :build_failure, type: Types.Interface(:call),
11
+ option :build_failure, type: Types::Callable,
12
12
  default: ->{GameValidator::Validator::Result::Failure::Builder::new}
13
13
 
14
14
  def call(action_hash:, user:)
@@ -7,12 +7,14 @@ Dry::Validation.load_extensions(:monads)
7
7
  module GameValidator
8
8
  class Validator
9
9
  class Base < Dry::Validation::Contract
10
- option :legal_options, type: Types::Array.of(Types::String)
11
- option :next_player_id, type: Types::Coercible::Integer
10
+ option :legal_options, type: Types::ArrayOfString
11
+ option :current_player_id, type: Types::Coercible::Integer
12
+ option :last_action_id, type: Types::Coercible::Integer
12
13
 
13
14
  params do
14
15
  required(:player_action).filled(:string)
15
16
  required(:user).filled(Types.Interface(:id, :admin?))
17
+ required(:last_action_id).filled(:integer)
16
18
  end
17
19
 
18
20
  rule(:player_action) do
@@ -22,10 +24,20 @@ module GameValidator
22
24
  end
23
25
 
24
26
  rule(:user) do
25
- if !values[:user].id.eql?(next_player_id) && !values[:user].admin?
27
+ if !values[:user].id.eql?(current_player_id) && !values[:user].admin?
26
28
  key.failure({text: 'Not logged in', status: 401})
27
29
  end
28
30
  end
31
+
32
+ rule(:last_action_id) do
33
+ if values[:last_action_id] < last_action_id
34
+ key.failure({text: "last_action_id '#{values[:last_action_id]}' is obsolete",status: 409})
35
+ end
36
+
37
+ if values[:last_action_id] > last_action_id
38
+ key.failure({text: "last_action_id '#{values[:last_action_id]}' not a match",status: 400})
39
+ end
40
+ end
29
41
  end
30
42
  end
31
43
  end
@@ -13,7 +13,7 @@ module GameValidator
13
13
  end
14
14
 
15
15
  def call(**args)
16
- @execute.(to_h, args)
16
+ @execute.(to_h.merge(args))
17
17
  end
18
18
  end
19
19
  end
@@ -1,3 +1,3 @@
1
1
  module GameValidator
2
- VERSION = '0.4.0'
2
+ VERSION = '0.6.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: game_validator
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Colin Horner
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-03-12 00:00:00.000000000 Z
11
+ date: 2021-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler