Module: JSONLogic

Defined in:
lib/json_logic.rb,
lib/json_logic/version.rb,
lib/json_logic/operation.rb

Defined Under Namespace

Classes: Operation

Constant Summary collapse

VERSION =
'0.1'

Class Method Summary collapse

Class Method Details

.apply(logic, data) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/json_logic.rb', line 6

def self.apply(logic, data)
  return logic unless logic.is_a?(Hash)                # pass-thru
  operator, values = logic.first                       # unwrap single-key hash
  values = [values] unless values.is_a?(Array)         # syntactic sugar
  new_vals = values.map { |value| apply(value, data) } # recursion step
  new_vals.flatten!(1) if new_vals.size == 1           # [['A']] => ['A']
  Operation.perform(operator, new_vals, data || {})    # perform operation
end

.filter(logic, data) ⇒ Object



15
16
17
# File 'lib/json_logic.rb', line 15

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