Module: HashQuery

Defined in:
lib/hash_query.rb

Instance Method Summary collapse

Instance Method Details

#all_subpaths(path = '*', subhash = self, prefix = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/hash_query.rb', line 9

def all_subpaths(path = '*', subhash = self, prefix = nil)
  parts = path.split('.')
  first_part = parts.shift

  matching_pathes = subhash.keys

  subpaths = matching_pathes.collect do |subpath|
    if Hash === subhash[subpath]
      new_prefix = [prefix, subpath].compact.join('.')
      all_subpaths(parts.join('.'), subhash[subpath], new_prefix)
    else
      [prefix, subpath].join('.')
    end
  end

  Set.new(subpaths).flatten
end

#query(path, subhash = self, prefix = nil) ⇒ Object



5
6
7
# File 'lib/hash_query.rb', line 5

def query(path, subhash = self, prefix = nil)
  all_subpaths(path, subhash, prefix).find_all { |k| File.fnmatch?(path, k) }
end