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 = []
	@hooks = {}
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

#hooksObject (readonly)

Returns the value of attribute hooks.



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

def hooks
  @hooks
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_hooks(route) ⇒ Object

Find hooks by Route



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/flame/router.rb', line 41

def find_hooks(route)
	result = {}
	hooks[route[:controller]].each do |type, hash|
		if type == :error
			result[type] = hash
		else
			result[type] = (hash[route[:action]] || []) | (hash[:*] || [])
		end
	end
	# p result
	result
end

#find_nearest_route(path_parts) ⇒ Object

Find the nearest route by path parts



31
32
33
34
35
36
37
38
# File 'lib/flame/router.rb', line 31

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) ⇒ Object

Find route by any attributes



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

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