Class: ActiveHash::Relation::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/active_hash/condition.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(constraints) ⇒ Condition

Returns a new instance of Condition.



4
5
6
7
# File 'lib/active_hash/condition.rb', line 4

def initialize(constraints)
  @constraints = constraints
  @inverted = false
end

Instance Attribute Details

#constraintsObject (readonly)

Returns the value of attribute constraints.



2
3
4
# File 'lib/active_hash/condition.rb', line 2

def constraints
  @constraints
end

#invertedObject (readonly)

Returns the value of attribute inverted.



2
3
4
# File 'lib/active_hash/condition.rb', line 2

def inverted
  @inverted
end

Instance Method Details

#invert!Object



9
10
11
12
13
# File 'lib/active_hash/condition.rb', line 9

def invert!
  @inverted = !inverted

  self
end

#matches?(record) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/active_hash/condition.rb', line 15

def matches?(record)
  match = begin
    return true unless constraints

    expectation_method = inverted ? :any? : :all?

    constraints.send(expectation_method) do |attribute, expected|
      value = record.read_attribute(attribute)

      matches_value?(value, expected)
    end
  end

  inverted ? !match : match
end