summaryrefslogtreecommitdiff
path: root/lib/chef/knife/core
diff options
context:
space:
mode:
authorLamont Granquist <lamont@scriptkiddie.org>2016-09-20 11:38:28 -0700
committerLamont Granquist <lamont@scriptkiddie.org>2016-09-20 11:38:28 -0700
commit057c450f4aa02e7f97a9cce6d506899a190509c1 (patch)
treea28b8bbc118267b586d3d63579f83ae972bc65c9 /lib/chef/knife/core
parent795e2cb806407151aa009fe0f56f138d1c5a7b2d (diff)
downloadchef-057c450f4aa02e7f97a9cce6d506899a190509c1.tar.gz
fix method_access and array handling in node presenter
Signed-off-by: Lamont Granquist <lamont@scriptkiddie.org>
Diffstat (limited to 'lib/chef/knife/core')
-rw-r--r--lib/chef/knife/core/generic_presenter.rb28
1 files changed, 12 insertions, 16 deletions
diff --git a/lib/chef/knife/core/generic_presenter.rb b/lib/chef/knife/core/generic_presenter.rb
index f273cb5bca..323c2cf350 100644
--- a/lib/chef/knife/core/generic_presenter.rb
+++ b/lib/chef/knife/core/generic_presenter.rb
@@ -175,23 +175,19 @@ class Chef
def extract_nested_value(data, nested_value_spec)
nested_value_spec.split(".").each do |attr|
- if data.nil?
- nil # don't get no method error on nil
- # Must check :[] before attr because spec can include
- # `keys` - want the key named `keys`, not a list of
- # available keys.
- elsif data.respond_to?(:[]) && data.has_key?(attr)
- data = data[attr]
- elsif data.respond_to?(attr.to_sym)
- data = data.send(attr.to_sym)
- else
- data = begin
- data.send(attr.to_sym)
- rescue NoMethodError
- nil
- end
- end
+ data =
+ if data.is_a?(Array)
+ data[attr.to_i]
+ elsif data.respond_to?(:[], false) && data.key?(attr)
+ data[attr]
+ elsif data.respond_to?(attr.to_sym, false)
+ # handles -a chef_environment and other things that hang of the node and aren't really attributes
+ data.public_send(attr.to_sym)
+ else
+ nil
+ end
end
+ # necessary for coercing hashable non-attribute objects hanging off the node into real hashes
( !data.kind_of?(Array) && data.respond_to?(:to_hash) ) ? data.to_hash : data
end