Class: Flame::Path

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(*paths) ⇒ Path

Create a new instance

Parameters:



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

Parameters:

  • parts (Array<String, Flame::Path>)

    parts of expected path

Returns:



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

Parameters:

  • other (Flame::Path, String)

    other path which will be concatinated

Returns:



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

Parameters:

Returns:

  • (-1, 0, 1)

    result of comparing



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

Parameters:

Returns:

  • (true, false)

    equal or not



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

TODO:

Add :arg:type support (:id:num, :name:str, etc.)

Complete path for the action of controller

Parameters:

  • ctrl (Flame::Controller)

    to which controller adapt

  • action (Symbol)

    to which action of controller adapt

Returns:



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`

Parameters:

  • args (Hash) (defaults to: {})

    arguments for assigning



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

Parameters:

  • other_path (Flame::Path)

    other path with values at arguments

Returns:

  • (Hash{Symbol => String})

    hash of arguments from two paths



91
92
93
# File 'lib/flame/path.rb', line 91

def extract_arguments(other_path)
  Extractor.new(parts, other_path.parts).run
end

#freezeObject

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

#partsArray<Flame::Path::Part>

Return parts of path, splitted by slash (/)

Returns:



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_endpointArray(Flame::Router::Routes, Flame::Router::Routes)

Path parts as keys of nested Hashes

Returns:



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_sString Also known as: to_str

Returns path as String.

Returns:

  • (String)

    path as String



103
104
105
# File 'lib/flame/path.rb', line 103

def to_s
  @path
end