Class: JsonPath::Enumerable

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/jsonpathv2/enumerable.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, object, mode, options = nil) ⇒ Enumerable

Returns a new instance of Enumerable.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/jsonpathv2/enumerable.rb', line 7

def initialize(path, object, mode, options = nil)
  @path = path.path
  @object = object
  @mode = mode
  @options = options
  @allow_eval = if @options && @options.key?(:allow_eval)
                  @options[:allow_eval]
                else
                  true
                end
end

Instance Attribute Details

#allow_evalObject (readonly) Also known as: allow_eval?

Returns the value of attribute allow_eval.



4
5
6
# File 'lib/jsonpathv2/enumerable.rb', line 4

def allow_eval
  @allow_eval
end

Instance Method Details

#each(context = @object, key = nil, pos = 0, &blk) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jsonpathv2/enumerable.rb', line 19

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)
  else
    if pos == (@path.size - 1) && node && allow_eval?
      yield_value(blk, context, key) if instance_eval("node #{@path[pos]}")
    end
  end

  if pos > 0 && @path[pos - 1] == '..'
    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