aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorelijah <elijah@riseup.net>2013-05-22 17:29:55 -0700
committerelijah <elijah@riseup.net>2013-05-22 17:29:55 -0700
commit32e377d905462fa2c20dde9b59532297a7f3aea6 (patch)
tree40aa631089badd8c2f35d1c2ee344d11903b940f /lib
parent109a3700ac9f7dc62482d9c3e54e4de0396b2fe5 (diff)
downloadleap_cli-32e377d905462fa2c20dde9b59532297a7f3aea6.tar.gz
leap_cli-32e377d905462fa2c20dde9b59532297a7f3aea6.tar.bz2
object list -- added pick_fields() and exclude()
Diffstat (limited to 'lib')
-rw-r--r--lib/leap_cli/config/object_list.rb49
1 files changed, 49 insertions, 0 deletions
diff --git a/lib/leap_cli/config/object_list.rb b/lib/leap_cli/config/object_list.rb
index ebb0bb0..830e96d 100644
--- a/lib/leap_cli/config/object_list.rb
+++ b/lib/leap_cli/config/object_list.rb
@@ -64,6 +64,12 @@ module LeapCli
end
end
+ def exclude(node)
+ list = self.dup
+ list.delete(node.name)
+ return list
+ end
+
def each_node(&block)
self.keys.sort.each do |node_name|
yield self[node_name]
@@ -108,6 +114,49 @@ module LeapCli
end
#
+ # pick_fields(field1, field2, ...)
+ #
+ # generates a Hash from the object list, but with only the fields that are picked.
+ #
+ # If there are more than one field, then the result is a Hash of Hashes.
+ # If there is just one field, it is a simple map to the value.
+ #
+ # For example:
+ #
+ # "neighbors" = "= nodes_like_me[:services => :couchdb].pick_fields('domain.full', 'ip_address')"
+ #
+ # generates this:
+ #
+ # neighbors:
+ # couch1:
+ # domain_full: couch1.bitmask.net
+ # ip_address: "10.5.5.44"
+ # couch2:
+ # domain_full: couch2.bitmask.net
+ # ip_address: "10.5.5.52"
+ #
+ # But this:
+ #
+ # "neighbors": "= nodes_like_me[:services => :couchdb].pick_fields('domain.full')"
+ #
+ # will generate this:
+ #
+ # neighbors:
+ # couch1: couch1.bitmask.net
+ # couch2: couch2.bitmask.net
+ #
+ def pick_fields(*fields)
+ self.values.inject({}) do |hsh, node|
+ value = self[node.name].pick(*fields)
+ if fields.size == 1
+ value = value.values.first
+ end
+ hsh[node.name] = value
+ hsh
+ end
+ end
+
+ #
# applies inherit_from! to all objects.
#
def inherit_from!(object_list)