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(app) ⇒ Router

Returns a new instance of Router.



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

def initialize(app)
	@app = app
	@routes = []
	@befores, @afters = Array.new(2) { {} }
end

Instance Attribute Details

#aftersObject (readonly)

Returns the value of attribute afters.



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

def afters
  @afters
end

#appObject (readonly)

Returns the value of attribute app.



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

def app
  @app
end

#beforesObject (readonly)

Returns the value of attribute befores.



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

def befores
  @befores
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



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

def add_controller(ctrl, path, block = nil)
	## TODO: Add Regexp paths

	## Add routes from controller to glob array
	ctrl.include(*@app.helpers)
	route_refine = RouteRefine.new(self, ctrl, path, block)
	concat_routes(route_refine) if ActionsValidator.new(route_refine).valid?
end

#find_afters(route) ⇒ Object

Find after hook by Route



41
42
43
44
# File 'lib/flame/router.rb', line 41

def find_afters(route)
	(afters[route[:controller]][:*] || []) +
	  (afters[route[:controller]][route[:action]] || [])
end

#find_befores(route) ⇒ Object

Find before hook by Route



35
36
37
38
# File 'lib/flame/router.rb', line 35

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

#find_route(attrs, with_hooks = true) ⇒ Object

Find route by any attributes



25
26
27
28
29
30
31
32
# File 'lib/flame/router.rb', line 25

def find_route(attrs, with_hooks = true)
	route = routes.find { |r| r.compare_attributes(attrs) }
	return route unless route && with_hooks
	route.merge(
		befores: find_befores(route),
		afters: find_afters(route)
	)
end