Last active
August 29, 2015 14:03
-
-
Save hmans/cd5e2e989ceff7ff163a to your computer and use it in GitHub Desktop.
Ruby Configuration Thingy
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
class ConfigurationProxy | |
attr_reader :data | |
def initialize | |
@data = {} | |
end | |
def method_missing(method, *args, &blk) | |
if args.any? || block_given? | |
if block_given? | |
@data[method] = self.class.parse(&blk) | |
else | |
@data[method] = args | |
end | |
else | |
@data[method] | |
end | |
end | |
class << self | |
def parse(&blk) | |
new.tap do |instance| | |
blk.call(instance) | |
end | |
end | |
end | |
end | |
module Pants | |
def Pants.config | |
@config | |
end | |
def Pants.configure(&blk) | |
@config = ConfigurationProxy.parse(&blk) | |
end | |
end | |
Pants.configure do |config| | |
config.foo 'bar' | |
config.moo 'baz' | |
config.servers do |servers| | |
servers.a_server 'http://hmans.de' | |
end | |
end | |
puts Pants.config.foo | |
puts Pants.config.servers.a_server |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment