Skip to content

Instantly share code, notes, and snippets.

@ivanvanderbyl
Created January 18, 2011 09:33
Show Gist options
  • Save ivanvanderbyl/784200 to your computer and use it in GitHub Desktop.
Save ivanvanderbyl/784200 to your computer and use it in GitHub Desktop.
Metaprogramming my DSLs
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