From 32e377d905462fa2c20dde9b59532297a7f3aea6 Mon Sep 17 00:00:00 2001 From: elijah Date: Wed, 22 May 2013 17:29:55 -0700 Subject: object list -- added pick_fields() and exclude() --- lib/leap_cli/config/object_list.rb | 49 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) 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] @@ -107,6 +113,49 @@ module LeapCli result 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. # -- cgit v1.2.3