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
ENUM_METHODS
NIL
OVERRIDE_COMPONENTS

Attributes

automatic[R]

return the automatic level attribute component

default[R]

return the cookbook level default attribute component

env_default[R]

return the environment level default attribute component

env_override[R]

return the environment 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

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, node = nil) click to toggle source
Calls superclass method Chef::Node::Mixin::StateTracking::new
# File lib/chef/node/attribute.rb, line 198
def initialize(normal, default, override, automatic, node = nil)
  @default        = VividMash.new(default, self, node, :default)
  @env_default    = VividMash.new({}, self, node, :env_default)
  @role_default   = VividMash.new({}, self, node, :role_default)
  @force_default  = VividMash.new({}, self, node, :force_default)

  @normal = VividMash.new(normal, self, node, :normal)

  @override       = VividMash.new(override, self, node, :override)
  @role_override  = VividMash.new({}, self, node, :role_override)
  @env_override   = VividMash.new({}, self, node, :env_override)
  @force_override = VividMash.new({}, self, node, :force_override)

  @automatic = VividMash.new(automatic, self, node, :automatic)

  super(nil, self, node, :merged)
end

Public Instance Methods

attribute?(key)
Alias for: has_key?
automatic=(new_data) click to toggle source
# File lib/chef/node/attribute.rb, line 290
def automatic=(new_data)
  reset
  @automatic = VividMash.new(new_data, self, __node__, :automatic)
end
combined_default(*path) click to toggle source
# File lib/chef/node/attribute.rb, line 423
def combined_default(*path)
  ret = merge_defaults(path)
  ret == NIL ? nil : ret
end
combined_override(*path) click to toggle source
# File lib/chef/node/attribute.rb, line 418
def combined_override(*path)
  ret = merge_overrides(path)
  ret == NIL ? nil : ret
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 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 225
def debug_value(*args)
  COMPONENTS.map do |component|
    value =
      begin
        instance_variable_get(component).read!(*args)
      rescue
        :not_present
      end
    [component.to_s.sub(/^@/, ""), value]
  end
end
default!(*args) click to toggle source

sets default attributes without merging

  • this API autovivifies (and cannot trainwreck)

# File lib/chef/node/attribute.rb, line 363
def default!(*args)
  return Decorator::Unchain.new(self, :default!) unless args.length > 0

  write(:default, *args)
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 238
def default=(new_data)
  reset
  @default = VividMash.new(new_data, self, __node__, :default)
end
default_unless(*args) click to toggle source
# File lib/chef/node/attribute.rb, line 434
def default_unless(*args)
  return Decorator::Unchain.new(self, :default_unless) unless args.length > 0

  write(:default, *args) if default.read(*args[0...-1]).nil?
end
dig(*path)
Alias for: read
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 250
def env_default=(new_data)
  reset
  @env_default = VividMash.new(new_data, self, __node__, :env_default)
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 280
def env_override=(new_data)
  reset
  @env_override = VividMash.new(new_data, self, __node__, :env_override)
end
exist?(*path) click to toggle source
# File lib/chef/node/attribute.rb, line 464
def exist?(*path)
  merged_attributes.exist?(*path)
end
force_default!(*args) click to toggle source

clears from all default precedence levels and then sets force_default

  • this API autovivifies (and cannot trainwreck)

# File lib/chef/node/attribute.rb, line 390
def force_default!(*args)
  return Decorator::Unchain.new(self, :force_default!) unless args.length > 0

  value = args.pop
  rm_default(*args)
  write(:force_default, *args, value)
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 256
def force_default=(new_data)
  reset
  @force_default = VividMash.new(new_data, self, __node__, :force_default)
end
force_override!(*args) click to toggle source

clears from all override precedence levels and then sets force_override

# File lib/chef/node/attribute.rb, line 399
def force_override!(*args)
  return Decorator::Unchain.new(self, :force_override!) unless args.length > 0

  value = args.pop
  rm_override(*args)
  write(:force_override, *args, value)
end
force_override=(new_data) click to toggle source
# File lib/chef/node/attribute.rb, line 285
def force_override=(new_data)
  reset
  @force_override = VividMash.new(new_data, self, __node__, :force_override)
end
has_key?(key) click to toggle source
# File lib/chef/node/attribute.rb, line 446
def has_key?(key)
  COMPONENTS.any? do |component_ivar|
    instance_variable_get(component_ivar).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 495
def inspect
  "#<#{self.class} " << (COMPONENTS + %i{@merged_attributes @properties}).map do |iv|
    "#{iv}=#{instance_variable_get(iv).inspect}"
  end.join(", ") << ">"
end
key?(key)
Alias for: has_key?
member?(key)
Alias for: has_key?
merged_attributes(*path) click to toggle source

Accessing merged attributes.

Note that merged_attributes('foo', 'bar', 'baz') can be called to compute only the deep merge of node['bar’], but in practice we currently always compute all of node even if the user only requires node['bar’].

# File lib/chef/node/attribute.rb, line 414
def merged_attributes(*path)
  merge_all(path)
end
normal!(*args) click to toggle source

sets normal attributes without merging

  • this API autovivifies (and cannot trainwreck)

# File lib/chef/node/attribute.rb, line 372
def normal!(*args)
  return Decorator::Unchain.new(self, :normal!) unless args.length > 0

  write(:normal, *args)
end
normal=(new_data) click to toggle source

Set the normal level attribute component to new_data

# File lib/chef/node/attribute.rb, line 262
def normal=(new_data)
  reset
  @normal = VividMash.new(new_data, self, __node__, :normal)
end
normal_unless(*args) click to toggle source
# File lib/chef/node/attribute.rb, line 428
def normal_unless(*args)
  return Decorator::Unchain.new(self, :normal_unless) unless args.length > 0

  write(:normal, *args) if normal.read(*args[0...-1]).nil?
end
override!(*args) click to toggle source

sets override attributes without merging

  • this API autovivifies (and cannot trainwreck)

# File lib/chef/node/attribute.rb, line 381
def override!(*args)
  return Decorator::Unchain.new(self, :override!) unless args.length > 0

  write(:override, *args)
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 268
def override=(new_data)
  reset
  @override = VividMash.new(new_data, self, __node__, :override)
end
override_unless(*args) click to toggle source
# File lib/chef/node/attribute.rb, line 440
def override_unless(*args)
  return Decorator::Unchain.new(self, :override_unless) unless args.length > 0

  write(:override, *args) if override.read(*args[0...-1]).nil?
end
read(*path) click to toggle source

method-style access to attributes (has to come after the prepended ImmutablizeHash)

# File lib/chef/node/attribute.rb, line 454
def read(*path)
  merged_attributes.read(*path)
end
Also aliased as: dig
read!(*path) click to toggle source
# File lib/chef/node/attribute.rb, line 460
def read!(*path)
  merged_attributes.read!(*path)
end
rm(*args) click to toggle source

clears attributes from all precedence levels

# File lib/chef/node/attribute.rb, line 300
def rm(*args)
  with_deep_merged_return_value(self, *args) do
    rm_default(*args)
    rm_normal(*args)
    rm_override(*args)
  end
end
rm_default(*args) click to toggle source

clears attributes from all default precedence levels

similar to: force_default!['bar'].delete('baz')

  • does not autovivify

  • does not trainwreck if interior keys do not exist

# File lib/chef/node/attribute.rb, line 313
def rm_default(*args)
  with_deep_merged_return_value(combined_default, *args) do
    default.unlink(*args)
    role_default.unlink(*args)
    env_default.unlink(*args)
    force_default.unlink(*args)
  end
end
rm_normal(*args) click to toggle source

clears attributes from normal precedence

equivalent to: normal!['bar'].delete('baz')

  • does not autovivify

  • does not trainwreck if interior keys do not exist

# File lib/chef/node/attribute.rb, line 327
def rm_normal(*args)
  normal.unlink(*args)
end
rm_override(*args) click to toggle source

clears attributes from all override precedence levels

equivalent to: force_override!['bar'].delete('baz')

  • does not autovivify

  • does not trainwreck if interior keys do not exist

# File lib/chef/node/attribute.rb, line 336
def rm_override(*args)
  with_deep_merged_return_value(combined_override, *args) do
    override.unlink(*args)
    role_override.unlink(*args)
    env_override.unlink(*args)
    force_override.unlink(*args)
  end
end
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 244
def role_default=(new_data)
  reset
  @role_default = VividMash.new(new_data, self, __node__, :role_default)
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 274
def role_override=(new_data)
  reset
  @role_override = VividMash.new(new_data, self, __node__, :role_override)
end
to_s() click to toggle source
# File lib/chef/node/attribute.rb, line 491
def to_s
  merged_attributes.to_s
end
write(level, *args, &block) click to toggle source
# File lib/chef/node/attribute.rb, line 468
def write(level, *args, &block)
  send(level).write(*args, &block)
end
write!(level, *args, &block) click to toggle source
# File lib/chef/node/attribute.rb, line 472
def write!(level, *args, &block)
  send(level).write!(*args, &block)
end

