Created
January 18, 2011 09:33
-
-
Save ivanvanderbyl/784200 to your computer and use it in GitHub Desktop.
Metaprogramming my DSLs
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
class SandwichMaker | |
end | |
class SandwichEater | |
end | |
module Cloudist | |
class << self | |
@@workers = {} | |
def handle(queue_name) | |
@@workers[queue_name.to_s] ||= [] | |
class << queue_name | |
def with(handler) | |
(@@workers[self.to_s] << handler).uniq! | |
end | |
end | |
queue_name | |
end | |
def use(handler) | |
proxy = handler.new | |
class << proxy | |
def to(queue_name) | |
((@@workers[queue_name.to_s] ||= []) << self.class).uniq! | |
end | |
end | |
proxy | |
end | |
def workers | |
@@workers | |
end | |
end | |
end | |
Cloudist.handle('make.sandwich').with(SandwichMaker) | |
Cloudist.use(SandwichEater).to('eat.sandwich') | |
p Cloudist.workers | |
# >> {"make.sandwich"=>[SandwichMaker], "eat.sandwich"=>[SandwichEater]} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment