veto 0.1.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (104) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +7 -94
  3. data/lib/veto/blocks/block.rb +25 -0
  4. data/lib/veto/blocks/checker.rb +28 -0
  5. data/lib/veto/blocks/conditional_block.rb +26 -0
  6. data/lib/veto/blocks/validate_block.rb +18 -0
  7. data/lib/veto/blocks/validates_block.rb +51 -0
  8. data/lib/veto/blocks/with_options_block.rb +7 -0
  9. data/lib/veto/check_context_object.rb +10 -0
  10. data/lib/veto/checks/attribute_check.rb +19 -0
  11. data/lib/veto/checks/check.rb +7 -0
  12. data/lib/veto/checks/check_factory.rb +18 -0
  13. data/lib/veto/{validators/exact_length_validator.rb → checks/exact_length_check.rb} +5 -4
  14. data/lib/veto/{validators/format_validator.rb → checks/format_check.rb} +5 -4
  15. data/lib/veto/{validators/greater_than_validator.rb → checks/greater_than_check.rb} +2 -4
  16. data/lib/veto/{validators/greater_than_or_equal_to_validator.rb → checks/greater_than_or_equal_to_check.rb} +2 -4
  17. data/lib/veto/{validators/inclusion_validator.rb → checks/inclusion_check.rb} +2 -4
  18. data/lib/veto/{validators/integer_validator.rb → checks/integer_check.rb} +2 -4
  19. data/lib/veto/{validators/length_range_validator.rb → checks/length_range_check.rb} +2 -4
  20. data/lib/veto/{validators/less_than_validator.rb → checks/less_than_check.rb} +2 -4
  21. data/lib/veto/{validators/less_than_or_equal_to_validator.rb → checks/less_than_or_equal_to_check.rb} +2 -4
  22. data/lib/veto/{validators/max_length_validator.rb → checks/max_length_check.rb} +2 -4
  23. data/lib/veto/checks/method_check.rb +17 -0
  24. data/lib/veto/{validators/min_length_validator.rb → checks/min_length_check.rb} +2 -4
  25. data/lib/veto/{validators/not_null_validator.rb → checks/not_null_check.rb} +2 -4
  26. data/lib/veto/{validators/numeric_validator.rb → checks/numeric_check.rb} +2 -4
  27. data/lib/veto/{validators/presence_validator.rb → checks/presence_check.rb} +2 -4
  28. data/lib/veto/conditions/condition.rb +7 -0
  29. data/lib/veto/conditions/condition_factory.rb +18 -0
  30. data/lib/veto/conditions/conditions.rb +21 -0
  31. data/lib/veto/conditions/context_method_condition.rb +11 -0
  32. data/lib/veto/conditions/entity_eval_condition.rb +11 -0
  33. data/lib/veto/conditions/if_conditions.rb +8 -0
  34. data/lib/veto/conditions/if_unless_conditions.rb +21 -0
  35. data/lib/veto/conditions/passing_condition.rb +7 -0
  36. data/lib/veto/conditions/primative_condition.rb +11 -0
  37. data/lib/veto/conditions/proc_condition.rb +11 -0
  38. data/lib/veto/conditions/unless_conditions.rb +8 -0
  39. data/lib/veto/configuration.rb +38 -37
  40. data/lib/veto/errors.rb +27 -27
  41. data/lib/veto/exceptions.rb +13 -3
  42. data/lib/veto/model.rb +33 -35
  43. data/lib/veto/validator.rb +68 -169
  44. data/lib/veto/version.rb +1 -1
  45. data/lib/veto.rb +90 -49
  46. data/spec/blocks/block_spec.rb +31 -0
  47. data/spec/blocks/check_block_spec.rb +46 -0
  48. data/spec/blocks/conditional_block_spec.rb +36 -0
  49. data/spec/blocks/validate_block_spec.rb +18 -0
  50. data/spec/blocks/validates_block_spec.rb +22 -0
  51. data/spec/checks/exact_length_check_spec.rb +37 -0
  52. data/spec/checks/format_validator_spec.rb +37 -0
  53. data/spec/checks/greater_than_check_spec.rb +48 -0
  54. data/spec/checks/greater_than_or_equal_to_check_spec.rb +48 -0
  55. data/spec/checks/inclusion_check_spec.rb +37 -0
  56. data/spec/checks/integer_check_spec.rb +37 -0
  57. data/spec/checks/length_range_check_spec.rb +37 -0
  58. data/spec/checks/less_than_check_spec.rb +48 -0
  59. data/spec/checks/less_than_or_equal_to_check_spec.rb +48 -0
  60. data/spec/checks/max_length_check_spec.rb +48 -0
  61. data/spec/checks/method_check_spec.rb +13 -0
  62. data/spec/checks/min_length_check_spec.rb +48 -0
  63. data/spec/checks/not_null_check_spec.rb +37 -0
  64. data/spec/checks/numeric_check_spec.rb +37 -0
  65. data/spec/checks/presence_check_spec.rb +48 -0
  66. data/spec/conditions/condition_factory_spec.rb +31 -0
  67. data/spec/conditions/conditions_spec.rb +25 -0
  68. data/spec/conditions/context_method_condition_spec.rb +25 -0
  69. data/spec/conditions/entity_eval_condition_spec.rb +21 -0
  70. data/spec/conditions/if_conditions_spec.rb +65 -0
  71. data/spec/conditions/if_unless_conditions_spec.rb +33 -0
  72. data/spec/conditions/primative_condition_spec.rb +22 -0
  73. data/spec/conditions/proc_condition_spec.rb +25 -0
  74. data/spec/conditions/unless_conditions_spec.rb +65 -0
  75. data/spec/configuration/message_spec.rb +18 -23
  76. data/spec/spec_helper.rb +2 -1
  77. data/spec/validator_spec.rb +224 -115
  78. metadata +96 -61
  79. data/lib/veto/attribute_validator_factory.rb +0 -36
  80. data/lib/veto/builder.rb +0 -65
  81. data/lib/veto/conditions.rb +0 -23
  82. data/lib/veto/conditions_evaluator.rb +0 -30
  83. data/lib/veto/validators/abstract_validator.rb +0 -15
  84. data/lib/veto/validators/attribute_validator.rb +0 -22
  85. data/lib/veto/validators/custom_method_validator.rb +0 -19
  86. data/spec/attribute_validator_factory_spec.rb +0 -72
  87. data/spec/builder_spec.rb +0 -38
  88. data/spec/conditions_evaluator_spec.rb +0 -90
  89. data/spec/conditions_spec.rb +0 -16
  90. data/spec/configuration_spec.rb +0 -6
  91. data/spec/errors_spec.rb +0 -22
  92. data/spec/model_spec.rb +0 -67
  93. data/spec/system/validator_spec.rb +0 -530
  94. data/spec/validators/exact_length_validator_spec.rb +0 -37
  95. data/spec/validators/format_validator_spec.rb +0 -32
  96. data/spec/validators/inclusion_validator_spec.rb +0 -45
  97. data/spec/validators/integer_validator_spec.rb +0 -42
  98. data/spec/validators/length_range_validator_spec.rb +0 -55
  99. data/spec/validators/max_length_validator_spec.rb +0 -32
  100. data/spec/validators/min_length_validator_spec.rb +0 -32
  101. data/spec/validators/not_null_validator_spec.rb +0 -27
  102. data/spec/validators/numeric_validator_spec.rb +0 -42
  103. data/spec/validators/presence_validator_spec.rb +0 -47
  104. data/spec/veto_spec.rb +0 -24
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 569cf65f25931b9a8ae0f2a357d7163c04195fa8
4
- data.tar.gz: c95247aed3ff88dd2e413e5d48c05fa13b6fb63b
3
+ metadata.gz: 97744d7c933224418b8f716cfb6495ce2561d765
4
+ data.tar.gz: 14621abaec3c7a5492f679b590239255f40dc614
5
5
  SHA512:
6
- metadata.gz: 2159bc303f0dd6ba0fa69b3ba497abeec0981893e6b630bd235fe7378207588a22b3d65913fc98a9efd491b67182bd5c1eccedfa436816e3ef18137efec3bec3
7
- data.tar.gz: 88f37e7ebc017a26b23dab4303ca28e8abd885286caa5fbc4c7f5f35bf30708c602ddaf39c65e4b8308d060a2e875ba5e8fbf29df93fbaa88e1de2fcedc652cb
6
+ metadata.gz: ffa5ca8c717a8f3bf1f2b97ce4b9fca1f9068357afcb4213bb0570def26236895679c6adaa836685b9c8584de3c3510e6839ccde71f7e0c052ff992ae1c0dd42
7
+ data.tar.gz: 2c06f80971bc003ac56b30312d7bfedbf00c6c2d186f734cfdf3997fdd7c93ac8eb0c7e80f806fbb49e300e81dbb4652f6bcfc918c1545b06a42c082ffc2ece5
data/README.md CHANGED
@@ -41,107 +41,20 @@ class Person
41
41
  attr_reader :name, :age, :errors
42
42
  end
43
43
 
44
- person = Person.new
45
-
46
- # Validation using class methods
44
+ person = Person.new
47
45
 
48
- PersonValidator.valid?(person) # => false
49
- PersonValidator.validate!(person) # => # => Veto::InvalidEntity, ["name is not present", "..."]
46
+ # Validate entity
50
47
 
