Method: Chef::Node#namespace

Defined in:
lib/chef/sugar/node.rb

#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



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

def namespace(*args, &block)
  @namespace_options = {
    precedence: default
  }.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

  nil
end