Class: Flame::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/flame/router.rb,
lib/flame/router/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.



10
11
12
13
# File 'lib/flame/router.rb', line 10

def initialize(app)
	@app = app
	@routes = []
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#routesObject (readonly)

Returns the value of attribute routes.



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

def routes
  @routes
end

Instance Method Details

#add_controller(ctrl, path = 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, nil) (defaults to: nil)

    root path for controller’s methods

Yields:

  • block for routes refine



19
20
21
22
23
24
25
# File 'lib/flame/router.rb', line 19

def add_controller(ctrl, path = nil, &block)
	## @todo Add Regexp paths

	## Add routes from controller to glob array
	route_refine = RouteRefine.new(self, ctrl, path, block)
	concat_routes(route_refine)
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`



38
39
40
41
42
43
44
45
46
47
# File 'lib/flame/router.rb', line 38

def find_nearest_route(path)
	path = Flame::Path.new(path) if path.is_a? String
	path_parts = path.parts.dup
	while path_parts.size >= 0
		route = find_route path: Flame::Path.new(*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`



30
31
32
33
# File 'lib/flame/router.rb', line 30

def find_route(attrs)
	route = routes.find { |r| r.compare_attributes(attrs) }
	route.dup if route
end