Class: JsonPath::Enumerable
- Inherits:
-
Object
- Object
- JsonPath::Enumerable
- Includes:
- Enumerable
- Defined in:
- lib/jsonpath/enumerable.rb
Instance Method Summary collapse
- #each(context = @object, key = nil, pos = 0, &blk) ⇒ Object
-
#initialize(path, object, mode, options = nil) ⇒ Enumerable
constructor
A new instance of Enumerable.
Constructor Details
#initialize(path, object, mode, options = nil) ⇒ Enumerable
Returns a new instance of Enumerable.
5 6 7 8 9 10 |
# File 'lib/jsonpath/enumerable.rb', line 5 def initialize(path, object, mode, = nil) @path = path.path @object = object @mode = mode @options = end |
Instance Method Details
#each(context = @object, key = nil, pos = 0, &blk) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jsonpath/enumerable.rb', line 12 def each(context = @object, key = nil, pos = 0, &blk) node = key ? context[key] : context @_current_node = node return yield_value(blk, context, key) if pos == @path.size case expr = @path[pos] when '*', '..', '@' 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) 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) } when Array then node.each_with_index { |_, i| each(node, i, pos, &blk) } end end end |