Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save josephholsten/a0b41dd718f753249608ca15fc587296 to your computer and use it in GitHub Desktop.
Save josephholsten/a0b41dd718f753249608ca15fc587296 to your computer and use it in GitHub Desktop.
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