Class: Aequitas::Rule::Within

Inherits:
Aequitas::Rule show all
Defined in:
lib/aequitas/rule/within.rb

Instance Attribute Summary collapse

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_info, #violation_values

Methods included from ValueObject

#equalize_on

Constructor Details

#initialize(attribute_name, options = {}) ⇒ Within

Returns a new instance of Within.



18
19
20
21
22
# File 'lib/aequitas/rule/within.rb', line 18

def initialize(attribute_name, options={})
  super

  @set = options.fetch(:set)
end

Instance Attribute Details

#setObject (readonly)

Returns the value of attribute set.



16
17
18
# File 'lib/aequitas/rule/within.rb', line 16

def set
  @set
end

Class Method Details

.rules_for(attribute_name, options) ⇒ Object

TODO: move options normalization into the validator macros



10
11
12
# File 'lib/aequitas/rule/within.rb', line 10

def self.rules_for(attribute_name, options)
  Array(new(attribute_name, options))
end

Instance Method Details

#valid?(resource) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
27
28
# File 'lib/aequitas/rule/within.rb', line 24

def valid?(resource)
  value = attribute_value(resource)

  skip?(value) || set.include?(value)
end

#violation_data(resource) ⇒ Object



34
35
36
# File 'lib/aequitas/rule/within.rb', line 34

def violation_data(resource)
  [ [ :set, set.to_a.join(', ') ] ]
end

#violation_type(resource) ⇒ Object



30
31
32
# File 'lib/aequitas/rule/within.rb', line 30

def violation_type(resource)
  :inclusion
end