Class: Hobby::Router
- Inherits:
-
Object
- Object
- Hobby::Router
- Defined in:
- lib/hobby/router.rb,
lib/hobby/router/route.rb,
lib/hobby/router/routes.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#app ⇒ Object
Returns the value of attribute app.
Instance Method Summary collapse
- #add_route(verb, path, &action) ⇒ Object
-
#initialize ⇒ Router
constructor
A new instance of Router.
- #initialize_copy(_router) ⇒ Object
- #map(path, app) ⇒ Object
- #route_for(env) ⇒ Object
- #to_rack_app ⇒ Object
- #use(*all) ⇒ Object
Constructor Details
#initialize ⇒ Router
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
#app ⇒ Object
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) ⇒ Object
30 31 32 |
# File 'lib/hobby/router.rb', line 30 def map path, app @maps << [path, app] 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_app ⇒ Object
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 |