Created
November 15, 2013 03:02
-
-
Save sstelfox/7478430 to your computer and use it in GitHub Desktop.
Simple DSL pattern for configuration
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 'singleton' | |
Config = Struct.new(:a, :b) | |
class Config | |
include Singleton | |
def self.[](arg) | |
instance.public_send(arg.to_sym) | |
end | |
def self.config | |
yield instance | |
end | |
end | |
Config.config do |c| | |
c.a = "something" | |
c.b = "else" | |
end | |
puts Config[:a] | |
puts Config["b"] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment