lib/json_logic.rb in json_logic-0.0.1 vs lib/json_logic.rb in json_logic-0.1
- old
+ new
@@ -2,15 +2,19 @@
require 'json_logic/truthy'
require 'json_logic/operation'
module JSONLogic
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
- values.map! { |value| apply(value, data) } # recursion step
- values.flatten!(1) if values.size == 1 # [['A']] => ['A']
- Operation.perform(operator, values, data || {}) # perform operation
+ 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
+
+ def self.filter(logic, data)
+ data.select { |d| apply(logic, d) }
end
end
require 'json_logic/version'