class Locomotive::Steam::Adapters::Memory::Condition

Constants

OPERATORS

Attributes

field[R]
operator[R]
value[R]

Public Class Methods

new(operator_and_field, value, locale) click to toggle source
# 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

Public Instance Methods

inspect() click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 44
def inspect
  "#{field}#{operator != :== ? '.' : ' '}#{operator} #{value.inspect}"
end
matches?(entry) click to toggle source
# 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

Protected Instance Methods

adapt_operator!(value) click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 70
def adapt_operator!(value)
  case value
  when Array
    @operator = :in if @operator == :==
  end
end
decode_operator_and_field!() click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 60
def decode_operator_and_field!
  if match = @operator_and_field.match(/^(?<field>[a-z0-9_-]+)\.(?<operator>.*)$/)
    @field    = match[:field].to_sym
    @operator = match[:operator].to_sym
    check_operator!
  end

  @operator = :matches if @value.is_a?(Regexp)
end
entry_value(entry) click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 50
def entry_value(entry)
  value = entry.send(@field) rescue nil # TODO: not safe to rely on rescue (hidden errors)

  if value.respond_to?(:translations)
    value[@locale]
  else
    value
  end
end
value_is_in_entry_value?(value) click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 77
def value_is_in_entry_value?(value)
  _matches = if value.is_a?(Array)
    array_contains?([*value], [*@value])
  else
    [*@value].include?(value)
  end
  @operator == :in ? _matches : !_matches
end

Private Instance Methods

array_contains?(source, target) click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 92
def array_contains?(source, target)
  if target.size == 0
    source.size == 0
  else
    (source & target).size != 0
  end
end
check_operator!() click to toggle source
# File lib/locomotive/steam/adapters/memory/condition.rb, line 88
def check_operator!
  raise UnsupportedOperator.new unless OPERATORS.include?(@operator)
end