Class: HashQuery::Query
- Inherits:
-
Object
- Object
- HashQuery::Query
- Defined in:
- lib/hash_query.rb
Instance Method Summary collapse
- #array_y?(node) ⇒ Boolean
- #descend(selectors, node, found) ⇒ Object
- #descend_array(selectors, node, found) ⇒ Object
- #descend_hash(selectors, node, found) ⇒ Object
- #hash_y?(node) ⇒ Boolean
-
#initialize(hash) ⇒ Query
constructor
A new instance of Query.
- #query(selectors) ⇒ Object
Constructor Details
#initialize(hash) ⇒ Query
Returns a new instance of Query.
56 57 58 |
# File 'lib/hash_query.rb', line 56 def initialize(hash) @hash = hash end |
Instance Method Details
#array_y?(node) ⇒ Boolean
75 76 77 |
# File 'lib/hash_query.rb', line 75 def array_y?(node) node.class.ancestors.include?(Array) end |
#descend(selectors, node, found) ⇒ Object
79 80 81 82 83 84 |
# File 'lib/hash_query.rb', line 79 def descend(selectors, node, found) return if node.nil? || (!hash_y?(node) && !array_y?(node)) descend_array(selectors, node, found) if array_y?(node) descend_hash(selectors, node, found) if hash_y?(node) return found end |
#descend_array(selectors, node, found) ⇒ Object
86 87 88 89 90 |
# File 'lib/hash_query.rb', line 86 def descend_array(selectors, node, found) node.each do |a| descend(selectors, a, found) end end |
#descend_hash(selectors, node, found) ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/hash_query.rb', line 92 def descend_hash(selectors, node, found) selectors = selectors.dup if selectors.first.match?(node) selector = selectors.shift if selectors.size == 0 found << selector.match!(node) else descend(selectors, selector.match!(node), found) end else node.select { |k, node| hash_y?(node) || array_y?(node) }.each do |k, node| descend(selectors, node, found) end end end |
#hash_y?(node) ⇒ Boolean
71 72 73 |
# File 'lib/hash_query.rb', line 71 def hash_y?(node) node.class.ancestors.include?(Hash) end |