diff options
Diffstat (limited to 'lib/chef/node/attribute.rb')
-rw-r--r-- | lib/chef/node/attribute.rb | 107 |
1 files changed, 94 insertions, 13 deletions
diff --git a/lib/chef/node/attribute.rb b/lib/chef/node/attribute.rb index 66569cf0e1..3eb6449046 100644 --- a/lib/chef/node/attribute.rb +++ b/lib/chef/node/attribute.rb @@ -58,7 +58,6 @@ class Chef :@force_default ] - OVERRIDE_COMPONENTS = [ :@override, :@role_override, @@ -146,7 +145,6 @@ class Chef METHOD_DEFN end - # return the cookbook level default attribute component attr_reader :default @@ -159,11 +157,6 @@ class Chef # return the force_default level attribute component attr_reader :force_default - # default! is the "advertised" method for force_default, but is - # implemented as an alias because instance variables can't (easily) have - # +!+ characters. - alias :default! :force_default - # return the "normal" level attribute component attr_reader :normal @@ -179,11 +172,6 @@ class Chef # return the force override level attribute component attr_reader :force_override - # +override!+ is the "advertised" method for +force_override+ but is - # implemented as an alias because instance variables can't easily have - # +!+ characters. - alias :override! :force_override - # return the automatic level attribute component attr_reader :automatic @@ -311,6 +299,100 @@ class Chef @automatic = VividMash.new(self, new_data) end + # + # Deleting attributes + # + + # clears attributes from all precedence levels + def rm(*args) + # just easier to compute our retval, rather than collect+merge sub-retvals + ret = args.inject(merged_attributes) do |attr, arg| + if attr.nil? || !attr.respond_to?(:[]) + nil + else + begin + attr[arg] + rescue TypeError + raise TypeError, "Wrong type in index of attribute (did you use a Hash index on an Array?)" + end + end + end + rm_default(*args) + rm_normal(*args) + rm_override(*args) + ret + end + + # does <level>['foo']['bar'].delete('baz') + def remove_from_precedence_level(level, *args, key) + multimash = level.element(*args) + multimash.nil? ? nil : multimash.delete(key) + end + + private :remove_from_precedence_level + + # clears attributes from all default precedence levels + # + # equivalent to: force_default!['foo']['bar'].delete('baz') + def rm_default(*args) + reset + remove_from_precedence_level(force_default!(autovivify: false), *args) + end + + # clears attributes from normal precedence + # + # equivalent to: normal!['foo']['bar'].delete('baz') + def rm_normal(*args) + reset + remove_from_precedence_level(normal!(autovivify: false), *args) + end + + # clears attributes from all override precedence levels + # + # equivalent to: force_override!['foo']['bar'].delete('baz') + def rm_override(*args) + reset + remove_from_precedence_level(force_override!(autovivify: false), *args) + end + + # + # Replacing attributes without merging + # + + # sets default attributes without merging + def default!(opts={}) + reset + MultiMash.new(self, @default, [], opts) + end + + # sets normal attributes without merging + def normal!(opts={}) + reset + MultiMash.new(self, @normal, [], opts) + end + + # sets override attributes without merging + def override!(opts={}) + reset + MultiMash.new(self, @override, [], opts) + end + + # clears from all default precedence levels and then sets force_default + def force_default!(opts={}) + reset + MultiMash.new(self, @force_default, [@default, @env_default, @role_default], opts) + end + + # clears from all override precedence levels and then sets force_override + def force_override!(opts={}) + reset + MultiMash.new(self, @force_override, [@override, @env_override, @role_override], opts) + end + + # + # Accessing merged attributes + # + def merged_attributes @merged_attributes ||= begin components = [merge_defaults, @normal, merge_overrides, @automatic] @@ -391,7 +473,6 @@ class Chef end end - end end |