Class: Flame::Router

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

Overview

Router class for routing

Defined Under Namespace

Classes: RouteRefine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize ⇒ Router

Returns a new instance of Router.



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

def initialize
	@routes = []
	@befores = {}
end

Instance Attribute Details

#befores ⇒ Object

Returns the value of attribute befores.



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

def befores
  @befores
end

#routes ⇒ Object

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



14
15
16
17
18
19
20
21
22
23
# File 'lib/flame/router.rb', line 14

def add_controller(ctrl, path, block = nil)
	## TODO: Add Regexp paths
	## TODO: Add `before` and `after` methods

	## Add routes from controller to glob array
	ctrl_routes = RouteRefine.new(ctrl, path, block)
	ActionsValidator.new(ctrl_routes.routes, ctrl).valid?
	routes.concat(ctrl_routes.routes)
	befores[ctrl] = ctrl_routes.befores
end

#find_befores(route) ⇒ Object

Find before hook by Route



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

def find_befores(route)
	(befores[route[:controller]][:*] || []) +
	  (befores[route[:controller]][route[:action]] || [])
end

#find_route(attrs) ⇒ Object

Find route by any attributes



26
27
28
# File 'lib/flame/router.rb', line 26

def find_route(attrs)
	routes.find { |route| route.compare_attributes(attrs) }
end