Class: Chef::Node
Defined Under Namespace
Classes: AttributeDoesNotExistError
Instance Method Summary collapse
-
#deep_fetch(*keys) ⇒ Object
Safely fetch a deeply nested attribute by specifying a list of keys, bypassing Ruby’s Hash notation.
-
#deep_fetch!(*keys) ⇒ Object
Deeply fetch a node attribute by specifying a list of keys, bypassing Ruby’s Hash notation.
-
#in?(environment) ⇒ Boolean
Determine if the current node is in the given Chef environment (or matches the given regular expression).
-
#method_missing(m, *args, &block) ⇒ nil, Object
Provide a nice DSL for defining attributes.
-
#namespace(*args, &block) ⇒ nil
Dynamically define the current namespace.
- #old_method_missing ⇒ Object
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.
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/chef/sugar/node.rb', line 155 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.
50 51 52 53 54 |
# File 'lib/chef/sugar/node.rb', line 50 def deep_fetch(*keys) 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.
72 73 74 75 76 77 78 79 80 |
# File 'lib/chef/sugar/node.rb', line 72 def deep_fetch!(*keys) keys.map!(&:to_s) keys.inject(attributes.to_hash) do |hash, key| hash[key] end rescue NoMethodError raise AttributeDoesNotExistError.new(keys) end |
#in?(environment) ⇒ Boolean
Determine if the current node is in the given Chef environment (or matches the given regular expression).
37 38 39 |
# File 'lib/chef/sugar/node.rb', line 37 def in?(environment) environment === chef_environment end |
#namespace(*args, &block) ⇒ nil
Dynamically define the current namespace. Multiple namespaces may be nested.
132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/chef/sugar/node.rb', line 132 def namespace(*args, &block) @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 nil end |
#old_method_missing ⇒ Object
144 |
# File 'lib/chef/sugar/node.rb', line 144 alias_method :old_method_missing, :method_missing |