Class: Locomotive::Steam::Adapters::Memory::Condition

Inherits:
Object
  • Object
show all
Defined in:
lib/locomotive/steam/adapters/memory/condition.rb

Defined Under Namespace

Classes: UnsupportedOperator

Constant Summary collapse

OPERATORS =
%i(== eq ne neq matches gt gte lt lte size all in nin).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(operator_and_field, value, locale) ⇒ Condition

Returns a new instance of Condition.



13
14
15
16
17
18
19
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 13

def initialize(operator_and_field, value, locale)
  @locale = locale.try(:to_sym)
  @operator_and_field, @value = operator_and_field, value
  @operator, @field = :==, operator_and_field

  decode_operator_and_field!
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



11
12
13
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 11

def field
  @field
end

#operatorObject (readonly)

Returns the value of attribute operator.



11
12
13
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 11

def operator
  @operator
end

#valueObject (readonly)

Returns the value of attribute value.



11
12
13
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 11

def value
  @value
end

Instance Method Details

#inspectObject



44
45
46
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 44

def inspect
  "#{field}#{operator != :== ? '.' : ' '}#{operator} #{value.inspect}"
end

#matches?(entry) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/locomotive/steam/adapters/memory/condition.rb', line 21

def matches?(entry)
  entry_value = entry_value(entry)

  adapt_operator!(entry_value)

  case @operator
  when :==        then entry_value == @value
  when :eq        then entry_value == @value
  when :ne        then entry_value != @value
  when :neq       then entry_value != @value
  when :matches   then @value =~ entry_value
  when :gt        then entry_value && entry_value > @value
  when :gte       then entry_value && entry_value >= @value
  when :lt        then entry_value && entry_value < @value
  when :lte       then entry_value && entry_value <= @value
  when :size      then entry_value.size == @value
  when :all       then array_contains?([*@value], entry_value)
  when :in, :nin  then value_is_in_entry_value?(entry_value)
  else
    raise UnknownConditionInScope.new("#{@operator} is unknown or not implemented.")
  end
end