Created
April 7, 2014 08:00
-
-
Save tempredirect/10016361 to your computer and use it in GitHub Desktop.
HashBuilder
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 HashBuilder | |
def initialize | |
@h = Hash.new | |
end | |
def set(key, value = nil, &block) | |
@h[key.to_s] = if block_given? | |
HashBuilder.build &block | |
else | |
value | |
end | |
end | |
def self.build(&block) | |
builder = HashBuilder.new | |
if block_given? | |
builder.instance_eval &block | |
end | |
@h | |
end | |
def method_missing(symbol, *args) | |
set(symbol, *args) | |
end | |
end | |
hb = HashBuilder.new | |
r = hb.build { | |
wibble 42 | |
wobble 43 | |
wiggle { | |
set "a", 1 | |
set "b", 2 | |
} | |
banana 25 | |
set "big-thing", 42 | |
} | |
puts r.inspect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
think we can get to:
default "xmlapi" do
foo "bar"
end
shared_service "blah" do
baz "cow"
end