Class: Flame::Router::Route

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

Overview

Class for Route in Router.routes

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, action, method, path) ⇒ Route

Returns a new instance of Route.



12
13
14
15
16
17
18
19
20
21
# File 'lib/flame/route.rb', line 12

def initialize(controller, action, method, path)
	@controller = controller
	@action = action
	@method = method.to_sym.upcase
	## MAKE PATH
	@path = path
	Validators::ArgumentsValidator.new(@controller, @path, @action).valid?
	@path_parts = @path.to_s.split('/').reject(&:empty?)
	freeze
end

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



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

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



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

def controller
  @controller
end

#methodObject (readonly)

Returns the value of attribute method.



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

def method
  @method
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#path_partsObject (readonly)

Returns the value of attribute path_parts.



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

def path_parts
  @path_parts
end

Class Method Details

.path_merge(*parts) ⇒ Object



59
60
61
# File 'lib/flame/route.rb', line 59

def self.path_merge(*parts)
	parts.join('/').gsub(%r{\/{2,}}, '/')
end

Instance Method Details

#arguments(request_parts) ⇒ Object

Extract arguments from request_parts for ‘execute`

Parameters:

  • request_parts (Array)

    parts of the request (Array of String)



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/flame/route.rb', line 47

def arguments(request_parts)
	@path_parts.each_with_index.with_object({}) do |(path_part, i), args|
		request_part = request_parts[i]
		path_part_opt = path_part[1] == ARG_CHAR_OPT
		next args unless path_part[0] == ARG_CHAR
		break args if path_part_opt && request_part.nil?
		args[
			path_part[(path_part_opt ? 2 : 1)..-1].to_sym
		] = URI.decode(request_part)
	end
end

#assign_arguments(args = {}) ⇒ Object

Assign arguments to path for ‘Controller.path_to`

Parameters:

  • args (Hash) (defaults to: {})

    arguments for assigning



40
41
42
43
# File 'lib/flame/route.rb', line 40

def assign_arguments(args = {})
	parts = @path_parts.map { |part| assign_argument(part, args) }
	self.class.path_merge(parts.unshift(''))
end

#compare_attributes(attrs) ⇒ Object

Compare attributes for ‘Router.find_route`

Parameters:

  • attrs (Hash)

    Hash of attributes for comparing



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

def compare_attributes(attrs)
	attrs.each do |name, value|
		next true if compare_attribute(name, value)
		break false
	end
end

#freezeObject



23
24
25
26
27
# File 'lib/flame/route.rb', line 23

def freeze
	@path.freeze
	@path_parts.freeze
	super
end