Class: JSONPath::Nodes::KeyNode

Inherits:
PathNode
  • Object
show all
Defined in:
lib/jsonpath/nodes.rb

Instance Method Summary collapse

Methods inherited from PathNode

#recurse, #traverse, #traversing_descendants?

Instance Method Details

#descend(*objects) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jsonpath/nodes.rb', line 87

def descend(*objects)
  results = []
  keys = find_keys
  traverse(objects) do |obj|
    if obj.is_a?(Hash)
      keys.each do |key|
        if obj.key?(key)
          results << obj[key]
        end
      end
    end
  end
  results
end

#find_keys(node = self, results = [], checked = []) ⇒ Object

supports finding the key from self or child elements



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/jsonpath/nodes.rb', line 75

def find_keys(node=self, results=[], checked=[])
  if node.respond_to?(:key)
    results << node.key.text_value
  end
  if node.elements && !node.elements.empty?
    node.elements.each do |element|
      find_keys(element, results)
    end
  end
  results
end