diff options
-rw-r--r-- | lib/chef/node/attribute.rb | 374 |
1 files changed, 189 insertions, 185 deletions
diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index 05b625dd02..69cb327d70 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -78,113 +78,117 @@ class Chef :@force_override, ].freeze - [:all?, - :any?, - :assoc, - :chunk, - :collect, - :collect_concat, - :compare_by_identity, - :compare_by_identity?, - :count, - :cycle, - :detect, - :drop, - :drop_while, - :each, - :each_cons, - :each_entry, - :each_key, - :each_pair, - :each_slice, - :each_value, - :each_with_index, - :each_with_object, - :empty?, - :entries, - :except, - :fetch, - :find, - :find_all, - :find_index, - :first, - :flat_map, - :flatten, - :grep, - :group_by, - :has_value?, - :include?, - :index, - :inject, - :invert, - :key, - :keys, - :length, - :map, - :max, - :max_by, - :merge, - :min, - :min_by, - :minmax, - :minmax_by, - :none?, - :one?, - :partition, - :rassoc, - :reduce, - :reject, - :reverse_each, - :select, - :size, - :slice_before, - :sort, - :sort_by, - :store, - :symbolize_keys, - :take, - :take_while, - :to_a, - :to_h, - :to_hash, - :to_set, - :value?, - :values, - :values_at, - :zip].each do |delegated_method| - define_method(delegated_method) do |*args, &block| - merged_attributes.send(delegated_method, *args, &block) - end - end - - # return the cookbook level default attribute component + ENUM_METHODS = [ + :all?, + :any?, + :assoc, + :chunk, + :collect, + :collect_concat, + :compare_by_identity, + :compare_by_identity?, + :count, + :cycle, + :detect, + :drop, + :drop_while, + :each, + :each_cons, + :each_entry, + :each_key, + :each_pair, + :each_slice, + :each_value, + :each_with_index, + :each_with_object, + :empty?, + :entries, + :except, + :fetch, + :find, + :find_all, + :find_index, + :first, + :flat_map, + :flatten, + :grep, + :group_by, + :has_value?, + :include?, + :index, + :inject, + :invert, + :key, + :keys, + :length, + :map, + :max, + :max_by, + :merge, + :min, + :min_by, + :minmax, + :minmax_by, + :none?, + :one?, + :partition, + :rassoc, + :reduce, + :reject, + :reverse_each, + :select, + :size, + :slice_before, + :sort, + :sort_by, + :store, + :symbolize_keys, + :take, + :take_while, + :to_a, + :to_h, + :to_hash, + :to_set, + :value?, + :values, + :values_at, + :zip, + ].freeze + + ENUM_METHODS.each do |delegated_method| + define_method(delegated_method) do |*args, &block| + merged_attributes.send(delegated_method, *args, &block) + end + end + + # return the cookbook level default attribute component attr_reader :default - # return the role level default attribute component + # return the role level default attribute component attr_reader :role_default - # return the environment level default attribute component + # return the environment level default attribute component attr_reader :env_default - # return the force_default level attribute component + # return the force_default level attribute component attr_reader :force_default - # return the "normal" level attribute component + # return the "normal" level attribute component attr_reader :normal - # return the cookbook level override attribute component + # return the cookbook level override attribute component attr_reader :override - # return the role level override attribute component + # return the role level override attribute component attr_reader :role_override - # return the enviroment level override attribute component + # return the enviroment level override attribute component attr_reader :env_override - # return the force override level attribute component + # return the force override level attribute component attr_reader :force_override - # return the automatic level attribute component + # return the automatic level attribute component attr_reader :automatic def initialize(normal, default, override, automatic, node = nil) @@ -205,15 +209,15 @@ class Chef super(nil, self, node, :merged) end - # Debug what's going on with an attribute. +args+ is a path spec to the - # attribute you're interested in. For example, to debug where the value - # of `node[:network][:default_interface]` is coming from, use: - # debug_value(:network, :default_interface). - # The return value is an Array of Arrays. The Arrays - # are pairs of `["precedence_level", value]`, where precedence level is - # the component, such as role default, normal, etc. and value is the - # attribute value set at that precedence level. If there is no value at - # that precedence level, +value+ will be the symbol +:not_present+. + # Debug what's going on with an attribute. +args+ is a path spec to the + # attribute you're interested in. For example, to debug where the value + # of `node[:network][:default_interface]` is coming from, use: + # debug_value(:network, :default_interface). + # The return value is an Array of Arrays. The Arrays + # are pairs of `["precedence_level", value]`, where precedence level is + # the component, such as role default, normal, etc. and value is the + # attribute value set at that precedence level. If there is no value at + # that precedence level, +value+ will be the symbol +:not_present+. def debug_value(*args) COMPONENTS.map do |component| value = @@ -226,49 +230,49 @@ class Chef end end - # Set the cookbook level default attribute component to +new_data+. + # Set the cookbook level default attribute component to +new_data+. def default=(new_data) reset @default = VividMash.new(new_data, self, __node__, :default) end - # Set the role level default attribute component to +new_data+ + # Set the role level default attribute component to +new_data+ def role_default=(new_data) reset @role_default = VividMash.new(new_data, self, __node__, :role_default) end - # Set the environment level default attribute component to +new_data+ + # Set the environment level default attribute component to +new_data+ def env_default=(new_data) reset @env_default = VividMash.new(new_data, self, __node__, :env_default) end - # Set the force_default (+default!+) level attributes to +new_data+ + # Set the force_default (+default!+) level attributes to +new_data+ def force_default=(new_data) reset @force_default = VividMash.new(new_data, self, __node__, :force_default) end - # Set the normal level attribute component to +new_data+ + # Set the normal level attribute component to +new_data+ def normal=(new_data) reset @normal = VividMash.new(new_data, self, __node__, :normal) end - # Set the cookbook level override attribute component to +new_data+ + # Set the cookbook level override attribute component to +new_data+ def override=(new_data) reset @override = VividMash.new(new_data, self, __node__, :override) end - # Set the role level override attribute component to +new_data+ + # Set the role level override attribute component to +new_data+ def role_override=(new_data) reset @role_override = VividMash.new(new_data, self, __node__, :role_override) end - # Set the environment level override attribute component to +new_data+ + # Set the environment level override attribute component to +new_data+ def env_override=(new_data) reset @env_override = VividMash.new(new_data, self, __node__, :env_override) @@ -284,11 +288,11 @@ class Chef @automatic = VividMash.new(new_data, self, __node__, :automatic) end - # - # Deleting attributes - # + # + # Deleting attributes + # - # clears attributes from all precedence levels + # clears attributes from all precedence levels def rm(*args) with_deep_merged_return_value(self, *args) do rm_default(*args) @@ -297,11 +301,11 @@ class Chef end end - # clears attributes from all default precedence levels - # - # similar to: force_default!['foo']['bar'].delete('baz') - # - does not autovivify - # - does not trainwreck if interior keys do not exist + # clears attributes from all default precedence levels + # + # similar to: force_default!['foo']['bar'].delete('baz') + # - does not autovivify + # - does not trainwreck if interior keys do not exist def rm_default(*args) with_deep_merged_return_value(combined_default, *args) do default.unlink(*args) @@ -311,20 +315,20 @@ class Chef end end - # clears attributes from normal precedence - # - # equivalent to: normal!['foo']['bar'].delete('baz') - # - does not autovivify - # - does not trainwreck if interior keys do not exist + # clears attributes from normal precedence + # + # equivalent to: normal!['foo']['bar'].delete('baz') + # - does not autovivify + # - does not trainwreck if interior keys do not exist def rm_normal(*args) normal.unlink(*args) end - # clears attributes from all override precedence levels - # - # equivalent to: force_override!['foo']['bar'].delete('baz') - # - does not autovivify - # - does not trainwreck if interior keys do not exist + # clears attributes from all override precedence levels + # + # equivalent to: force_override!['foo']['bar'].delete('baz') + # - does not autovivify + # - does not trainwreck if interior keys do not exist def rm_override(*args) with_deep_merged_return_value(combined_override, *args) do override.unlink(*args) @@ -344,37 +348,37 @@ class Chef private :with_deep_merged_return_value - # - # Replacing attributes without merging - # + # + # Replacing attributes without merging + # - # sets default attributes without merging - # - # - this API autovivifies (and cannot trainwreck) + # sets default attributes without merging + # + # - this API autovivifies (and cannot trainwreck) def default!(*args) return Decorator::Unchain.new(self, :default!) unless args.length > 0 write(:default, *args) end - # sets normal attributes without merging - # - # - this API autovivifies (and cannot trainwreck) + # sets normal attributes without merging + # + # - this API autovivifies (and cannot trainwreck) def normal!(*args) return Decorator::Unchain.new(self, :normal!) unless args.length > 0 write(:normal, *args) end - # sets override attributes without merging - # - # - this API autovivifies (and cannot trainwreck) + # sets override attributes without merging + # + # - this API autovivifies (and cannot trainwreck) def override!(*args) return Decorator::Unchain.new(self, :override!) unless args.length > 0 write(:override, *args) end - # clears from all default precedence levels and then sets force_default - # - # - this API autovivifies (and cannot trainwreck) + # clears from all default precedence levels and then sets force_default + # + # - this API autovivifies (and cannot trainwreck) def force_default!(*args) return Decorator::Unchain.new(self, :force_default!) unless args.length > 0 value = args.pop @@ -382,7 +386,7 @@ class Chef write(:force_default, *args, value) end - # clears from all override precedence levels and then sets force_override + # clears from all override precedence levels and then sets force_override def force_override!(*args) return Decorator::Unchain.new(self, :force_override!) unless args.length > 0 value = args.pop @@ -390,14 +394,13 @@ class Chef write(:force_override, *args, value) end - # - # Accessing merged attributes. - # - # Note that merged_attributes('foo', 'bar', 'baz') can be called to compute only the - # deep merge of node['foo']['bar']['baz'], but in practice we currently always compute - # all of node['foo'] even if the user only requires node['foo']['bar']['baz']. - # - + # + # Accessing merged attributes. + # + # Note that merged_attributes('foo', 'bar', 'baz') can be called to compute only the + # deep merge of node['foo']['bar']['baz'], but in practice we currently always compute + # all of node['foo'] even if the user only requires node['foo']['bar']['baz']. + # def merged_attributes(*path) merge_all(path) end @@ -430,7 +433,8 @@ class Chef instance_variable_get(component_ivar).key?(key) end end - # method-style access to attributes (has to come after the prepended ImmutablizeHash) + + # method-style access to attributes (has to come after the prepended ImmutablizeHash) def read(*path) merged_attributes.read(*path) @@ -479,16 +483,16 @@ class Chef private - # Helper method for merge_all/merge_defaults/merge_overrides. - # - # apply_path(thing, [ "foo", "bar", "baz" ]) = thing["foo"]["bar"]["baz"] - # - # The path value can be nil in which case the whole component is returned. - # - # It returns nil (does not raise an exception) if it walks off the end of an Mash/Hash/Array, it does not - # raise any TypeError if it attempts to apply a hash key to an Integer/String/TrueClass, and just returns - # nil in that case. - # + # Helper method for merge_all/merge_defaults/merge_overrides. + # + # apply_path(thing, [ "foo", "bar", "baz" ]) = thing["foo"]["bar"]["baz"] + # + # The path value can be nil in which case the whole component is returned. + # + # It returns nil (does not raise an exception) if it walks off the end of an Mash/Hash/Array, it does not + # raise any TypeError if it attempts to apply a hash key to an Integer/String/TrueClass, and just returns + # nil in that case. + # def apply_path(component, path) path ||= [] path.inject(component) do |val, path_arg| @@ -509,20 +513,20 @@ class Chef end end - # For elements like Fixnums, true, nil... + # For elements like Fixnums, true, nil... def safe_dup(e) e.dup rescue TypeError e end - # Deep merge all attribute levels using hash-only merging between different precidence - # levels (so override arrays completely replace arrays set at any default level). - # - # The path allows for selectively deep-merging a subtree of the node object. - # - # @param path [Array] Array of args to method chain to descend into the node object - # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object + # Deep merge all attribute levels using hash-only merging between different precidence + # levels (so override arrays completely replace arrays set at any default level). + # + # The path allows for selectively deep-merging a subtree of the node object. + # + # @param path [Array] Array of args to method chain to descend into the node object + # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object def merge_all(path) components = [ merge_defaults(path), @@ -537,12 +541,12 @@ class Chef ret == NIL ? nil : ret end - # Deep merge the default attribute levels with array merging. - # - # The path allows for selectively deep-merging a subtree of the node object. - # - # @param path [Array] Array of args to method chain to descend into the node object - # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object + # Deep merge the default attribute levels with array merging. + # + # The path allows for selectively deep-merging a subtree of the node object. + # + # @param path [Array] Array of args to method chain to descend into the node object + # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object def merge_defaults(path) ret = DEFAULT_COMPONENTS.inject(NIL) do |merged, component_ivar| component_value = apply_path(instance_variable_get(component_ivar), path) @@ -551,12 +555,12 @@ class Chef ret == NIL ? nil : ret end - # Deep merge the override attribute levels with array merging. - # - # The path allows for selectively deep-merging a subtree of the node object. - # - # @param path [Array] Array of args to method chain to descend into the node object - # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object + # Deep merge the override attribute levels with array merging. + # + # The path allows for selectively deep-merging a subtree of the node object. + # + # @param path [Array] Array of args to method chain to descend into the node object + # @return [attr] Deep Merged values (may be VividMash, Hash, Array, etc) from the node object def merge_overrides(path) ret = OVERRIDE_COMPONENTS.inject(NIL) do |merged, component_ivar| component_value = apply_path(instance_variable_get(component_ivar), path) @@ -565,7 +569,7 @@ class Chef ret == NIL ? nil : ret end - # needed for __path__ + # needed for __path__ def convert_key(key) key.kind_of?(Symbol) ? key.to_s : key end @@ -592,11 +596,11 @@ class Chef elsif merge_onto.kind_of?(Array) && merge_with.kind_of?(Array) merge_onto |= merge_with - # If merge_with is nil, don't replace merge_onto + # If merge_with is nil, don't replace merge_onto elsif merge_with.nil? merge_onto - # In all other cases, replace merge_onto with merge_with + # In all other cases, replace merge_onto with merge_with else if merge_with.kind_of?(Hash) Chef::Node::ImmutableMash.new(merge_with) @@ -625,11 +629,11 @@ class Chef end merge_onto - # If merge_with is nil, don't replace merge_onto + # If merge_with is nil, don't replace merge_onto elsif merge_with.nil? merge_onto - # In all other cases, replace merge_onto with merge_with + # In all other cases, replace merge_onto with merge_with else if merge_with.kind_of?(Hash) Chef::Node::ImmutableMash.new(merge_with) |