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.
      12 13 14 15  | 
    
      # File 'lib/flame/path.rb', line 12 def initialize(*paths) @path = self.class.merge(*paths) freeze end  | 
  
Class Method Details
.merge(*parts) ⇒ Object
      8 9 10  | 
    
      # File 'lib/flame/path.rb', line 8 def self.merge(*parts) parts.join('/').gsub(%r{\/{2,}}, '/') end  | 
  
Instance Method Details
#<=>(other) ⇒ Object
Compare by parts count and the first arg position
      30 31 32 33 34 35 36 37 38 39 40  | 
    
      # File 'lib/flame/path.rb', line 30 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
      42 43 44 45  | 
    
      # File 'lib/flame/path.rb', line 42 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
      49 50 51 52 53 54 55 56 57  | 
    
      # File 'lib/flame/path.rb', line 49 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 end  | 
  
#assign_arguments(args = {}) ⇒ Object
Assign arguments to path for ‘Controller.path_to`
      85 86 87 88  | 
    
      # File 'lib/flame/path.rb', line 85 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
      72 73 74 75 76 77 78 79 80 81  | 
    
      # File 'lib/flame/path.rb', line 72 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
      22 23 24 25 26 27  | 
    
      # File 'lib/flame/path.rb', line 22 def freeze @path.freeze parts.each(&:freeze) parts.freeze super end  | 
  
#match?(other) ⇒ Boolean
Can recieve other as String
      60 61 62 63 64 65 66 67 68  | 
    
      # File 'lib/flame/path.rb', line 60 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
      17 18 19 20  | 
    
      # File 'lib/flame/path.rb', line 17 def parts @parts ||= @path.to_s.split('/').reject(&:empty?) .map! { |part| PathPart.new(part) } end  | 
  
#to_s ⇒ Object Also known as: to_str
      90 91 92  | 
    
      # File 'lib/flame/path.rb', line 90 def to_s @path end  |