Class: Flame::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/router.rb,
lib/flame/route.rb

Overview

Router class for routing

Defined Under Namespace

Classes: Route, RouteRefine

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#appObject (readonly)

Returns the value of attribute app.



7
8
9
# File 'lib/flame/router.rb', line 7

def app
  @app
end

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

Parameters:

  • ctrl (Flame::Controller)

    class of the controller which will be added

  • path (String)

    root path for controller’s methods

  • block (Proc, nil) (defaults to: nil)

    block for routes refine



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

Parameters:

  • path_parts (Array)

    parts of path for route finding

Returns:

  • (Flame::Route, nil)

    return the found nearest route, otherwise ‘nil`



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

Parameters:

  • attrs (Hash)

    attributes for comparing

Returns:

  • (Flame::Route, nil)

    return the found route, otherwise ‘nil`



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