Class: Aequitas::Rule::Value

Inherits:
Aequitas::Rule show all
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

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

#equalizer

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Aequitas::Rule

#attribute_value, #execute?, #skip?, #validate, #violation_data, #violation_info, #violation_values

Methods included from ValueObject

#equalize_on

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, options)
  super

  @expected = options.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, options)
  eq  = options.values_at(:eq,  :equal, :equals, :equal_to, :exactly).compact.first
  ne  = options.values_at(:ne,  :not_equal_to).compact.first
  gt  = options.values_at(:gt,  :greater_than).compact.first
  lt  = options.values_at(:lt,  :less_than).compact.first
  gte = options.values_at(:gte, :greater_than_or_equal_to).compact.first
  lte = options.values_at(:lte, :less_than_or_equal_to).compact.first
  rng = options.values_at(:in,  :within).compact.first

  rules = []
  rules << Equal.new(attribute_name,              options.merge(:expected => eq))  if eq
  rules << NotEqual.new(attribute_name,           options.merge(:expected => ne))  if ne
  rules << GreaterThan.new(attribute_name,        options.merge(:expected => gt))  if gt
  rules << LessThan.new(attribute_name,           options.merge(:expected => lt))  if lt
  rules << GreaterThanOrEqual.new(attribute_name, options.merge(:expected => gte)) if gte
  rules << LessThanOrEqual.new(attribute_name,    options.merge(:expected => lte)) if lte
  rules << Range.new(attribute_name,              options.merge(:expected => rng)) if rng
  rules
end

Instance Method Details

#expectedObject



40
41
42
# File 'lib/aequitas/rule/value.rb', line 40

def expected
  @expected.respond_to?(:call) ? @expected.call : @expected
end

#valid?(resource) ⇒ Boolean

Returns:

  • (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