Class: Kawaii::Route

Inherits:
Object
  • Object
show all
Includes:
MethodChain
Defined in:
lib/kawaii/route.rb

Overview

Matching and resolution for a single route.

Instance Attribute Summary

Attributes included from MethodChain

#parent_scope

Instance Method Summary (collapse)

Methods included from MethodChain

#method_missing, #respond_to?

Constructor Details

- (Route) initialize(scope, path, &block)

Create a Kawaii::Route object. consumed by Matcher.compile

Parameters:

  • path (String, Regexp, Matcher)

    any path specification which can be

  • block (Proc)

    route handler



10
11
12
13
14
# File 'lib/kawaii/route.rb', line 10

def initialize(scope, path, &block)
  self.parent_scope = scope
  @matcher = Matcher.compile(path, full_match: true)
  @block = block
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Kawaii::MethodChain

Instance Method Details

- (RouteHandler) match(env)

Tries to match the route against a Rack environment. the route's handler block in on Kawaii::RouteHandler#call. Can be nil if no match found.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (RouteHandler)

    a Rack application creating environment to run



21
22
23
24
25
# File 'lib/kawaii/route.rb', line 21

def match(env)
  match = @matcher.match(env[Rack::PATH_INFO])
  # puts "Route#match #{@matcher} #{env[Rack::PATH_INFO]} #{match.inspect}"
  RouteHandler.new(self, match.params, &@block) if match
end