51
- # Validation using validator instance
52
-
53
- validator = PersonValidator.new(person)
54
- validator.valid? # => false
55
- validator.validate! # => # => Veto::InvalidEntity, ["name is not present", "..."]
48
+ validator = PersonValidator.new
49
+ validator.valid?(person) # => false
50
+ validator.validate!(person) # => # => Veto::InvalidEntity, ["name is not present", "..."]
56
51
  validator.errors.full_messages # => ["first name is not present", "..."]
57
52
 
58
53
  # If entity has errors attr_accessor, errors will be passed to the entity
59
54
 
60
55
  person.errors # => nil
61
- PersonValidator.valid?(person) # => false
62
- person.errors.full_messages # => ["first name is not present", "..."]
63
- ```
64
-
65
- ### Class Method Usage
66
-
67
- The simplest way to validate an entity is to pass it directly to one of the validator's class methods.
68
-
69
- #### valid?
70
-
71
- Validate an entity by passing it to the `valid?` method.
72
-
73
- ```ruby
74
- person = Person.new
75
- PersonValidator.valid?(person) # => false
76
- ```
77
-
78
- #### validate!
79
-
80
- For strict validations, pass the entity to the `validate!` method instead. This method will raise an exception if the validation fails.
81
-
82
- ```ruby
83
- person = Person.new
84
- PersonValidator.validate!(person) # => Veto::InvalidEntity, ["first name is not present", "..."]
85
- ```
86
-
87
- ### Instance Method Usage
88
-
89
- Using a validator instance is also simple, and has some advantages in regards to validation errors (more below). The validation methods available on a validator instance are the same as the class methods.
90
-
91
- #### initialization
92
-
93
- When creating a validator instance, the entity you wish to be validated needs to be passed as an argument to the `new` method.
94
-
95
- ```ruby
96
- person = Person.new
97
- validator = PersonValidator.new(person)
98
- ```
99
- #### valid?
100
-
101
- Calling the `valid?` method on a validator instance will return a boolean response, although the method does not receive arguments.
102
-
103
- ```ruby
104
- person = Person.new
105
- validator = PersonValidator.new(person)
106
- validator.valid? # => false
107
- ```
108
-
109
- #### validate!
110
-
111
- For strict validations…
112
-
113
- ```ruby
114
- person = Person.new
115
- validator = PersonValidator.new(person)
116
- validator.validate! # => Veto::InvalidEntity, ["first name is not present", "..."]
117
- ```
118
-
119
- #### errors
120
-
121
- The `errors` method will return an errors object. The errors object will contain the error messages generated during the most recent validation.
122
-
123
- ```ruby
124
- person = Person.new
125
- validator = PersonValidator.new(person)
126
- validator.errors.full_messages # => []
127
-
128
- validator.valid? # => false
129
- validator.errors.full_messages # => ["first name is not present", "..."]
130
- ```
131
-
132
- ### Errors on Entity
133
-
134
- Whether you're validating via validator class methods or a validator instance, if the entity being validated has an `errors` attr_accessor defined, the validator will attempt to populate the entity with an errors object each time it is validated.
135
-
136
- ```ruby
137
- class Person
138
- attr_accessor :errors, :first_name, :last_name, ...
139
- end
140
-
141
- person = Person.new
142
- person.errors # => nil
143
-
144
- PersonValidator.valid?(person) # => false
56
+ validator = PersonValidator.new
57
+ validator.valid?(person) # => false
145
58
  person.errors.full_messages # => ["first name is not present", "..."]
146
59
  ```
147
60
 
@@ -0,0 +1,25 @@
1
+ module Veto
2
+ class Block
3
+ def initialize
4
+ @children = []
5
+ end
6
+
7
+ def call(*args)
8
+ call_children(*args)
9
+ end
10
+
11
+ def <<(child)
12
+ @children << child
13
+ end
14
+
15
+ def children
16
+ @children
17
+ end
18
+
19
+ private
20
+
21
+ def call_children(*args)
22
+ children.each {|child| child.call(*args)}
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,28 @@
1
+ module Veto
2
+ class Checker < Block
3
+ def self.from_children(children=[])
4
+ inst = new
5
+ children.each{|child| inst << child }
6
+ inst
7
+ end
8
+
9
+ def initialize(&block)
10
+ super
11
+ instance_eval(&block) if block_given?
12
+ end
13
+
14
+ def validates(*args)
15
+ self << ValidatesBlock.build(*args)
16
+ end
17
+
18
+ def validate(*args)
19
+ self << ValidateBlock.build(*args)
20
+ end
21
+
22
+ def with_options(*args, &block)
23
+ b = WithOptionsBlock.build(*args)
24
+ b << self.class.new(&block)
25
+ self << b
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,26 @@
1
+ module Veto
2
+ class ConditionalBlock < Block
3
+ def initialize(options={})
4
+ @options = options
5
+ super()
6
+ end
7
+
8
+ def call(*args)
9
+ call_children(*args) if conditions.pass?(*args)
10
+ end
11
+
12
+ def options(hash={})
13
+ conditions_filter(:reject)
14
+ end
15
+
16
+ private
17
+
18
+ def conditions
19
+ IfUnlessConditions.new(conditions_filter(:select))
20
+ end
21
+
22
+ def conditions_filter(type)
23
+ @options.send(type){|k,v| [:if, :unless].include?(k)}
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,18 @@
1
+ module Veto
2
+ class ValidateBlock < ConditionalBlock
3
+ def self.build(*args)
4
+ block = new(*args)
5
+ block << MethodCheck.new(block.method_name)
6
+ block
7
+ end
8
+
9
+ def initialize(method_name, options={})
10
+ @method_name = method_name
11
+ super(options)
12
+ end
13
+
14
+ def method_name
15
+ @method_name
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ module Veto
2
+ class ValidatesBlock < ConditionalBlock
3
+ class CheckBlock < ConditionalBlock
4
+ def self.build(*args)
5
+ block = new(*args)
6
+ block << ::Veto::CheckFactory.new(block.type, block.attribute_name, block.options)
7
+ block
8
+ end
9
+
10
+ def initialize(type, attribute_name, value_or_options=nil)
11
+ @type = type
12
+ @attribute_name = attribute_name
13
+ options =
14
+ case value_or_options
15
+ when TrueClass
16
+ {}
17
+ when Hash
18
+ value_or_options
19
+ when Range, Array
20
+ {:in => value_or_options}
21
+ else
22
+ {:with => value_or_options}
23
+ end
24
+ super(options)
25
+ end
26
+
27
+ def attribute_name
28
+ @attribute_name
29
+ end
30
+
31
+ def type
32
+ @type
33
+ end
34
+ end
35
+
36
+ def self.build(*args)
37
+ block = new(*args)
38
+ block.options.each {|k,v| block << CheckBlock.build(k, block.attribute_name, v)}
39
+ block
40
+ end
41
+
42
+ def initialize(attribute_name, options={})
43
+ @attribute_name = attribute_name
44
+ super(options)
45
+ end
46
+
47
+ def attribute_name
48
+ @attribute_name
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,7 @@
1
+ module Veto
2
+ class WithOptionsBlock < ConditionalBlock
3
+ def self.build(*args)
4
+ new(*args)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,10 @@
1
+ module Veto
2
+ class CheckContextObject
3
+ attr_reader :entity, :context, :errors
4
+ def initialize(entity, context, errors)
5
+ @entity = entity
6
+ @context = context
7
+ @errors = errors
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,19 @@
1
+ module Veto
2
+ class AttributeCheck < Check
3
+ def initialize(attribute_name, options={})
4
+ @attribute_name = attribute_name
5
+ @options = options
6
+ end
7
+
8
+ def call(cco)
9
+ value = cco.entity.public_send(@attribute_name)
10
+ check(@attribute_name, value, cco.errors, @options)
11
+ end
12
+
13
+ private
14
+
15
+ def check(attribute_name, value, errors, options)
16
+ raise(NotImplementedError)
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,7 @@
1
+ module Veto
2
+ class Check
3
+ def call(cco)
4
+ raise(NotImplementedError)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Veto
2
+ class CheckFactory
3
+ def self.new(type, attribute_name, options={})
4
+ class_name = "#{camel_case(type.to_s)}Check"
5
+ begin
6
+ Veto.const_get(class_name).new(attribute_name, options)
7
+ rescue NameError => e
8
+ raise(CheckNotAssigned, "Check not found: ::Veto::#{class_name}")
9
+ end
10
+ end
11
+
12
+ private
13
+
14
+ def self.camel_case(str)
15
+ str.split('_').map{|w| w.capitalize }.join('')
16
+ end
17
+ end
18
+ end
@@ -1,8 +1,9 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class ExactLengthValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class ExactLengthCheck < AttributeCheck
3
+
4
+ private
5
+
6
+ def check(attribute, value, errors, options={})
6
7
  exact = options.fetch(:with)
7
8
  message = options.fetch(:message, :exact_length)
8
9
  on = options.fetch(:on, attribute)
@@ -1,8 +1,9 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class FormatValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class FormatCheck < AttributeCheck
3
+
4
+ private
5
+
6
+ def check(attribute, value, errors, options={})
6
7
  pattern = options.fetch(:with)
7
8
  message = options.fetch(:message, :format)
8
9
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class GreaterThanValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class GreaterThanCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  boundary = options.fetch(:with)
7
5
  message = options.fetch(:message, :greater_than)
8
6
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class GreaterThanOrEqualToValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class GreaterThanOrEqualToCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  boundary = options.fetch(:with)
7
5
  message = options.fetch(:message, :greater_than_or_equal_to)
8
6
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class InclusionValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class InclusionCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  set = options.fetch(:in)
7
5
  inclusion_method = set.respond_to?(:cover?) ? :cover? : :include?
8
6
  message = options.fetch(:message, :inclusion)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class IntegerValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class IntegerCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  message = options.fetch(:message, :integer)
7
5
  on = options.fetch(:on, attribute)
8
6
 
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class LengthRangeValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class LengthRangeCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  range = options.fetch(:in)
7
5
  inclusion_method = range.respond_to?(:cover?) ? :cover? : :include?
8
6
  message = options.fetch(:message, :length_range)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class LessThanValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class LessThanCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  boundary = options.fetch(:with)
7
5
  message = options.fetch(:message, :less_than)
8
6
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class LessThanOrEqualToValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class LessThanOrEqualToCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  boundary = options.fetch(:with)
7
5
  message = options.fetch(:message, :less_than_or_equal_to)
8
6
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class MaxLengthValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class MaxLengthCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  max = options.fetch(:with)
7
5
  message = options.fetch(:message, :max_length)
8
6
  on = options.fetch(:on, attribute)
@@ -0,0 +1,17 @@
1
+ module Veto
2
+ class MethodCheck < Check
3
+ def initialize(method_name)
4
+ @method_name = method_name
5
+ end
6
+
7
+ def call(cco)
8
+ cco.context.send(@method_name, cco.entity)
9
+ end
10
+
11
+ private
12
+
13
+ def check(attribute_name, value, errors, options)
14
+ raise(NotImplementedError)
15
+ end
16
+ end
17
+ end
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class MinLengthValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class MinLengthCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  min = options.fetch(:with)
7
5
  message = options.fetch(:message, :min_length)
8
6
  on = options.fetch(:on, attribute)
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class NotNullValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class NotNullCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  message = options.fetch(:message, :not_null)
7
5
  on = options.fetch(:on, attribute)
8
6
 
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class NumericValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class NumericCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  message = options.fetch(:message, :numeric)
7
5
  on = options.fetch(:on, attribute)
8
6
 
@@ -1,8 +1,6 @@
1
- require 'veto/validators/attribute_validator'
2
-
3
1
  module Veto
4
- class PresenceValidator < AttributeValidator
5
- def validate entity, attribute, value, errors, options={}
2
+ class PresenceCheck < AttributeCheck
3
+ def check(attribute, value, errors, options={})
6
4
  msg = options.fetch(:message, :presence)
7
5
  on = options.fetch(:on, attribute)
8
6
  v = value.is_a?(String) ? value.gsub(/\s+/, '') : value
@@ -0,0 +1,7 @@
1
+ module Veto
2
+ class Condition
3
+ def pass?(*args)
4
+ raise(NotImplementedError)
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,18 @@
1
+ module Veto
2
+ class ConditionFactory
3
+ def self.new(condition_option)
4
+ condition_class =
5
+ case condition_option
6
+ when String
7
+ EntityEvalCondition
8
+ when Symbol
9
+ ContextMethodCondition
10
+ when Proc
11
+ ProcCondition
12
+ else
13
+ PrimativeCondition
14
+ end
15
+ condition_class.new(condition_option)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ module Veto
2
+ class Conditions < Condition
3
+ extend Forwardable
4
+
5
+ def_delegators :conditions, :each, :size
6
+
7
+ def initialize(options_list=[])
8
+ @options_list = options_list
9
+ end
10
+
11
+ private
12
+
13
+ def conditions
14
+ options_list.map{|options| ConditionFactory.new(options) }
15
+ end
16
+
17
+ def options_list
18
+ [*@options_list]
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,11 @@
1
+ module Veto
2
+ class ContextMethodCondition < Condition
3
+ def initialize(symbol)
4
+ @symbol = symbol
5
+ end
6
+
7
+ def pass?(cco)
8
+ !!cco.context.send(@symbol, cco.entity)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,11 @@
1
+ module Veto
2
+ class EntityEvalCondition < Condition
3
+ def initialize(eval_string)
4
+ @eval_string = eval_string
5
+ end
6
+
7
+ def pass?(cco)
8
+ !!cco.entity.instance_eval(@eval_string)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,8 @@
1
+ module Veto
2
+ class IfConditions < Conditions
3
+ def pass?(*args)
4
+ conditions.each {|c| return(false) unless c.pass?(*args) }
5
+ true
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,21 @@
1
+ module Veto
2
+ class IfUnlessConditions < Condition
3
+ def initialize(options)
4
+ @options = options
5
+ end
6
+
7
+ def pass?(*args)
8
+ if_conditions.pass?(*args) && unless_conditions.pass?(*args)
9
+ end
10
+
11
+ private
12
+
13
+ def if_conditions
14
+ IfConditions.new(@options[:if])
15
+ end
16
+
17
+ def unless_conditions
18
+ UnlessConditions.new(@options[:unless])
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ module Veto
2
+ class PassingCondition < Condition
3
+ def pass?(*args)
4
+ true
5
+ end
6
+ end
7
+ end