Skip to content

Instantly share code, notes, and snippets.

@mikehale
Created February 16, 2009 16:54
Show Gist options
  • Select an option

  • Save mikehale/65248 to your computer and use it in GitHub Desktop.

Select an option

Save mikehale/65248 to your computer and use it in GitHub Desktop.
# So imagine that you have an attribute mash named apache:
# apache = Mash.new unless attribute?("apache")
#
# And you'd like to prefer the values defined by the node over the defaults in your attributes file:
# apache[:version] = '2.0.6' unless apache.has_key?(:version)
#
# Now instead of having to check every key you can do something like this:
# apache Mash.new unless attribute?("apache")
# apache.prefer_defaults = false
# apache[:version] = '2.0.6'
class Mash
attr_accessor :prefer_defaults
@prefer_defaults = false
hash_writer = instance_method('[]=')
define_method('[]=') do |key,value|
if prefer_defaults
hash_writer.bind(self).call(key, value)
else
hash_writer.bind(self).call(key, value) unless has_key?(key)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment