Class: Flame::Route

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

Overview

Class for Route in Router.routes

Defined Under Namespace

Classes: Executable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Route

Returns a new instance of Route.



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

def initialize(attrs = {})
	@attributes = attrs.merge(
		path_parts: attrs[:path].to_s.split('/').reject(&:empty?)
	)
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



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

def attributes
  @attributes
end

Instance Method Details

#[](key) ⇒ Object



12
13
14
# File 'lib/flame/route.rb', line 12

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

#[]=(key, value) ⇒ Object



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

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

#arguments(request_parts) ⇒ Object

Extract arguments from request_parts for ‘execute`



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

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`



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/flame/route.rb', line 84

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`



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

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`



54
55
56
57
58
59
60
61
62
63
# File 'lib/flame/route.rb', line 54

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`



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

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



65
66
67
# File 'lib/flame/route.rb', line 65

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

#compare_path_parts(request_parts) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/flame/route.rb', line 69

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

#executableObject

Create Executable object (route)



21
22
23
# File 'lib/flame/route.rb', line 21

def executable
	Executable.new(self)
end