Skip to content

Instantly share code, notes, and snippets.

@bdimcheff
Created October 18, 2009 01:32
Show Gist options
  • Save bdimcheff/212509 to your computer and use it in GitHub Desktop.
Save bdimcheff/212509 to your computer and use it in GitHub Desktop.
module Router
def self.included(base) # :nodoc:
base.extend ClassMethods
end
module ClassMethods
attr_reader :routes
def map(regex, options = {}, &block)
define_method "#{regex}", &block
unbound_method = instance_method("#{regex}")
block = lambda { unbound_method.bind(self).call }
@routes ||= []
@routes.push([regex, block]).last
end
end
def route!(email)
routes = self.class.routes
routes.each do |regex, block|
if regex =~ email
instance_eval(&block)
end
end
end
end
class Foo
include Router
def foo
puts "foo!"
end
end
Foo.map(/.*/) {
puts "this is the method body and I should be able to call foo"
foo
}
Foo.new.route!('')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment