diff options
author | elijah <elijah@riseup.net> | 2012-10-09 00:05:44 -0700 |
---|---|---|
committer | elijah <elijah@riseup.net> | 2012-10-09 00:05:44 -0700 |
commit | 73b126976ad7843eb47a84944cf191bf05b14216 (patch) | |
tree | 918656f8d7c637e8c7a8f0c010eff55bfd98ae1b /lib/leap_cli/config_list.rb | |
parent | 578ac2f5dc7432317d7a022bed9d869ab89ee45c (diff) |
fixed paths
Diffstat (limited to 'lib/leap_cli/config_list.rb')
-rw-r--r-- | lib/leap_cli/config_list.rb | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/lib/leap_cli/config_list.rb b/lib/leap_cli/config_list.rb new file mode 100644 index 0000000..c8ff23b --- /dev/null +++ b/lib/leap_cli/config_list.rb @@ -0,0 +1,77 @@ +module LeapCli + class ConfigList < Hash + + def initialize(config=nil) + if config + self << config + end + end + + # + # if the key is a hash, we treat it as a condition and filter all the configs using the condition + # + # for example: + # + # nodes[:public_dns => true] + # + # will return a ConfigList with node configs that have public_dns set to true + # + def [](key) + if key.is_a? Hash + results = ConfigList.new + field, match_value = key.to_a.first + field = field.is_a?(Symbol) ? field.to_s : field + match_value = match_value.is_a?(Symbol) ? match_value.to_s : match_value + each do |name, config| + value = config[field] + if !value.nil? + if value.is_a? Array + if value.includes?(match_value) + results[name] = config + end + else + if value == match_value + results[name] = config + end + end + end + end + results + else + super + end + end + + def <<(config) + if config.is_a? ConfigList + self.merge!(config) + else + self[config['name']] = config + end + end + + # + # converts the hash of configs into an array of hashes, with ONLY the specified fields + # + def fields(*fields) + result = [] + keys.sort.each do |name| + result << self[name].to_h(*fields) + end + result + end + + # + # like fields(), but returns an array of values instead of an array of hashes. + # + def field(field) + field = field.to_s + result = [] + keys.sort.each do |name| + result << self[name][field] + end + result + end + + end +end |