Skip to content

Instantly share code, notes, and snippets.

@gumayunov
Created September 10, 2009 07:40
Show Gist options
  • Save gumayunov/184397 to your computer and use it in GitHub Desktop.
Save gumayunov/184397 to your computer and use it in GitHub Desktop.
DSL block example
#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