Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Configuration Thingy
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
You can’t perform that action at this time.