game_validator 0.3.0 → 0.6.0

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: 919eea6127e5739639bfd7034cf90ac44f4686dcc98fe8bd05d5cec91214458e
4
- data.tar.gz: 2169e75bd4797488853984472902d076da7c032eee445ba89ad02a5db98f7fac
3
+ metadata.gz: bd90aaf91b77cf3e07547bab0e7b2ab215d91247a61dd1b2db3cc1970a6ddc51
4
+ data.tar.gz: d4f13f99c8cc2fd30b1fb6cbc12d2a7dec41d69030cb73b7aaf6f0367da74248
5
5
  SHA512:
6
- metadata.gz: c9002a19d864067465f51a27cf344087d75f94b22704e2b6bccb07d38d3b9c2d56470bddac6a13a90874b7f8c34feb8e88a2afdf5b318294e0b6a7e6c1ff7438
7
- data.tar.gz: 41e821d0ab7d559142a95dce2afd1032a4926a846e0be6d338f65abbc74804310668e922176940244eebbef5d096a994f5ea80e1832a7aa825fbf16b16307ab7
6
+ metadata.gz: 5e1f12c87026ecbe1386fde7ff5a1c8afb8fa6340b952a08707a2fab65124a01d984fe50a61f3d2259cf21b8d12ae652fd6eba4121b5bb4a062eb8efda424b55
7
+ data.tar.gz: 7d06c2c9f5939529f97b0d084da7021233c0598e0b4218aeb8570849edf20b118f00dbed51f3e18faed079e8199fe04c6e11b05d6dc25fa81429c3cba89a5715
data/Gemfile.lock CHANGED
@@ -1,14 +1,14 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- game_validator (0.2.0)
4
+ game_validator (0.6.0)
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
data/README.md CHANGED
@@ -79,9 +79,26 @@ 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.
86
+
87
+ You can also wrap successful results from the validator, using GameValidator::Validator::ValidateToAction and GameValidator::Validator::Result
88
+ ```
89
+ class DoStuff
90
+ def call(change_orders:, **args)
91
+ # do stuff
92
+ change_orders
93
+ end
94
+ end
95
+
96
+ GameValidator::Validator::ValidateToAction::new(
97
+ validator: some_regular validator,
98
+ wrap: ->(successful_result){GameValidator::Validator::Result::new(
99
+ result: successful_result,
100
+ execute: DoStuff::new)})
101
+ ```
85
102
 
86
103
  ## Development
87
104
 
@@ -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.3.0' do |spec|
5
+ Gem::Specification.new 'game_validator', '0.6.0' do |spec|
6
6
  spec.name = 'game_validator'
7
7
  spec.version = GameValidator::VERSION
8
8
  spec.authors = ['Colin Horner']
@@ -3,6 +3,14 @@ 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)
6
14
 
7
15
  require 'game_validator/version'
8
16
  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:)
@@ -17,7 +17,7 @@ module GameValidator
17
17
  result = validate_player_action_and_user.(a)
18
18
  return build_failure.(errors: result.errors) if result.failure?
19
19
  validate = full_validator_for[[a[:player_action], a[:user].admin?]]
20
- result = validate.(a)
20
+ result = validate.(action_hash)
21
21
  return build_failure.(errors: result.errors) if result.failure?
22
22
  result
23
23
  end
@@ -1,20 +1,20 @@
1
1
  # frozen string_literal: true
2
2
 
3
3
  require 'dry-validation'
4
- require 'dry-initializer'
5
4
 
6
5
  Dry::Validation.load_extensions(:monads)
7
6
 
8
7
  module GameValidator
9
8
  class Validator
10
9
  class Base < Dry::Validation::Contract
11
- extend Dry::Initializer
12
- option :legal_options, type: Types::Array.of(Types::String)
13
- 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
14
13
 
15
14
  params do
16
15
  required(:player_action).filled(:string)
17
16
  required(:user).filled(Types.Interface(:id, :admin?))
17
+ required(:last_action_id).filled(:integer)
18
18
  end
19
19
 
20
20
  rule(:player_action) do
@@ -24,10 +24,20 @@ module GameValidator
24
24
  end
25
25
 
26
26
  rule(:user) do
27
- if !values[:user].id.eql?(next_player_id) && !values[:user].admin?
27
+ if !values[:user].id.eql?(current_player_id) && !values[:user].admin?
28
28
  key.failure({text: 'Not logged in', status: 401})
29
29
  end
30
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
31
41
  end
32
42
  end
33
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
@@ -0,0 +1,20 @@
1
+ # frozen string_literal: true
2
+
3
+ require 'dry-initializer'
4
+
5
+ module GameValidator
6
+ class Validator
7
+ class ValidateToAction
8
+ extend Dry::Initializer
9
+ option :validate, type: Types.Interface(:call)
10
+ option :wrap, type: Types.Interface(:call)
11
+
12
+ def call(action_hash)
13
+ result = validate.(action_hash)
14
+ result = wrap.(result) if result.success?
15
+ result
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -1,3 +1,3 @@
1
1
  module GameValidator
2
- VERSION = '0.3.0'
2
+ VERSION = '0.6.0'
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.3.0
4
+ version: 0.6.0
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-01-22 00:00:00.000000000 Z
11
+ date: 2021-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -148,6 +148,7 @@ files:
148
148
  - lib/game_validator/validator/result/failure.rb
149
149
  - lib/game_validator/validator/result/failure/builder.rb
150
150
  - lib/game_validator/validator/result/failure/node.rb
151
+ - lib/game_validator/validator/validate_to_action.rb
151
152
  - lib/game_validator/version.rb
152
153
  homepage:
153
154
  licenses: