Class: Flame::Router
- Inherits:
-
Object
- Object
- Flame::Router
- Defined in:
- lib/flame/router.rb,
lib/flame/route.rb
Overview
Router class for routing
Defined Under Namespace
Classes: Route, RouteRefine
Constant Summary collapse
- ARG_CHAR =
':'.freeze
- ARG_CHAR_OPT =
'?'.freeze
Instance Attribute Summary collapse
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#routes ⇒ Object
readonly
Returns the value of attribute routes.
Instance Method Summary collapse
-
#add_controller(ctrl, path, block = nil) ⇒ Object
Add the controller with it’s methods to routes.
-
#find_nearest_route(path_parts) ⇒ Flame::Route?
Find the nearest route by path parts.
-
#find_route(attrs) ⇒ Flame::Route?
Find route by any attributes.
-
#initialize(app) ⇒ Router
constructor
A new instance of Router.
Constructor Details
#initialize(app) ⇒ Router
Returns a new instance of Router.
9 10 11 12 |
# File 'lib/flame/router.rb', line 9 def initialize(app) @app = app @routes = [] end |
Instance Attribute Details
#app ⇒ Object (readonly)
Returns the value of attribute app.
7 8 9 |
# File 'lib/flame/router.rb', line 7 def app @app end |
#routes ⇒ Object (readonly)
Returns the value of attribute routes.
7 8 9 |
# File 'lib/flame/router.rb', line 7 def routes @routes end |
Instance Method Details
#add_controller(ctrl, path, block = nil) ⇒ Object
Add the controller with it’s methods to routes
18 19 20 21 22 23 24 25 26 |
# File 'lib/flame/router.rb', line 18 def add_controller(ctrl, path, block = nil) ## @todo Add Regexp paths ## Add routes from controller to glob array route_refine = RouteRefine.new(self, ctrl, path, block) if Validators::ActionsValidator.new(route_refine).valid? concat_routes(route_refine) end end |
#find_nearest_route(path_parts) ⇒ Flame::Route?
Find the nearest route by path parts
39 40 41 42 43 44 45 46 |
# File 'lib/flame/router.rb', line 39 def find_nearest_route(path_parts) while path_parts.size >= 0 route = find_route(path_parts: path_parts) break if route || path_parts.empty? path_parts.pop end route end |
#find_route(attrs) ⇒ Flame::Route?
Find route by any attributes
31 32 33 34 |
# File 'lib/flame/router.rb', line 31 def find_route(attrs) route = routes.find { |r| r.compare_attributes(attrs) } route.dup if route end |