MyGem.configure block - GIANT ROBOTS SMASHING INTO OTHER GIANT ROBOTS:
January 20, 2010 Tagged: pattern ruby gem configuration Comments (View) MyGem.configure block We recently made some enhancements to Clearance . One of those was to replace a manual process of the developer setting constants in their Rails environment files with a configuration block that could be used in config/initializers/clearance.rb. I liked the way Hoptoad does it and wanted to implement the same pattern: HoptoadNotifier.configure do |config| config.api_key = 'your_key_here' end So let’s implement: module Clearance class << self attr_accessor :configuration end def self.configure self.configuration ||= Configuration.new yield(configuration) end class Configuration attr_accessor :mailer_sender def initialize @mailer_sender = '[email protected]' end end end We have a configure class method that stores a Configuration object inside the Clearance module. Anything app...
Full article:
http://robots.thoughtbot.com/post/344833329/mygem-configu...