Class: Aequitas::Rule::Value
- Inherits:
-
Aequitas::Rule
- Object
- Aequitas::Rule
- Aequitas::Rule::Value
- Defined in:
- lib/aequitas/rule/value.rb,
lib/aequitas/rule/value/equal.rb,
lib/aequitas/rule/value/range.rb,
lib/aequitas/rule/value/less_than.rb,
lib/aequitas/rule/value/not_equal.rb,
lib/aequitas/rule/value/greater_than.rb,
lib/aequitas/rule/value/less_than_or_equal.rb,
lib/aequitas/rule/value/greater_than_or_equal.rb
Direct Known Subclasses
Equal, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, NotEqual, Range
Defined Under Namespace
Classes: Equal, GreaterThan, GreaterThanOrEqual, LessThan, LessThanOrEqual, NotEqual, Range
Instance Attribute Summary
Attributes inherited from Aequitas::Rule
#attribute_name, #custom_message, #guard, #skip_condition
Attributes included from ValueObject
Class Method Summary collapse
-
.rules_for(attribute_name, options) ⇒ Object
TODO: move options normalization into the validator macros?.
Instance Method Summary collapse
- #expected ⇒ Object
-
#initialize(attribute_name, options) ⇒ Value
constructor
A new instance of Value.
- #valid?(resource) ⇒ Boolean
Methods inherited from Aequitas::Rule
#attribute_value, #execute?, #skip?, #validate, #violation_data, #violation_info, #violation_values
Methods included from ValueObject
Constructor Details
#initialize(attribute_name, options) ⇒ Value
Returns a new instance of Value.
34 35 36 37 38 |
# File 'lib/aequitas/rule/value.rb', line 34 def initialize(attribute_name, ) super @expected = .fetch(:expected) end |
Class Method Details
.rules_for(attribute_name, options) ⇒ Object
TODO: move options normalization into the validator macros?
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/aequitas/rule/value.rb', line 14 def self.rules_for(attribute_name, ) eq = .values_at(:eq, :equal, :equals, :equal_to, :exactly).compact.first ne = .values_at(:ne, :not_equal_to).compact.first gt = .values_at(:gt, :greater_than).compact.first lt = .values_at(:lt, :less_than).compact.first gte = .values_at(:gte, :greater_than_or_equal_to).compact.first lte = .values_at(:lte, :less_than_or_equal_to).compact.first rng = .values_at(:in, :within).compact.first rules = [] rules << Equal.new(attribute_name, .merge(:expected => eq)) if eq rules << NotEqual.new(attribute_name, .merge(:expected => ne)) if ne rules << GreaterThan.new(attribute_name, .merge(:expected => gt)) if gt rules << LessThan.new(attribute_name, .merge(:expected => lt)) if lt rules << GreaterThanOrEqual.new(attribute_name, .merge(:expected => gte)) if gte rules << LessThanOrEqual.new(attribute_name, .merge(:expected => lte)) if lte rules << Range.new(attribute_name, .merge(:expected => rng)) if rng rules end |
Instance Method Details
#expected ⇒ Object
40 41 42 |
# File 'lib/aequitas/rule/value.rb', line 40 def expected @expected.respond_to?(:call) ? @expected.call : @expected end |
#valid?(resource) ⇒ Boolean
44 45 46 47 48 49 50 51 52 |
# File 'lib/aequitas/rule/value.rb', line 44 def valid?(resource) # TODO: is it even possible for expected to be nil? # if so, return a dummy validator when expected is nil return true if expected.nil? value = attribute_value(resource) skip?(value) || valid_value?(value) end |