Module: Aequitas::Rule::Within::Range

Includes:
Aequitas::Rule::Within
Included in:
Bounded, UnboundedBegin, UnboundedEnd
Defined in:
lib/aequitas/rule/within/range.rb,
lib/aequitas/rule/within/range/bounded.rb,
lib/aequitas/rule/within/range/unbounded_end.rb,
lib/aequitas/rule/within/range/unbounded_begin.rb

Defined Under Namespace

Classes: Bounded, UnboundedBegin, UnboundedEnd

Constant Summary collapse

Infinity =
1.0 / 0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#rangeObject (readonly)

Returns the value of attribute range.



14
15
16
# File 'lib/aequitas/rule/within/range.rb', line 14

def range
  @range
end

Class Method Details

.new(attribute_name, options) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/aequitas/rule/within/range.rb', line 20

def self.new(attribute_name, options)
  super if Within::Range != self

  range = options.fetch(:range) { options.fetch(:set) }

  if range.first != -Infinity && range.last != Infinity
    Bounded.new(attribute_name, options)
  elsif range.first == -Infinity
    UnboundedBegin.new(attribute_name, options)
  elsif range.last == Infinity
    UnboundedEnd.new(attribute_name, options)
  end
end

.rules_for(attribute_name, options) ⇒ Object



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

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

Instance Method Details

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



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

def initialize(attribute_name, options={})
  super

  @range = options.fetch(:range) { options.fetch(:set) }
end

#valid?(resource) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/aequitas/rule/within/range.rb', line 40

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

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