Class: Hobby::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/hobby/router.rb,
lib/hobby/router/route.rb,
lib/hobby/router/routes.rb,
lib/hobby/router/builder.rb

Defined Under Namespace

Classes: Builder, Route, Routes

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRouter

Returns a new instance of Router.



3
4
5
6
# File 'lib/hobby/router.rb', line 3

def initialize
  @routes = Routes.new
  @uses, @maps = [], []
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



34
35
36
# File 'lib/hobby/router.rb', line 34

def app
  @app
end

Instance Method Details

#add_route(verb, path, &action) ⇒ Object



12
13
14
15
16
17
18
19
# 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) ⇒ Object



8
9
10
# File 'lib/hobby/router.rb', line 8

def initialize_copy _router
  @uses, @maps = @uses.dup, @maps.dup
end

#map(path, app = nil, &block) ⇒ Object



30
31
32
# File 'lib/hobby/router.rb', line 30

def map path, app = nil, &block
  @maps << Builder::Map.new(path, app, &block)
end

#route_for(env) ⇒ Object



21
22
23
24
# 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_appObject



36
37
38
39
40
# File 'lib/hobby/router.rb', line 36

def to_rack_app
  builder = create_builder
  builder.run app
  builder.to_app
end

#use(*all) ⇒ Object



26
27
28
# File 'lib/hobby/router.rb', line 26

def use *all
  @uses << all
end