Created
September 22, 2011 20:36
-
-
Save happyrobots/1235965 to your computer and use it in GitHub Desktop.
Configuration Patterns
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module MyCoolModule | |
class Configuration | |
attr_accessor :my_value | |
def initialize | |
# set defaults | |
end | |
end | |
def self.configure | |
@@config = MyCoolModule::Configuration.new | |
yield(@@config) | |
end | |
def self.initialize! | |
@@config.do_something if @@config | |
end | |
end | |
MyCoolModule.configure do |config| | |
# override defaults | |
end | |
MyCoolModule.initialize! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'active_support/ordered_options' | |
module MyCoolModule | |
def self.configure | |
@@config = ActiveSupport::OrderedOptions.new | |
yield(@@config) | |
end | |
def self.initialize! | |
do_something(@@config) | |
end | |
end | |
MyCoolModule.configure do |config| | |
# Define anything | |
end | |
MyCoolModule.initialize! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment