# 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

automatic[R]

return the automatic level attribute component

default[R]

return the cookbook level default attribute component

default![R]

return the #force_default level attribute component

env_default[R]

return the environment level default attribute component

env_override[R]

return the enviroment level override attribute component

force_default[R]

return the #force_default level attribute component

force_override[R]

return the force override level attribute component

normal[R]

return the “normal” level attribute component

override[R]

return the cookbook level override attribute component

override![R]

return the force override level attribute component

role_default[R]

return the role level default attribute component

role_override[R]

return the role level override attribute component

### Public Class Methods

new(normal, default, override, automatic) click to toggle source

    # 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

[](key) click to toggle source

    # File lib/chef/node/attribute.rb, line 332
    def [](key)
      merged_attributes[key]
    end

[]=(key, value) click to toggle source

    # File lib/chef/node/attribute.rb, line 336
    def []=(key, value)
      merged_attributes[key] = value
    end

attribute?(key)
Alias for: has_key?
automatic=(new_data) click to toggle source

    # File lib/chef/node/attribute.rb, line 309
    def automatic=(new_data)
      reset
      @automatic = VividMash.new(self, new_data)
    end

combined_default() click to toggle source

    # File lib/chef/node/attribute.rb, line 328
    def combined_default
      @combined_default ||= immutablize(merge_defaults)
    end

combined_override() click to toggle source

    # File lib/chef/node/attribute.rb, line 324
    def combined_override
      @combined_override ||= immutablize(merge_overrides)
    end

debug_value(*args) click to toggle source

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

default=(new_data) click to toggle source

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

env_default=(new_data) click to toggle source

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

env_override=(new_data) click to toggle source

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

force_default=(new_data) click to toggle source

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

force_override=(new_data) click to toggle source

    # File lib/chef/node/attribute.rb, line 304
    def force_override=(new_data)
      reset
      @force_override = VividMash.new(self, new_data)
    end

has_key?(key) click to toggle source

    # 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

Also aliased as: attribute?, member?, include?, key?
include?(key)
Alias for: has_key?
inspect() click to toggle source

    # 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

key?(key)
Alias for: has_key?
member?(key)
Alias for: has_key?
merged_attributes() click to toggle source

    # 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

method_missing(symbol, *args) click to toggle source

    # 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

normal=(new_data) click to toggle source

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

override=(new_data) click to toggle source

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

reset()
Alias for: reset_cache
reset_cache() click to toggle source

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

Also aliased as: reset
role_default=(new_data) click to toggle source

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

role_override=(new_data) click to toggle source

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

set_unless?() click to toggle source

    # File lib/chef/node/attribute.rb, line 374
    def set_unless?
      @set_unless_present
    end

set_unless_value_present=(setting) click to toggle source

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