class Chef::Node::Attribute
Attribute¶ ↑
Attribute implements a nested key-value (Hash) and flat collection (Array) data structure supporting multiple levels of precedence, such that a given key may have multiple values internally, but will only return the highest precedence value when reading.
Constants
- COMPONENTS
List of the component attribute hashes, in order of precedence, low to high.
- DEFAULT_COMPONENTS
- OVERRIDE_COMPONENTS
Attributes
return the automatic level attribute component
return the cookbook level default attribute component
return the #force_default level attribute component
return the environment level default attribute component
return the enviroment level override attribute component
return the #force_default level attribute component
return the force override level attribute component
return the “normal” level attribute component
return the cookbook level override attribute component
return the force override level attribute component
return the role level default attribute component
return the role level override attribute component
Public Class Methods
# File lib/chef/node/attribute.rb, line 190 def initialize(normal, default, override, automatic) @set_unless_present = false @default = VividMash.new(self, default) @env_default = VividMash.new(self, {}) @role_default = VividMash.new(self, {}) @force_default = VividMash.new(self, {}) @normal = VividMash.new(self, normal) @override = VividMash.new(self, override) @role_override = VividMash.new(self, {}) @env_override = VividMash.new(self, {}) @force_override = VividMash.new(self, {}) @automatic = VividMash.new(self, automatic) @merged_attributes = nil @combined_override = nil @combined_default = nil end
Public Instance Methods
# File lib/chef/node/attribute.rb, line 332 def [](key) merged_attributes[key] end
# File lib/chef/node/attribute.rb, line 336 def []=(key, value) merged_attributes[key] = value end
# File lib/chef/node/attribute.rb, line 309 def automatic=(new_data) reset @automatic = VividMash.new(self, new_data) end
# File lib/chef/node/attribute.rb, line 328 def combined_default @combined_default ||= immutablize(merge_defaults) end
# File lib/chef/node/attribute.rb, line 324 def combined_override @combined_override ||= immutablize(merge_overrides) 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[:default_interface]` is coming
from, use:
debug_value(:network, :default_interface).
The return value is an Array of Arrays. The first element is
`[“set_unless_enabled?”, Boolean]`, which describes whether the attribute
collection is in “set_unless” mode. The rest of 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
.
# File lib/chef/node/attribute.rb, line 223 def debug_value(*args) components = COMPONENTS.map do |component| ivar = instance_variable_get(component) value = args.inject(ivar) do |so_far, key| if so_far == :not_present :not_present elsif so_far.has_key?(key) so_far[key] else :not_present end end [component.to_s.sub(/^@/,""), value] end [["set_unless_enabled?", @set_unless_present]] + components end
Set the cookbook level default attribute component to
new_data
.
# File lib/chef/node/attribute.rb, line 257 def default=(new_data) reset @default = VividMash.new(self, new_data) end
Set the environment level default attribute component to
new_data
# File lib/chef/node/attribute.rb, line 269 def env_default=(new_data) reset @env_default = VividMash.new(self, new_data) end
Set the environment level override attribute component to
new_data
# File lib/chef/node/attribute.rb, line 299 def env_override=(new_data) reset @env_override = VividMash.new(self, new_data) end
Set the #force_default
(default!
) level attributes to new_data
# File lib/chef/node/attribute.rb, line 275 def force_default=(new_data) reset @force_default = VividMash.new(self, new_data) end
# File lib/chef/node/attribute.rb, line 304 def force_override=(new_data) reset @force_override = VividMash.new(self, new_data) end
# File lib/chef/node/attribute.rb, line 340 def has_key?(key) COMPONENTS.any? do |component_ivar| instance_variable_get(component_ivar).has_key?(key) end end
# File lib/chef/node/attribute.rb, line 368 def inspect "#<#{self.class} " << (COMPONENTS + [:@merged_attributes, :@properties]).map{|iv| "#{iv}=#{instance_variable_get(iv).inspect}" }.join(', ') << ">" end
# File lib/chef/node/attribute.rb, line 314 def merged_attributes @merged_attributes ||= begin components = [merge_defaults, @normal, merge_overrides, @automatic] resolved_attrs = components.inject(Mash.new) do |merged, component| Chef::Mixin::DeepMerge.hash_only_merge(merged, component) end immutablize(resolved_attrs) end end
# File lib/chef/node/attribute.rb, line 353 def method_missing(symbol, *args) if args.empty? if key?(symbol) self[symbol] else raise NoMethodError, "Undefined method or attribute `#{symbol}' on `node'" end elsif symbol.to_s =~ /=$/ key_to_set = symbol.to_s[/^(.+)=$/, 1] self[key_to_set] = (args.length == 1 ? args[0] : args) else raise NoMethodError, "Undefined node attribute or method `#{symbol}' on `node'" end end
Set the normal level attribute component to new_data
# File lib/chef/node/attribute.rb, line 281 def normal=(new_data) reset @normal = VividMash.new(self, new_data) end
Set the cookbook level override attribute component to
new_data
# File lib/chef/node/attribute.rb, line 287 def override=(new_data) reset @override = VividMash.new(self, new_data) end
Clears #merged_attributes, which will cause it to be recomputed on the next access.
# File lib/chef/node/attribute.rb, line 247 def reset_cache @merged_attributes = nil @combined_default = nil @combined_override = nil @set_unless_present = false end
Set the role level default attribute component to new_data
# File lib/chef/node/attribute.rb, line 263 def role_default=(new_data) reset @role_default = VividMash.new(self, new_data) end
Set the role level override attribute component to new_data
# File lib/chef/node/attribute.rb, line 293 def role_override=(new_data) reset @role_override = VividMash.new(self, new_data) end
# File lib/chef/node/attribute.rb, line 374 def set_unless? @set_unless_present end
Enables or disables `||=`-like attribute setting. See, e.g., Chef::Node#set_unless
# File lib/chef/node/attribute.rb, line 241 def set_unless_value_present=(setting) @set_unless_present = setting end
Private Instance Methods
# File lib/chef/node/attribute.rb, line 380 def merge_defaults DEFAULT_COMPONENTS.inject(Mash.new) do |merged, component_ivar| component_value = instance_variable_get(component_ivar) Chef::Mixin::DeepMerge.merge(merged, component_value) end end
# File lib/chef/node/attribute.rb, line 387 def merge_overrides OVERRIDE_COMPONENTS.inject(Mash.new) do |merged, component_ivar| component_value = instance_variable_get(component_ivar) Chef::Mixin::DeepMerge.merge(merged, component_value) end end