lib/jsonpath/enumerable.rb in jsonpath-1.0.3 vs lib/jsonpath/enumerable.rb in jsonpath-1.0.4
- old
+ new
@@ -26,10 +26,14 @@
each(context, key, pos + 1, &blk)
when '$'
each(context, key, pos + 1, &blk) if node == @object
when /^\[(.*)\]$/
handle_wildecard(node, expr, context, key, pos, &blk)
+ when /\(.*\)/
+ keys = expr.gsub(/[()]/, '').split(',').map(&:strip)
+ new_context = filter_context(context, keys)
+ yield_value(blk, new_context, key)
end
if pos > 0 && @path[pos - 1] == '..' || (@path[pos - 1] == '*' && @path[pos] != '..')
case node
when Hash then node.each { |k, _| each(node, k, pos, &blk) }
@@ -37,9 +41,21 @@
end
end
end
private
+
+ def filter_context(context, keys)
+ case context
+ when Hash
+ # TODO: Change this to `slice(*keys)` when ruby version support is > 2.4
+ context.select { |k| keys.include?(k) }
+ when Array
+ context.each_with_object([]) do |c, memo|
+ memo << c.select { |k| keys.include?(k) }
+ end
+ end
+ end
def handle_wildecard(node, expr, _context, _key, pos, &blk)
expr[1, expr.size - 2].split(',').each do |sub_path|
case sub_path[0]
when '\'', '"'