-
-
Save dannytip/298359 to your computer and use it in GitHub Desktop.
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 "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