Class: Chef::Node

Inherits:
Object show all
Defined in:
lib/chef/sugar/node.rb

Defined Under Namespace

Classes: AttributeDoesNotExistError

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(m, *args, &block) ⇒ nil, Object

Provide a nice DSL for defining attributes. method_missing is called on all the attribute names. For more information on how to use the DSL, see the class-level documentation.

Returns:

  • (nil)

    to prevent accidential method chaining if the block isn’t closed

  • (Object)

    If no argument is passed in, method becomes an attribute accessor



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/chef/sugar/node.rb', line 167

def method_missing(m, *args, &block)
  old_method_missing(m, *args, &block)
rescue NoMethodError
  # The Node Attribute's key is the method name
  key = m.to_s
  # If arguments are passed in, set node attribute with args as the value
  if args.size > 0
    vivified[key] = args.size == 1 ? args.first : args
    return nil
  # If no arguments are passed in, attempt to access corresponding attribute
  else
    deep_key = current_namespace.dup << key
    return deep_fetch!(*deep_key)
  end
end

Instance Method Details

#deep_fetch(*keys) ⇒ Object

Safely fetch a deeply nested attribute by specifying a list of keys, bypassing Ruby’s Hash notation. This method swallows NoMethodError exceptions, avoiding the most common error in Chef-land.

This method will return nil if any deeply nested key does not exist.

See Also:

  • Chef::Node.[Node[Node#deep_fetch!]


54
55
56
57
58
59
# File 'lib/chef/sugar/node.rb', line 54

def deep_fetch(*keys)
  Chef::Sugar::Deprecation.deprecated "the chef-sugar deep_fetch method is deprecated and should be replaced by node.read"
  deep_fetch!(*keys)
rescue NoMethodError, AttributeDoesNotExistError
  nil
end

#deep_fetch!(*keys) ⇒ Object

Deeply fetch a node attribute by specifying a list of keys, bypassing Ruby’s Hash notation.

This method will raise any exceptions, such as undefined method ‘[]’ for nil:NilClass, just as if you used the native attribute notation. If you want a safely vivified hash, see #deep_fetch.

Examples:

Fetch a deeply nested key

node.deep_fetch(:foo, :bar, :zip) #=> node['foo']['bar']['zip']

Parameters:

  • keys (Array<String, Symbol>)

    the list of keys to kdeep fetch

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/sugar/node.rb', line 77

def deep_fetch!(*keys)
  Chef::Sugar::Deprecation.deprecated "the chef-sugar deep_fetch method is deprecated and should be replaced by node.read!"
  keys.map!(&:to_s)

  keys.inject(attributes.to_hash) do |hash, key|
    if hash.key?(key)
      hash[key]
    else
      raise AttributeDoesNotExistError.new(keys, key)
    end
  end
end

#in?(environment) ⇒ Boolean

Determine if the current node is in the given Chef environment (or matches the given regular expression).

Parameters:

Returns:

  • (Boolean)


40
41
42
43
# File 'lib/chef/sugar/node.rb', line 40

def in?(environment)
  Chef::Sugar::Deprecation.deprecated "the chef-sugar node.in? method is deprecated"
  environment === chef_environment
end

#namespace(*args, &block) ⇒ nil

Dynamically define the current namespace. Multiple namespaces may be nested.

Examples:

Define a simple namespace


namespace 'apache2' do
  # ...
end

Define a nested namespace


namespace 'apache2', 'config' do
  # ...
end

Define a complex nested namespace


namespace 'apache2' do
  namespace 'config' do
    # ...
  end
end

Define a namespace with a custom precedence level


namespace 'apache2', precedence: normal do
  # Attributes here will use the "normal" level
end

Define different nested precedence levels


namespace 'apache2', precedence: normal do
  # Attributes defined here will use the "normal" level

  namespace 'config', precedence: override do
    # Attributes defined  here will use the "override" level
  end
end

Parameters:

  • args (Array)

    the list of arguments (such as the namespace and precedence levels) the user gave

  • block (Proc)

    the nested evaluation context

Returns:

  • (nil)

    to prevent accidential method chaining if the block isn’t closed



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/chef/sugar/node.rb', line 140

def namespace(*args, &block)
  Chef::Sugar::Deprecation.deprecated "the chef-sugar attribute namespace setting is deprecated, please use traditional chef attribute notation"
  @namespace_options = namespace_options.merge(args.last.is_a?(Hash) ? args.pop : {})

  keys = args.map(&:to_s)

  @current_namespace = current_namespace + keys
  instance_eval(&block)
  @current_namespace = current_namespace - keys

  if @current_namespace.empty?
    @namespace_options = nil
  end
  nil
end

#old_method_missingObject



156
# File 'lib/chef/sugar/node.rb', line 156

alias_method :old_method_missing, :method_missing