Created
February 6, 2013 23:59
-
-
Save acook/4727078 to your computer and use it in GitHub Desktop.
Extensible configuration objects from YAML
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 ConfigModule | |
def [] key | |
config.send key | |
end | |
def config | |
@config ||= load_config | |
end | |
def config_file file | |
@config_file = file | |
end | |
def namespace name | |
@namespace = name | |
end | |
def load_config | |
file = OpenStruct.new YAML.load_file(@config_file) | |
@namespace ? file[@namespace] : file | |
end | |
def method_missing name, *args, &block | |
config.send name, *args, &block | |
end | |
end |
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 ExampleConfig | |
extend ConfigModule | |
config_file './config/example.yml' | |
namespace Rails.env | |
def kanoodle | |
'ka' + config.noodle | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment