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.4.4'
Class Method Summary collapse
- .add_operation(operator, function) ⇒ Object
- .apply(logic, data) ⇒ Object
- .filter(logic, data) ⇒ Object
Class Method Details
.add_operation(operator, function) ⇒ Object
23 24 25 26 27 |
# File 'lib/json_logic.rb', line 23 def self.add_operation(operator, function) Operation.class.send(:define_method, operator) do |v, d| function.call(v, d) end end |
.apply(logic, data) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/json_logic.rb', line 6 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 |
.filter(logic, data) ⇒ Object
19 20 21 |
# File 'lib/json_logic.rb', line 19 def self.filter(logic, data) data.select { |d| apply(logic, d) } end |