Class: Aequitas::Rule
- Inherits:
-
Object
- Object
- Aequitas::Rule
- Extended by:
- ValueObject
- Defined in:
- lib/aequitas/rule.rb,
lib/aequitas/rule/block.rb,
lib/aequitas/rule/guard.rb,
lib/aequitas/rule/value.rb,
lib/aequitas/rule/format.rb,
lib/aequitas/rule/length.rb,
lib/aequitas/rule/method.rb,
lib/aequitas/rule/within.rb,
lib/aequitas/rule/absence.rb,
lib/aequitas/rule/presence.rb,
lib/aequitas/rule/acceptance.rb,
lib/aequitas/rule/format/url.rb,
lib/aequitas/rule/absence/nil.rb,
lib/aequitas/rule/format/proc.rb,
lib/aequitas/rule/value/equal.rb,
lib/aequitas/rule/value/range.rb,
lib/aequitas/rule/confirmation.rb,
lib/aequitas/rule/length/equal.rb,
lib/aequitas/rule/length/range.rb,
lib/aequitas/rule/absence/blank.rb,
lib/aequitas/rule/format/regexp.rb,
lib/aequitas/rule/numericalness.rb,
lib/aequitas/rule/length/maximum.rb,
lib/aequitas/rule/length/minimum.rb,
lib/aequitas/rule/primitive_type.rb,
lib/aequitas/rule/skip_condition.rb,
lib/aequitas/rule/value/less_than.rb,
lib/aequitas/rule/value/not_equal.rb,
lib/aequitas/rule/presence/not_nil.rb,
lib/aequitas/rule/presence/not_blank.rb,
lib/aequitas/rule/value/greater_than.rb,
lib/aequitas/rule/format/email_address.rb,
lib/aequitas/rule/numericalness/integer.rb,
lib/aequitas/rule/primitive_type/virtus.rb,
lib/aequitas/rule/value/less_than_or_equal.rb,
lib/aequitas/rule/numericalness/non_integer.rb,
lib/aequitas/rule/value/greater_than_or_equal.rb
Direct Known Subclasses
Absence, Acceptance, Block, Confirmation, Format, Length, Method, Numericalness, Presence, PrimitiveType, Value, Within
Defined Under Namespace
Classes: Absence, Acceptance, Block, Confirmation, Format, Guard, Length, Method, Numericalness, Presence, PrimitiveType, SkipCondition, Value, Within
Instance Attribute Summary collapse
- #attribute_name ⇒ Object readonly private
- #custom_message ⇒ Object readonly private
- #guard ⇒ Object readonly private
- #skip_condition ⇒ Object readonly private
Attributes included from ValueObject
Class Method Summary collapse
-
.rules_for(attribute_name, options, &block) ⇒ #each(Rule)
Get the validators for the given attribute_name and options.
Instance Method Summary collapse
- #attribute_value(resource) ⇒ Object
-
#execute?(resource) ⇒ Boolean
Determines if this validator should be run against the resource by delegating to the guard configured for this rule.
-
#initialize(attribute_name, options = {}) ⇒ Rule
constructor
Construct a validator.
-
#skip?(value) ⇒ Boolean
private
Test the value to see if it is blank or nil, and if it is allowed.
-
#validate(resource) ⇒ NilClass, Violation
Validate the
resource
arg against this Rule. - #violation_data(resource) ⇒ Object private
- #violation_info(resource) ⇒ Object private
- #violation_values(resource) ⇒ Object private
Methods included from ValueObject
Constructor Details
#initialize(attribute_name, options = {}) ⇒ Rule
Construct a validator. Capture the :if and :unless clauses when present.
TODO: remove Hash as a value for :message
(see Violation#[] in backwards.rb)
59 60 61 62 63 64 |
# File 'lib/aequitas/rule.rb', line 59 def initialize(attribute_name, = {}) @attribute_name = attribute_name @custom_message = .fetch(:message, nil) @guard = .fetch(:guard) { Guard.new() } @skip_condition = .fetch(:skip_condition) { SkipCondition.new() } end |
Instance Attribute Details
#attribute_name ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
16 17 18 |
# File 'lib/aequitas/rule.rb', line 16 def attribute_name @attribute_name end |
#custom_message ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
18 19 20 |
# File 'lib/aequitas/rule.rb', line 18 def @custom_message end |
#guard ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
20 21 22 |
# File 'lib/aequitas/rule.rb', line 20 def guard @guard end |
#skip_condition ⇒ Object (readonly)
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
22 23 24 |
# File 'lib/aequitas/rule.rb', line 22 def skip_condition @skip_condition end |
Class Method Details
.rules_for(attribute_name, options, &block) ⇒ #each(Rule)
Get the validators for the given attribute_name and options
34 35 36 |
# File 'lib/aequitas/rule.rb', line 34 def self.rules_for(attribute_name, , &block) Array(new(attribute_name, , &block)) end |
Instance Method Details
#attribute_value(resource) ⇒ Object
103 104 105 |
# File 'lib/aequitas/rule.rb', line 103 def attribute_value(resource) resource.validation_attribute_value(attribute_name) end |
#execute?(resource) ⇒ Boolean
Determines if this validator should be run against the resource by delegating to the guard configured for this rule
84 85 86 |
# File 'lib/aequitas/rule.rb', line 84 def execute?(resource) guard.allow?(resource) end |
#skip?(value) ⇒ Boolean
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Test the value to see if it is blank or nil, and if it is allowed. Note that allowing blank without explicitly denying nil allows nil values, since nil.blank? is true.
99 100 101 |
# File 'lib/aequitas/rule.rb', line 99 def skip?(value) skip_condition.skip?(value) end |
#validate(resource) ⇒ NilClass, Violation
Validate the resource
arg against this Rule
74 75 76 77 78 79 80 |
# File 'lib/aequitas/rule.rb', line 74 def validate(resource) if valid?(resource) nil else Violation.new(resource, , self) end end |
#violation_data(resource) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
118 119 120 |
# File 'lib/aequitas/rule.rb', line 118 def violation_data(resource) [ ] end |
#violation_info(resource) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
108 109 110 |
# File 'lib/aequitas/rule.rb', line 108 def violation_info(resource) Hash[ violation_data(resource) ] end |
#violation_values(resource) ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
113 114 115 |
# File 'lib/aequitas/rule.rb', line 113 def violation_values(resource) violation_data(resource).map { |(_, value)| value } end |