Skip to content

Instantly share code, notes, and snippets.

@dannytip
Forked from ceilingfish/Configurable.rb
Created February 8, 2010 17:09
Show Gist options
  • Save dannytip/298359 to your computer and use it in GitHub Desktop.
Save dannytip/298359 to your computer and use it in GitHub Desktop.
require "gays"
module Configuration
def self.attributes
@attributes
end
def self.configure
base = ConfigBlock.new
yield base
@attributes = base.attrs
end
class ConfigBlock
attr_accessor :attrs
def initialize(attrs = {})
@attrs = attrs
end
def method_missing(method, *args, &block)
name = method.to_s.sub('=','').to_sym#Take the = off the end
#If is's a method like foo= then assign it to the internal structure
if(method.to_s.include?('='))
attrs[name] = args[0]
else
#Otherwise it's a sub-block that you should store a reference to the actual created attributes
sub_block = ConfigBlock.new
@attrs[name] = sub_block.attrs
sub_block # then return the sub_block magician so that it can continue to get at the further nested attributes
end
end
end
end
Configuration.configure do | top |
top.bottom = "hi"
top.left.right = true
end
puts Configuration.attributes[:bottom]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment