Created
September 10, 2009 07:40
-
-
Save gumayunov/184397 to your computer and use it in GitHub Desktop.
DSL block example
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
#lib | |
class DSLAble | |
def with_dsl(&block) | |
if block.arity == 1 | |
block.call(self) | |
else | |
instance_eval(&block) | |
end | |
end | |
end | |
#helper | |
class MenuBuilder | |
include DSLAble | |
def build(&block) | |
with_dsl(block) | |
end | |
def item(html = nil, &lazy) | |
@items << (html || lazy || "") | |
end | |
def close | |
end | |
end | |
def menu(&block) | |
MenuBuilder.build block | |
end | |
# haml view | |
= menu do | |
- item { link_to ""} | |
- item link_to | |
- close "Cancel" | |
- item menu do |m| | |
- m.item link_to "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment