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

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Route

Returns a new instance of Route.



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

def initialize(attrs = {})
	@attributes = attrs.merge(
		## Split path to parts (Array of String)
		path_parts: attrs[:path].to_s.split('/').reject(&:empty?).freeze
	)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



5
6
7
# File 'lib/flame/route.rb', line 5

def attributes
  @attributes
end

Instance Method Details

#[](key) ⇒ Object

Get the attribute of route

Parameters:

  • key (Symbol)

    name of attribute



16
17
18
# File 'lib/flame/route.rb', line 16

def [](key)
	@attributes[key]
end

#[]=(key, value) ⇒ Object

Set the attribute of route

Parameters:

  • key (Symbol)

    name of attribute

  • value (Object)

    value of attribute



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

def []=(key, value)
	@attributes[key] = value
end

#arguments(request_parts) ⇒ Object

Extract arguments from request_parts for ‘execute`

Parameters:

  • request_parts (Array)

    parts of the request (Array of String)



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

def arguments(request_parts)
	self[:path_parts].each_with_index.with_object({}) do |(path_part, i), args|
		request_part = request_parts[i]
		path_part_opt = path_part[1] == '?'
		next args unless path_part[0] == ':'
		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_argument(path_part, args = {}) ⇒ Object

Helpers for ‘assign_arguments`



89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/flame/route.rb', line 89

def assign_argument(path_part, args = {})
	## Not argument
	return path_part unless path_part[0] == ':'
	## Not required argument
	return args[path_part[2..-1].to_sym] if path_part[1] == '?'
	## Required argument
	param = args[path_part[1..-1].to_sym]
	## Required argument is nil
	fail ArgumentNotAssignedError.new(self[:path], path_part) if param.nil?
	## All is ok
	param
end

#assign_arguments(args = {}) ⇒ Object

Assign arguments to path for ‘Controller.path_to`

Parameters:

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

    arguments for assigning



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

def assign_arguments(args = {})
	self[:path_parts]
	  .map { |path_part| assign_argument(path_part, args) }
	  .unshift('').join('/').gsub(%r{\/{2,}}, '/')
end

#compare_attribute(name, value) ⇒ Object

Helpers for ‘compare_attributes`



59
60
61
62
63
64
65
66
67
68
# File 'lib/flame/route.rb', line 59

def compare_attribute(name, value)
	case name
	when :method
		compare_method(value)
	when :path_parts
		compare_path_parts(value)
	else
		self[name] == value
	end
end

#compare_attributes(attrs) ⇒ Object

Compare attributes for ‘Router.find_route`

Parameters:

  • attrs (Hash)

    Hash of attributes for comparing



29
30
31
32
33
34
# File 'lib/flame/route.rb', line 29

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

#compare_method(request_method) ⇒ Object



70
71
72
# File 'lib/flame/route.rb', line 70

def compare_method(request_method)
	self[:method].upcase.to_sym == request_method.upcase.to_sym
end

#compare_path_parts(request_parts) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/flame/route.rb', line 74

def compare_path_parts(request_parts)
	# p route_path
	req_path_parts = self[:path_parts].select { |part| part[1] != '?' }
	return false if request_parts.count < req_path_parts.count
	# compare_parts(request_parts, self[:path_parts])
	request_parts.each_with_index do |request_part, i|
		path_part = self[:path_parts][i]
		# p request_part, path_part
		break false unless path_part
		next if path_part[0] == ':'
		break false unless request_part == path_part
	end
end