Forked from jdblack/gist:1eed5d3a5c2d155159447ce5362a462f
Last active
February 28, 2017 00:05
-
-
Save josephholsten/a0b41dd718f753249608ca15fc587296 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Chef::Node | |
# dig takes a node or databag and traverses through node/hash attributes according to the provided path | |
# databagitem.dig('this.attribute.here') | |
def dig(path, default=nil) | |
# works fine in chef 10 | |
path.split(".").inject(self) do |l,k| | |
if l.respond_to?(:keys) | |
(l.to_hash[k] || l.to_hash[k.to_sym] || default) | |
else | |
default | |
end | |
end | |
end | |
end | |
class Chef::DataBagItem | |
# dig takes a node or databag and traverses through node/hash attributes according to the provided path | |
# databagitem.dig('this.attribute.here') | |
def dig(path, default=nil) | |
# works fine in chef 10 | |
path.split(".").inject(self) do |l,k| | |
if l.respond_to?(:keys) | |
(l.to_hash[k] || l.to_hash[k.to_sym] || default) | |
else | |
default | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment