Created
October 18, 2009 01:32
-
-
Save bdimcheff/212509 to your computer and use it in GitHub Desktop.
This file contains 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
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