Skip to content

Instantly share code, notes, and snippets.

@hmans
Last active August 29, 2015 14:03
Show Gist options
  • Save hmans/cd5e2e989ceff7ff163a to your computer and use it in GitHub Desktop.
Save hmans/cd5e2e989ceff7ff163a to your computer and use it in GitHub Desktop.
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