lib/json_logic/operation.rb in json_logic-0.4.6 vs lib/json_logic/operation.rb in json_logic-0.4.7
- old
+ new
@@ -1,17 +1,32 @@
module JSONLogic
+ ITERABLE_KEY = "".freeze
+
class Operation
LAMBDAS = {
'var' => ->(v, d) do
- return d unless d.is_a?(Hash) or d.is_a?(Array)
- return v == [""] ? (d.is_a?(Array) ? d : d[""]) : d.deep_fetch(*v)
+ if !(d.is_a?(Hash) || d.is_a?(Array))
+ d
+ else
+ if v == [JSONLogic::ITERABLE_KEY]
+ if d.is_a?(Array)
+ d
+ else
+ d[JSONLogic::ITERABLE_KEY]
+ end
+ else
+ d.deep_fetch(*v)
+ end
+ end
end,
'missing' => ->(v, d) { v.select { |val| d.deep_fetch(val).nil? } },
'missing_some' => ->(v, d) {
present = v[1] & d.keys
present.size >= v[0] ? [] : LAMBDAS['missing'].call(v[1], d)
},
'some' => -> (v,d) do
+ return false unless v[0].is_a?(Array)
+
v[0].any? do |val|
interpolated_block(v[1], val).truthy?
end
end,
'filter' => -> (v,d) do