|
| 1 | +# frozen_string_literal: true |
| 2 | + |
1 | 3 | class JsonPath
|
2 | 4 | class Enumerable
|
3 | 5 | include ::Enumerable
|
@@ -32,7 +34,7 @@ def each(context = @object, key = nil, pos = 0, &blk)
|
32 | 34 |
|
33 | 35 | private
|
34 | 36 |
|
35 |
| - def handle_wildecard(node, expr, context, key, pos, &blk) |
| 37 | + def handle_wildecard(node, expr, _context, _key, pos, &blk) |
36 | 38 | expr[1, expr.size - 2].split(',').each do |sub_path|
|
37 | 39 | case sub_path[0]
|
38 | 40 | when '\'', '"'
|
@@ -60,27 +62,32 @@ def handle_wildecard(node, expr, context, key, pos, &blk)
|
60 | 62 | end_idx %= node.size
|
61 | 63 | step = process_function_or_literal(array_args[2], 1)
|
62 | 64 | next unless step
|
63 |
| - (start_idx..end_idx).step(step) { |i| each(node, i, pos + 1, &blk) } |
| 65 | + if @mode == :delete |
| 66 | + (start_idx..end_idx).step(step) { |i| node[i] = nil } |
| 67 | + node.compact! |
| 68 | + else |
| 69 | + (start_idx..end_idx).step(step) { |i| each(node, i, pos + 1, &blk) } |
| 70 | + end |
64 | 71 | end
|
65 | 72 | end
|
66 | 73 | end
|
67 | 74 |
|
68 | 75 | def handle_question_mark(sub_path, node, pos, &blk)
|
69 | 76 | case node
|
70 |
| - when Array |
71 |
| - node.size.times do |index| |
72 |
| - @_current_node = node[index] |
73 |
| - # exps = sub_path[1, sub_path.size - 1] |
74 |
| - # if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]") |
75 |
| - if process_function_or_literal(sub_path[1, sub_path.size - 1]) |
76 |
| - each(@_current_node, nil, pos + 1, &blk) |
77 |
| - end |
78 |
| - end |
79 |
| - when Hash |
| 77 | + when Array |
| 78 | + node.size.times do |index| |
| 79 | + @_current_node = node[index] |
| 80 | + # exps = sub_path[1, sub_path.size - 1] |
| 81 | + # if @_current_node.send("[#{exps.gsub(/@/, '@_current_node')}]") |
80 | 82 | if process_function_or_literal(sub_path[1, sub_path.size - 1])
|
81 | 83 | each(@_current_node, nil, pos + 1, &blk)
|
82 | 84 | end
|
83 |
| - else |
| 85 | + end |
| 86 | + when Hash |
| 87 | + if process_function_or_literal(sub_path[1, sub_path.size - 1]) |
| 88 | + each(@_current_node, nil, pos + 1, &blk) |
| 89 | + end |
| 90 | + else |
84 | 91 | yield node if process_function_or_literal(sub_path[1, sub_path.size - 1])
|
85 | 92 | end
|
86 | 93 | end
|
|
0 commit comments