Created
March 13, 2011 04:52
-
-
Save ahastudio/867876 to your computer and use it in GitHub Desktop.
#PNA2011 Ruby DSL example
This file contains hidden or 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
def application(name, &block) | |
$app = Application.new | |
$app.name = name | |
$app.instance_eval(&block) | |
end | |
class Application | |
attr_accessor :name | |
def initialize | |
@responders = {} | |
end | |
def open(path) | |
responder = @responders[path] | |
if responder | |
responder.handle | |
else | |
puts 'no respond' | |
end | |
end | |
def respond(path, &block) | |
@responders[path] = Responder.new(&block) | |
end | |
end | |
class Responder | |
def initialize(&block) | |
@block = block | |
end | |
def handle | |
instance_eval(&@block) | |
end | |
def render(words) | |
puts words | |
end | |
end | |
# ---- | |
application 'Test' do | |
respond '/' do | |
render 'ok!' | |
end | |
respond '/test' do | |
render 'welcome' | |
end | |
end | |
# ---- | |
$app.open '/' | |
$app.open '/test' | |
$app.open '/404' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment