Accessing Chef node attributes
Topics: Clean Chef code
Using a helper method is part of good Clean Chef code.
It shortens evaluation from node["my_namespace"]["my_attribute"] to cb_attr("my_attribute").
module MyCookbook
module Helpers
def cb_attr(name)
attribute_namespace = node["my_cookbook"]
if attribute_namespace.key?(name)
attribute_namespace[name]
else
raise "Undefined node attribute `#{name}`"
end
end
end
end