Module: SafeCredentials::HashQuery
- Defined in:
- lib/safe_credentials/hash_query.rb
Instance Method Summary collapse
- #all_subpaths(path = '*', subhash = self, prefix = nil) ⇒ Object
- #query(path, subhash = self, prefix = nil) ⇒ Object
Instance Method Details
#all_subpaths(path = '*', subhash = self, prefix = nil) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/safe_credentials/hash_query.rb', line 10 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
6 7 8 |
# File 'lib/safe_credentials/hash_query.rb', line 6 def query(path, subhash = self, prefix = nil) all_subpaths(path, subhash, prefix).find_all { |k| File.fnmatch?(path.gsub('.', '/'), k.gsub('.', '/')) } end |