Class: Flame::Path
- Inherits:
-
Object
- Object
- Flame::Path
- Extended by:
- Forwardable
- Includes:
- Memery
- Defined in:
- lib/flame/path.rb
Overview
Class for working with paths
Defined Under Namespace
Classes: Part
Class Method Summary collapse
-
.merge(*parts) ⇒ Flame::Path
Merge parts of path to one path.
Instance Method Summary collapse
-
#+(other) ⇒ Flame::Path
Create new instance from self and other by concatinating.
-
#<=>(other) ⇒ -1, ...
Compare by parts count and the first arg position.
-
#==(other) ⇒ true, false
Compare with other path by parts.
-
#adapt(ctrl, action) ⇒ Flame::Path
Complete path for the action of controller.
-
#assign_arguments(args = {}) ⇒ Object
Assign arguments to path for ‘Controller#path_to`.
-
#extract_arguments(other_path) ⇒ Hash{Symbol => String}
Extract arguments from other path with values at arguments.
-
#freeze ⇒ Object
Freeze all strings in object.
-
#initialize(*paths) ⇒ Path
constructor
Create a new instance.
-
#parts ⇒ Array<Flame::Path::Part>
Return parts of path, splitted by slash (
/). -
#to_routes_with_endpoint ⇒ Array(Flame::Router::Routes, Flame::Router::Routes)
Path parts as keys of nested Hashes.
-
#to_s ⇒ String
(also: #to_str)
Path as String.
Constructor Details
#initialize(*paths) ⇒ Path
Create a new instance
27 28 29 30 |
# File 'lib/flame/path.rb', line 27 def initialize(*paths) @path = self.class.merge(*paths) freeze end |
Class Method Details
.merge(*parts) ⇒ Flame::Path
Merge parts of path to one path
20 21 22 |
# File 'lib/flame/path.rb', line 20 def self.merge(*parts) parts.join('/').gsub(%r|/{2,}|, '/') end |
Instance Method Details
#+(other) ⇒ Flame::Path
Create new instance from self and other by concatinating
50 51 52 |
# File 'lib/flame/path.rb', line 50 def +(other) self.class.new(self, other) end |
#<=>(other) ⇒ -1, ...
Compare by parts count and the first arg position
57 58 59 60 61 62 63 |
# File 'lib/flame/path.rb', line 57 def <=>(other) self_parts, other_parts = [self, other].map(&:parts) by_parts_size = self_parts.size <=> other_parts.size return by_parts_size unless by_parts_size.zero? compare_by_args_in_parts self_parts.zip(other_parts) end |
#==(other) ⇒ true, false
Compare with other path by parts
68 69 70 71 |
# File 'lib/flame/path.rb', line 68 def ==(other) other = self.class.new(other) if other.is_a? String parts == other.parts end |
#adapt(ctrl, action) ⇒ Flame::Path
Add :arg:type support (:id:num, :name:str, etc.)
Complete path for the action of controller
78 79 80 81 82 83 84 85 86 |
# File 'lib/flame/path.rb', line 78 def adapt(ctrl, action) parameters = ctrl.instance_method(action).parameters parameters.map! do |parameter| parameter_type, parameter_name = parameter path_part = self.class::Part.new parameter_name, arg: parameter_type path_part unless parts.include? path_part end self.class.new @path.empty? ? "/#{action}" : self, *parameters.compact end |
#assign_arguments(args = {}) ⇒ Object
Assign arguments to path for ‘Controller#path_to`
97 98 99 100 |
# File 'lib/flame/path.rb', line 97 def assign_arguments(args = {}) result_parts = parts.filter_map { |part| assign_argument(part, args) } self.class.merge result_parts.unshift(nil) end |
#extract_arguments(other_path) ⇒ Hash{Symbol => String}
Extract arguments from other path with values at arguments
91 92 93 |
# File 'lib/flame/path.rb', line 91 def extract_arguments(other_path) Extractor.new(parts, other_path.parts).run end |
#freeze ⇒ Object
Freeze all strings in object
40 41 42 43 44 45 |
# File 'lib/flame/path.rb', line 40 def freeze @path.freeze parts.each(&:freeze) parts.freeze super end |
#parts ⇒ Array<Flame::Path::Part>
Return parts of path, splitted by slash (/)
34 35 36 37 |
# File 'lib/flame/path.rb', line 34 memoize def parts @path.to_s.split('/').reject(&:empty?) .map! { |part| self.class::Part.new(part) } end |
#to_routes_with_endpoint ⇒ Array(Flame::Router::Routes, Flame::Router::Routes)
Path parts as keys of nested Hashes
111 112 113 114 115 116 117 |
# File 'lib/flame/path.rb', line 111 def to_routes_with_endpoint endpoint = parts.reduce(result = Flame::Router::Routes.new) do |hash, part| hash[part] ||= Flame::Router::Routes.new end [result, endpoint] end |
#to_s ⇒ String Also known as: to_str
Returns path as String.
103 104 105 |
# File 'lib/flame/path.rb', line 103 def to_s @path end |