Class: Flame::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.



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

#[](attribute) ⇒ Object



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

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

#arguments(request_parts) ⇒ Object

Extract arguments from request_parts for ‘execute`



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/flame/route.rb', line 37

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

#arranged_params(params) ⇒ Object

Arguments in order as parameters of method of controller



50
51
52
53
54
55
# File 'lib/flame/route.rb', line 50

def arranged_params(params)
	self[:controller].instance_method(self[:action]).parameters
	  .each_with_object([]) do |par, arr|
		  arr << params[par[1]] if par[0] == :req || params[par[1]]
	  end
end

#assign_arguments(args = {}) ⇒ Object

Assign arguments to path for ‘Controller.path_to`



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

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

#compare_attributes(attrs) ⇒ Object

Compare attributes for ‘Router.find_route`



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

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

#merge(attrs) ⇒ Object



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

def merge(attrs)
	dup.attributes.merge!(attrs)
	self
end