Class: Kawaii::RouteContext

Inherits:
Object
  • Object
show all
Includes:
MethodChain, RoutingMethods
Defined in:
lib/kawaii/route_context.rb

Overview

Implementation of nested routes generated via Router#context.

Examples:

A simple context

context '/foo' do
  get '/bar' do
  end
end

# It is a rough equivalent of:

ctx = RouteContext.new('/foo')
ctx.get '/bar' do
end

Instance Attribute Summary

Attributes included from MethodChain

#parent_scope

Instance Method Summary (collapse)

Methods included from MethodChain

#method_missing, #respond_to?

Methods included from RoutingMethods

add_http_method, #context, #delete, #get, #head, #link, #methods_used, #options, #patch, #post, #put, #route, #trace, #unlink

Constructor Details

- (RouteContext) initialize(scope, path)

Create a Kawaii::RouteContext object. be consumed by Matcher.compile

Parameters:

  • path (String, Regexp, Matcher)

    any path specification which can



24
25
26
27
28
# File 'lib/kawaii/route_context.rb', line 24

def initialize(scope, path)
  self.parent_scope = scope
  super()
  @matcher = Matcher.compile(path, starts_with: true)
end

Dynamic Method Handling

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

Instance Method Details

- (Route) match(env)

Tries to match the context against a Rack environment. no match found.

Parameters:

  • env (Hash)

    Rack environment

Returns:

  • (Route)

    matching route defined inside the context. Can be nil if



34
35
36
37
# File 'lib/kawaii/route_context.rb', line 34

def match(env)
  m = @matcher.match(env[Rack::PATH_INFO])
  super(env.merge(Rack::PATH_INFO => ensure_slash(m.remaining_path))) if m
end