Private Instance Methods

apply_path(component, path) click to toggle source

Helper method for merge_all/merge_defaults/merge_overrides.

apply_path(thing, [ “foo”, “bar”, “baz” ]) = thing[“bar”]

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.

# File lib/chef/node/attribute.rb, line 513
def apply_path(component, path)
  path ||= []
  path.inject(component) do |val, path_arg|
    if val.respond_to?(:[])
      # Have an Array-like or Hash-like thing
      if !val.respond_to?(:has_key?)
        # Have an Array-like thing
        val[path_arg]
      elsif val.key?(path_arg)
        # Hash-like thing (must check has_key? first to protect against Autovivification)
        val[path_arg]
      else
        NIL
      end
    else
      NIL
    end
  end
end
convert_key(key) click to toggle source

needed for __path__

# File lib/chef/node/attribute.rb, line 588
def convert_key(key)
  key.is_a?(Symbol) ? key.to_s : key
end
deep_merge!(merge_onto, merge_with) click to toggle source

@api private

# File lib/chef/node/attribute.rb, line 595
def deep_merge!(merge_onto, merge_with)
  # If there are two Hashes, recursively merge.
  if merge_onto.is_a?(Hash) && merge_with.is_a?(Hash)
    merge_with.each do |key, merge_with_value|
      value =
        if merge_onto.key?(key)
          deep_merge!(safe_dup(merge_onto.internal_get(key)), merge_with_value)
        else
          merge_with_value
        end

      # internal_set bypasses converting keys, does convert values and allows writing to immutable mashes
      merge_onto.internal_set(key, value)
    end
    merge_onto

  elsif merge_onto.is_a?(Array) && merge_with.is_a?(Array)
    merge_onto |= merge_with

  # 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
  else
    if merge_with.is_a?(Hash)
      Chef::Node::ImmutableMash.new(merge_with)
    elsif merge_with.is_a?(Array)
      Chef::Node::ImmutableArray.new(merge_with)
    else
      merge_with
    end
  end
end
hash_only_merge!(merge_onto, merge_with) click to toggle source

@api private

# File lib/chef/node/attribute.rb, line 631
def hash_only_merge!(merge_onto, merge_with)
  # If there are two Hashes, recursively merge.
  if merge_onto.is_a?(Hash) && merge_with.is_a?(Hash)
    merge_with.each do |key, merge_with_value|
      value =
        if merge_onto.key?(key)
          hash_only_merge!(safe_dup(merge_onto.internal_get(key)), merge_with_value)
        else
          merge_with_value
        end

      # internal_set bypasses converting keys, does convert values and allows writing to immutable mashes
      merge_onto.internal_set(key, value)
    end
    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
  else
    if merge_with.is_a?(Hash)
      Chef::Node::ImmutableMash.new(merge_with)
    elsif merge_with.is_a?(Array)
      Chef::Node::ImmutableArray.new(merge_with)
    else
      merge_with
    end
  end
end
merge_all(path) click to toggle source

Deep merge all attribute levels using hash-only merging between different precedence 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

# File lib/chef/node/attribute.rb, line 547
def merge_all(path)
  components = [
    merge_defaults(path),
    apply_path(@normal, path),
    merge_overrides(path),
    apply_path(@automatic, path),
  ]

  ret = components.inject(NIL) do |merged, component|
    hash_only_merge!(merged, component)
  end
  ret == NIL ? nil : ret
end
merge_defaults(path) click to toggle source

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

# File lib/chef/node/attribute.rb, line 567
def merge_defaults(path)
  DEFAULT_COMPONENTS.inject(NIL) do |merged, component_ivar|
    component_value = apply_path(instance_variable_get(component_ivar), path)
    deep_merge!(merged, component_value)
  end
end
merge_overrides(path) click to toggle source

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

# File lib/chef/node/attribute.rb, line 580
def merge_overrides(path)
  OVERRIDE_COMPONENTS.inject(NIL) do |merged, component_ivar|
    component_value = apply_path(instance_variable_get(component_ivar), path)
    deep_merge!(merged, component_value)
  end
end
safe_dup(e) click to toggle source

For elements like Fixnums, true, nil…

# File lib/chef/node/attribute.rb, line 534
def safe_dup(e)
  e.dup
rescue TypeError
  e
end
with_deep_merged_return_value(obj, *path, last) { || ... } click to toggle source
# File lib/chef/node/attribute.rb, line 345
def with_deep_merged_return_value(obj, *path, last)
  hash = obj.read(*path)
  return nil unless hash.is_a?(Hash)

  ret = hash[last]
  yield
  ret
end