I\'m attempting to write a library for a chef cookbook that simplifies some common searches.
For example, I\'d like to be able to do something like this in coo
I just ran into this while trying to access the current environment in a library. I couldn't really figure out how to use modules to get access to the node and I didn't want to pass the node into each method call (or the instantiation call) so I did this (example code.. not the actual functionality):
# libraries/account.rb
class Account
@@env = "_default"
def self.env=(env)
@@env = env
end
def settings
Chef::EncryptedDataBagItem.load(@@env, "settings") || {}
end
end
# recipes/accounts.rb
Account.env = node.chef_environment
Account.new.settings
I don't know if using class variables is frowned upon, but it works in all my tests and it's nice and easy to use.