Sha256: e69e674923126226c91c03a8b13240f83f3b051c0be944398d002b38243c0da1

Contents?: true

Size: 930 Bytes

Versions: 3

Compression:

Stored size: 930 Bytes

Contents

require 'core_ext/deep_fetch'
require 'core_ext/stringify_keys'
require 'json_logic/truthy'
require 'json_logic/operation'
module JSONLogic
  def self.apply(logic, data)
    if logic.is_a?(Array)
      return logic.map { |val| apply(val, data) }
    end

    return logic unless logic.is_a?(Hash)                  # pass-thru

    data = data.stringify_keys if data.is_a?(Hash)         # Stringify keys to keep out problems with symbol/string mismatch
    operator, values = logic.first                         # unwrap single-key hash
    values = [values] unless values.is_a?(Array)           # syntactic sugar
    Operation.perform(operator, values, data || {})
  end

  def self.filter(logic, data)
    data.select { |d| apply(logic, d) }
  end

  def self.add_operation(operator, function)
    Operation.class.send(:define_method, operator) do |v, d|
      function.call(v, d)
    end
  end
end

require 'json_logic/version'

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
json_logic-0.4.4 lib/json_logic.rb
json_logic-0.4.3 lib/json_logic.rb
json_logic-0.4.1 lib/json_logic.rb