class JSONLogic::Operation

Constants

LAMBDAS

Public Class Methods

add_operation(operator, function) click to toggle source
# File lib/json_logic/operation.rb, line 148
def self.add_operation(operator, function)
  self.class.send(:define_method, operator) do |v, d|
    function.call(v, d)
  end
end
interpolated_block(block, data) click to toggle source
# File lib/json_logic/operation.rb, line 117
def self.interpolated_block(block, data)
  # Make sure the empty var is there to be used in iterator
  JSONLogic.apply(block, data.is_a?(Hash) ? data.merge({"": data}) : { "": data })
end
is_iterable?(operator) click to toggle source

Determine if values associated with operator need to be re-interpreted for each iteration(ie some kind of iterator) or if values can just be evaluated before passing in.

# File lib/json_logic/operation.rb, line 144
def self.is_iterable?(operator)
  ['filter', 'some', 'all', 'none', 'in', 'map', 'reduce'].include?(operator.to_s)
end
is_standard?(operator) click to toggle source
# File lib/json_logic/operation.rb, line 138
def self.is_standard?(operator)
  LAMBDAS.key?(operator.to_s)
end
perform(operator, values, data) click to toggle source
# File lib/json_logic/operation.rb, line 122
def self.perform(operator, values, data)
  # If iterable, we can only pre-fill the first element, the second one must be evaluated per element.
  # If not, we can prefill all.

  if is_iterable?(operator)
    interpolated = [JSONLogic.apply(values[0], data), *values[1..-1]]
  else
    interpolated = values.map { |val| JSONLogic.apply(val, data) }
  end

  interpolated.flatten!(1) if interpolated.size == 1           # [['A']] => ['A']

  return LAMBDAS[operator.to_s].call(interpolated, data) if is_standard?(operator)
  send(operator, interpolated, data)
end