Class: Flame::Router
- Inherits:
-
Object
- Object
- Flame::Router
- Extended by:
- Forwardable
- Defined in:
- lib/flame/router.rb,
lib/flame/router/route.rb,
lib/flame/router/routes.rb,
lib/flame/router/routes_refine.rb,
lib/flame/router/controller_finder.rb,
lib/flame/router/routes_refine/mounting.rb
Overview
Comment due to private_constant
Defined Under Namespace
Classes: Route, Routes, RoutesRefine
Constant Summary collapse
- HTTP_METHODS =
i[GET POST PUT PATCH DELETE].freeze
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#reverse_routes ⇒ Object
readonly
Returns the value of attribute reverse_routes.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add(routes_refine) ⇒ Object
Add RoutesRefine to Router.
-
#find_nearest_route(path) ⇒ Flame::Route?
Find the nearest route by path.
-
#initialize(app) ⇒ Router
constructor
A new instance of Router.
-
#path_of(route_or_controller, action = nil) ⇒ Flame::Path
Find the path of route.
Constructor Details
#initialize(app) ⇒ Router
Returns a new instance of Router.
26 27 28 29 30 |
# File 'lib/flame/router.rb', line 26 def initialize(app) @app = app @routes = Flame::Router::Routes.new @reverse_routes = {} end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
21 22 23 |
# File 'lib/flame/router.rb', line 21 def app @app end |
#reverse_routes ⇒ Object (readonly)
Returns the value of attribute reverse_routes.
21 22 23 |
# File 'lib/flame/router.rb', line 21 def reverse_routes @reverse_routes end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
21 22 23 |
# File 'lib/flame/router.rb', line 21 def routes @routes end |
Instance Method Details
#add(routes_refine) ⇒ Object
Add RoutesRefine to Router
36 37 38 39 |
# File 'lib/flame/router.rb', line 36 def add(routes_refine) routes.deep_merge! routes_refine.routes reverse_routes.merge! routes_refine.reverse_routes end |
#find_nearest_route(path) ⇒ Flame::Route?
Find the nearest route by path
44 45 46 47 48 49 50 |
# File 'lib/flame/router.rb', line 44 def find_nearest_route(path) path_parts = path.parts.dup loop do route = routes.navigate(*path_parts)&.first_route break route if route || path_parts.pop.nil? end end |
#path_of(route_or_controller, action = nil) ⇒ Flame::Path
Find the path of route
57 58 59 60 61 62 63 64 65 66 |
# File 'lib/flame/router.rb', line 57 def path_of(route_or_controller, action = nil) if route_or_controller.is_a?(Flame::Router::Route) route = route_or_controller controller = route.controller action = route.action else controller = route_or_controller end reverse_routes.dig(controller.to_s, action) end |