Class: Flame::Router

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Router

Returns a new instance of Router.

Parameters:



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

#appObject (readonly)

Returns the value of attribute app.



21
22
23
# File 'lib/flame/router.rb', line 21

def app
  @app
end

#reverse_routesObject (readonly)

Returns the value of attribute reverse_routes.



21
22
23
# File 'lib/flame/router.rb', line 21

def reverse_routes
  @reverse_routes
end

#routesObject (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

Parameters:



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

Parameters:

Returns:

  • (Flame::Route, nil)

    return the found nearest route or nil



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

Parameters:

Returns:

  • (Flame::Path)

    mounted path to action of controller



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