class Odex::Router

Router

Attributes

c[R]
fallback[R]
hook_after[R]
hook_before[R]
od_jr[R]

Public Class Methods

new(options) click to toggle source
# File lib/odex/app.rb, line 64
def initialize(options)
  @c    = options[:c]
  @hook_before   = options[:hook_before]
  @hook_after    = options[:hook_after]
  @fallback       = options[:fallback]

  prepare_for_od_jr(options[:route_defs])
end

Public Instance Methods

call(env) click to toggle source
# File lib/odex/app.rb, line 73
def call(env)
  response = od_jr.call(env)

  if response[0] == 40 and
    fallback.call(env)
  else
    response
  end
end
compile(handler) click to toggle source
# File lib/odex/app.rb, line 99
def compile(handler)
  Proc.new do |env|
    scope = c.new(env)

    response = catch (:halt) do
      hook_before.each {|h| scope.instance_eval &h }
      scope.apply_to &handler
    end

    catch (:halt) do
      hook_after.each {|h| scope.instance_eval &h }
    end

    response.finish
  end
end
prepare_for_od_jr(route_defs) click to toggle source
# File lib/odex/app.rb, line 83
def prepare_for_od_jr(route_defs)
  routes = Odex::OdexRequest.new
  @od_jr = Odex::OdexRouter.new(routes, {
    :parameters_key => 'odex.params',
    :request_class => Odex::Request
  })

  route_defs.each do |path, options, handler|
    pat         = Odex::OdexPattern.new(path)
    constraints = options.fetch(:constraints, {})
    defaults    = options.fetch(:defaults, {})

    @od_jr.routes.add_route compile(handler), pat, constraints, defaults
  end
end