Class: Locomotive::Steam::Liquid::Scopeable::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/liquid/scopeable.rb

Constant Summary collapse

OPERATORS =
%w(all gt gte in lt lte ne nin size)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value) ⇒ Condition

Returns a new instance of Condition.



56
57
58
59
60
61
62
63
64
65
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 56

def initialize(name, value)
  self.name, self.right_operand = name, value

  self.process_right_operand

  # default value
  self.operator = :==

  self.decode_operator_based_on_name
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



54
55
56
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 54

def name
  @name
end

#operatorObject

Returns the value of attribute operator.



54
55
56
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 54

def operator
  @operator
end

#right_operandObject

Returns the value of attribute right_operand.



54
55
56
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 54

def right_operand
  @right_operand
end

Instance Method Details

#matches?(entry) ⇒ Boolean

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 67

def matches?(entry)
  value = self.get_value(entry)

  self.decode_operator_based_on_value(value)

  case self.operator
  when :==      then value == self.right_operand
  when :ne      then value != self.right_operand
  when :matches then self.right_operand =~ value
  when :gt      then value > self.right_operand
  when :gte     then value >= self.right_operand
  when :lt      then value < self.right_operand
  when :lte     then value <= self.right_operand
  when :size    then value.size == self.right_operand
  when :all     then [*self.right_operand].contains?(value)
  when :in, :nin
    _matches = if value.is_a?(Array)
      [*value].contains?([*self.right_operand])
    else
      [*self.right_operand].include?(value)
    end
    self.operator == :in ? _matches : !_matches
  else
    raise UnknownConditionInScope.new("#{self.operator} is unknown or not implemented.")
  end
end

#to_sObject



94
95
96
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 94

def to_s
  "#{name} #{operator} #{self.right_operand.to_s}"
end