Class: Router
Instance Attribute Summary collapse
-
#routes ⇒ Object
Returns the value of attribute routes.
Instance Method Summary collapse
- #add(regexp, args, &block) ⇒ Object
- #handle(path, request) ⇒ Object
-
#initialize(request_handler) ⇒ Router
constructor
A new instance of Router.
-
#route(regexp, parser, *args, &block) ⇒ Object
route %r(/foo/(.+)/(w+)/(d+)), AnyParser, :matches, :in, :order.
Methods included from FromFile
Constructor Details
#initialize(request_handler) ⇒ Router
Returns a new instance of Router.
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/liquid/router.rb', line 10 def initialize(request_handler) @request_handler = request_handler @routes = [] @cache = Hash.new do |hash, path| hash[path] = nil @routes.each do |regex, block, args| match = path.match(regex) hash[path] = block.curry[match] if match end hash[path] end end |
Instance Attribute Details
#routes ⇒ Object
Returns the value of attribute routes.
8 9 10 |
# File 'lib/liquid/router.rb', line 8 def routes @routes end |
Instance Method Details
#add(regexp, args, &block) ⇒ Object
24 25 26 |
# File 'lib/liquid/router.rb', line 24 def add(regexp, args, &block) @routes << [regexp, block, args] end |
#handle(path, request) ⇒ Object
42 43 44 |
# File 'lib/liquid/router.rb', line 42 def handle(path, request) @cache[path].call(request) if @cache[path] end |
#route(regexp, parser, *args, &block) ⇒ Object
route %r(/foo/(.+)/(w+)/(d+)), AnyParser, :matches, :in, :order
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/liquid/router.rb', line 29 def route(regexp, parser, *args, &block) block = lambda do |match, env| params = args.each_with_index.inject({}) do |hash, (name, index)| hash[name] = match[index+1] hash end return @request_handler.handle(parser, env, params) end add(regexp, args, &block) end |