Class: JsonPath::Enumerable
- Inherits:
-
Object
- Object
- JsonPath::Enumerable
- Includes:
- Enumerable
- Defined in:
- lib/jsonpath/enumerable.rb
Instance Attribute Summary collapse
-
#allow_eval ⇒ Object
(also: #allow_eval?)
readonly
Returns the value of attribute allow_eval.
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.
7 8 9 10 |
# File 'lib/jsonpath/enumerable.rb', line 7 def initialize(path, object, mode, = nil) @path, @object, @mode, @options = path.path, object, mode, @allow_eval = @options && @options.key?(:allow_eval) ? @options[:allow_eval] : true end |
Instance Attribute Details
#allow_eval ⇒ Object (readonly) Also known as: allow_eval?
Returns the value of attribute allow_eval.
4 5 6 |
# File 'lib/jsonpath/enumerable.rb', line 4 def allow_eval @allow_eval 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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# 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 '@' each(context, key, pos + 1, &blk) when /^\[(.*)\]$/ expr[1,expr.size - 2].split(',').each do |sub_path| case sub_path[0] when ?', ?" if node.is_a?(Hash) k = sub_path[1,sub_path.size - 2] each(node, k, pos + 1, &blk) if node.key?(k) end when ?? raise "Cannot use ?(...) unless eval is enabled" unless allow_eval? case node when Hash, Array (node.is_a?(Hash) ? node.keys : (0..node.size)).each do |e| each(node, e, pos + 1) { |n| @_current_node = n yield n if process_function_or_literal(sub_path[1, sub_path.size - 1]) } end else yield node if process_function_or_literal(sub_path[1, sub_path.size - 1]) end else if node.is_a?(Array) array_args = sub_path.split(':') if array_args[0] == ?* start_idx = 0 end_idx = node.size - 1 else start_idx = process_function_or_literal(array_args[0], 0) next unless start_idx end_idx = (array_args[1] && process_function_or_literal(array_args[1], -1) || (sub_path.count(':') == 0 ? start_idx : -1)) next unless end_idx end start_idx %= node.size end_idx %= node.size step = process_function_or_literal(array_args[2], 1) next unless step (start_idx..end_idx).step(step) {|i| each(node, i, pos + 1, &blk)} end end end else if pos == (@path.size - 1) && node && allow_eval? if eval("node #{@path[pos]}") yield_value(blk, context, key) end end end if pos > 0 && @path[pos-1] == '..' case node when Hash then node.each {|k, v| each(node, k, pos, &blk) } when Array then node.each_with_index {|n, i| each(node, i, pos, &blk) } end end end |