Class: NinjaModel::Predicate

Inherits:
Object
  • Object
show all
Defined in:
lib/ninja_model/predicate.rb

Constant Summary collapse

PREDICATES =
[:eq, :ne, :gt, :gte, :lt, :lte, :in]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, meth, *args) ⇒ Predicate

Returns a new instance of Predicate.



8
9
10
11
12
13
# File 'lib/ninja_model/predicate.rb', line 8

def initialize(attribute, meth, *args)
  @attribute = attribute
  @meth = meth
  @valued = !args.blank?
  @value = args.blank? ? nil : args.first
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



6
7
8
# File 'lib/ninja_model/predicate.rb', line 6

def attribute
  @attribute
end

#methObject (readonly)

Returns the value of attribute meth.



6
7
8
# File 'lib/ninja_model/predicate.rb', line 6

def meth
  @meth
end

#valueObject

Returns the value of attribute value.



6
7
8
# File 'lib/ninja_model/predicate.rb', line 6

def value
  @value
end

Instance Method Details

#has_value?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/ninja_model/predicate.rb', line 20

def has_value?
  @valued
end

#test(suspect) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/ninja_model/predicate.rb', line 24

def test(suspect)
  case meth
  when :eq
    suspect.eql?(value)
  when :ne
    !suspect.eql?(value)
  when :gt
    suspect > value
  when :gte
    suspect >= value
  when :lt
    suspect < value
  when :lte
    suspect <= value
  when :in
    value.include?(suspect)
  end
end