diff options
author | Daniel DeLeo <dan@opscode.com> | 2010-08-22 11:18:43 -0700 |
---|---|---|
committer | Daniel DeLeo <dan@opscode.com> | 2010-08-22 11:18:43 -0700 |
commit | 7fd94a998104f790ef25e7fb03d877510e5ab260 (patch) | |
tree | 02fc98844b2058bce73c837c8a8329cd82c27a08 /chef/lib/chef/node | |
parent | 12b97322e7ac4d293252f1464994d0883be2ea8a (diff) | |
download | chef-7fd94a998104f790ef25e7fb03d877510e5ab260.tar.gz |
[CHEF-1610] refactor c/n/a method_missing for clarity
Diffstat (limited to 'chef/lib/chef/node')
-rw-r--r-- | chef/lib/chef/node/attribute.rb | 37 |
1 files changed, 20 insertions, 17 deletions
diff --git a/chef/lib/chef/node/attribute.rb b/chef/lib/chef/node/attribute.rb index e7f99d09d4..a4799f8389 100644 --- a/chef/lib/chef/node/attribute.rb +++ b/chef/lib/chef/node/attribute.rb @@ -404,27 +404,30 @@ class Chef end end + # Fetches or sets the value, depending on if any arguments are given. + # ==== Fetching + # If no arguments are given, fetches the value: + # node.network + # => {network data} + # Getters will find either a string or symbol key. + # ==== Setting + # If arguments are given, a value will be set. Both normal setter and DSL + # style setters are allowed: + # node.foo = "bar" + # node.foo("bar") + # Both set node[:foo] = "bar" def method_missing(symbol, *args) - by = symbol - if self.attribute?(symbol) - by = symbol - elsif self.attribute?(symbol.to_s) - by = symbol.to_s - else - if args.length != 0 - by = symbol + if args.empty? + if key?(symbol) + self[symbol] + elsif key?(symbol.to_s) + self[symbol.to_s] else - raise ArgumentError, "Attribute #{symbol.to_s} is not defined!" unless auto_vivifiy_on_read - end - end - - if args.length != 0 - if by.to_s =~ /^(.+)=$/ - by = $1 + raise ArgumentError, "Attribute #{symbol} is not defined!" unless auto_vivifiy_on_read end - self[by] = args.length == 1 ? args[0] : args else - self[by] + key_to_set = symbol.to_s[/^(.+)=$/, 1] || symbol + self[key_to_set] = (args.length == 1 ? args[0] : args) end end |