Class: Flame::Path
- Inherits:
-
Object
- Object
- Flame::Path
- Defined in:
- lib/flame/path.rb
Overview
Class for working with paths
Defined Under Namespace
Classes: PathPart
Class Method Summary collapse
Instance Method Summary collapse
-
#<=>(other) ⇒ Object
Compare by parts count and the first arg position.
- #==(other) ⇒ Object
-
#adapt(ctrl, action) ⇒ Object
Complete path for the action of controller.
-
#assign_arguments(args = {}) ⇒ Object
Assign arguments to path for ‘Controller.path_to`.
-
#extract_arguments(other_path) ⇒ Object
Extract arguments from other path with values at arguments.
- #freeze ⇒ Object
-
#initialize(*paths) ⇒ Path
constructor
A new instance of Path.
-
#match?(other) ⇒ Boolean
Can recieve other as String.
- #parts ⇒ Object
- #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(*paths) ⇒ Path
Returns a new instance of Path.
14 15 16 17 |
# File 'lib/flame/path.rb', line 14 def initialize(*paths) @path = self.class.merge(*paths) freeze end |
Class Method Details
.merge(*parts) ⇒ Object
10 11 12 |
# File 'lib/flame/path.rb', line 10 def self.merge(*parts) parts.join('/').gsub(%r{\/{2,}}, '/') end |
Instance Method Details
#<=>(other) ⇒ Object
Compare by parts count and the first arg position
32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/flame/path.rb', line 32 def <=>(other) self_parts, other_parts = [self, other].map(&:parts) parts_size = self_parts.size <=> other_parts.size return parts_size unless parts_size.zero? self_parts.zip(other_parts) .reduce(0) do |result, (self_part, other_part)| break -1 if self_part.arg? && !other_part.arg? break 1 if other_part.arg? && !self_part.arg? result end end |
#==(other) ⇒ Object
44 45 46 47 |
# File 'lib/flame/path.rb', line 44 def ==(other) other = self.class.new(other) if other.is_a? String parts == other.parts end |
#adapt(ctrl, action) ⇒ Object
TODO:
Add :arg:type support (:id:num, :name:str, etc.)
Complete path for the action of controller
51 52 53 54 55 56 57 58 59 |
# File 'lib/flame/path.rb', line 51 def adapt(ctrl, action) parameters = ctrl.instance_method(action).parameters parameters.map! do |parameter| parameter_type, parameter_name = parameter path_part = PathPart.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`
87 88 89 90 |
# File 'lib/flame/path.rb', line 87 def assign_arguments(args = {}) result_parts = parts.map { |part| assign_argument(part, args) }.compact self.class.merge result_parts.unshift(nil) end |
#extract_arguments(other_path) ⇒ Object
Extract arguments from other path with values at arguments
74 75 76 77 78 79 80 81 82 83 |
# File 'lib/flame/path.rb', line 74 def extract_arguments(other_path) parts.each_with_index.with_object({}) do |(part, i), args| other_part = other_path.parts[i].to_s next args unless part.arg? break args if part.opt_arg? && other_part.empty? args[ part[(part.opt_arg? ? 2 : 1)..-1].to_sym ] = URI.decode(other_part) end end |
#freeze ⇒ Object
24 25 26 27 28 29 |
# File 'lib/flame/path.rb', line 24 def freeze @path.freeze parts.each(&:freeze) parts.freeze super end |
#match?(other) ⇒ Boolean
Can recieve other as String
62 63 64 65 66 67 68 69 70 |
# File 'lib/flame/path.rb', line 62 def match?(other) other = self.class.new(other) if other.is_a? String return false unless other_contain_required_parts?(other) result = [self, other].map { |path| path.parts.size }.max.times do |i| break false unless compare_parts parts[i], other.parts[i] end result = true if result result end |
#parts ⇒ Object
19 20 21 22 |
# File 'lib/flame/path.rb', line 19 def parts @parts ||= @path.to_s.split('/').reject(&:empty?) .map! { |part| PathPart.new(part) } end |
#to_s ⇒ Object Also known as: to_str
92 93 94 |
# File 'lib/flame/path.rb', line 92 def to_s @path end |