Created
February 15, 2011 11:11
-
-
Save youngbrioche/827406 to your computer and use it in GitHub Desktop.
Small configuration DSL
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 Foo | |
module Configuration | |
require 'ostruct' | |
extend ActiveSupport::Concern | |
included do | |
# | |
end | |
module ClassMethods | |
def method_missing(method_name, *args) | |
@config ||= OpenStruct.new | |
if method_name.to_s.match(/^.+=$/) | |
@config.send(method_name, args.first) | |
else | |
@config.send(method_name) | |
end | |
end | |
def configure | |
yield self | |
end | |
end | |
end | |
end | |
# Usage | |
module App | |
include Foo::Configuration | |
end | |
App.configure do |config| | |
config.test_value = :test | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment