class Flame::Path
Class for working with paths
Public Class Methods
merge(*parts)
click to toggle source
# File lib/flame/path.rb, line 10 def self.merge(*parts) parts.join('/').gsub(%r{\/{2,}}, '/') end
new(*paths)
click to toggle source
# File lib/flame/path.rb, line 14 def initialize(*paths) @path = self.class.merge(*paths) freeze end
Public Instance Methods
<=>(other)
click to toggle source
Compare by parts count and the first arg position
# 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)
click to toggle source
# 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)
click to toggle source
Complete path for the action of controller @todo Add :arg:type support (:id:num, :name:str, etc.)
# 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 = {})
click to toggle source
Assign arguments to path for `Controller.path_to` @param args [Hash] arguments for assigning
# 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)
click to toggle source
Extract arguments from other path with values at arguments @param other_path [Flame::Path] other path with values at arguments
# 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()
click to toggle source
Calls superclass method
# File lib/flame/path.rb, line 24 def freeze @path.freeze parts.each(&:freeze) parts.freeze super end
match?(other)
click to toggle source
Can recieve other as String
# 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()
click to toggle source
# File lib/flame/path.rb, line 19 def parts @parts ||= @path.to_s.split('/').reject(&:empty?) .map! { |part| PathPart.new(part) } end
to_s()
click to toggle source
# File lib/flame/path.rb, line 92 def to_s @path end
Also aliased as: to_str
Private Instance Methods
assign_argument(part, args = {})
click to toggle source
Helpers for `assign_arguments`
# File lib/flame/path.rb, line 116 def assign_argument(part, args = {}) ## Not argument return part unless part.arg? ## Not required argument return args[part[2..-1].to_sym] if part.opt_arg? ## Required argument param = args[part[1..-1].to_sym] ## Required argument is nil error = Errors::ArgumentNotAssignedError.new(@path, part) raise error if param.nil? ## All is ok param end
compare_parts(part, other_part)
click to toggle source
# File lib/flame/path.rb, line 107 def compare_parts(part, other_part) return unless part return if other_part.nil? && !part.opt_arg? # p other_part, part return true if part.arg? return true if other_part == part end
other_contain_required_parts?(other)
click to toggle source
# File lib/flame/path.rb, line 99 def other_contain_required_parts?(other) other_parts = other.parts req_path_parts = parts.reject(&:opt_arg?) fixed_path_parts = parts.reject(&:arg?) (fixed_path_parts - other_parts).empty? && other_parts.count >= req_path_parts.count end