class Hobby::Router

Attributes

app[RW]

Public Class Methods

new() click to toggle source
# File lib/hobby/router.rb, line 3
def initialize
  @routes = Routes.new
  @uses, @maps = [], []
end

Public Instance Methods

add_route(verb, path, &action) click to toggle source
# File lib/hobby/router.rb, line 12
def add_route verb, path, &action
  route = Route.new verb, path, &action

  path = nil if path.eql? '/'
  @routes["#{verb}#{path}"] = route

  route
end
initialize_copy(_router) click to toggle source
# File lib/hobby/router.rb, line 8
def initialize_copy _router
  @uses, @maps = @uses.dup, @maps.dup
end
map(path, app) click to toggle source
# File lib/hobby/router.rb, line 30
def map path, app
  @maps << [path, app]
end
route_for(env) click to toggle source
# File lib/hobby/router.rb, line 21
def route_for env
  route, params = @routes["#{env['REQUEST_METHOD']}#{env['PATH_INFO']}"]
  params ? route.with_params(params) : route
end
to_rack_app() click to toggle source
# File lib/hobby/router.rb, line 36
def to_rack_app
  builder = create_builder
  builder.run app
  builder.to_app
end
use(*all) click to toggle source
# File lib/hobby/router.rb, line 26
def use *all
  @uses << all
end

Private Instance Methods

create_builder() click to toggle source
# File lib/hobby/router.rb, line 43
def create_builder
  builder = Rack::Builder.new

  @uses.each { |all| builder.use *all }
  @maps.each { |path, app|
    builder.map path do run app end
  }

  builder
end