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.
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.
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.
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).
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.
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" = .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? = nil end nil end |
#old_method_missing ⇒ Object
156 |
# File 'lib/chef/sugar/node.rb', line 156 alias_method :old_method_missing, :method_missing |