Class: Jsapi::Meta::Pathname

Inherits:
Object
  • Object
show all
Defined in:
lib/jsapi/meta/pathname.rb

Overview

Represents a relative path name.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*segments) ⇒ Pathname

:nodoc:



20
21
22
23
24
25
# File 'lib/jsapi/meta/pathname.rb', line 20

def initialize(*segments) # :nodoc:
  @segments = segments.flat_map do |segment|
    segment = segment.to_s.delete_prefix('/')
    segment.present? ? segment.split('/', -1) : ''
  end
end

Instance Attribute Details

#segmentsObject (readonly)

Returns the value of attribute segments.



16
17
18
# File 'lib/jsapi/meta/pathname.rb', line 16

def segments
  @segments
end

Class Method Details

.from(name) ⇒ Object

Transforms name to an instance of this class.



9
10
11
12
13
# File 'lib/jsapi/meta/pathname.rb', line 9

def from(name)
  return name if name.is_a?(Pathname)

  name.nil? ? new : new(name)
end

Instance Method Details

#+(other) ⇒ Object

Creates a new Pathname by appending other to itself. Returns itself if other is nil.



35
36
37
38
39
# File 'lib/jsapi/meta/pathname.rb', line 35

def +(other)
  return self if other.nil?

  Pathname.new(*@segments, *Pathname.from(other).segments)
end

#==(other) ⇒ Object Also known as: eql?

:nodoc:



27
28
29
# File 'lib/jsapi/meta/pathname.rb', line 27

def ==(other) # :nodoc:
  other.is_a?(Pathname) && segments == other.segments
end

#ancestorsObject

Returns an array containing itself and all parent pathnames.



42
43
44
45
46
# File 'lib/jsapi/meta/pathname.rb', line 42

def ancestors
  @ancestors ||= @segments.count.downto(0).map do |i|
    Pathname.new(*@segments[0, i])
  end
end

#inspectObject

:nodoc:



48
49
50
# File 'lib/jsapi/meta/pathname.rb', line 48

def inspect # :nodoc:
  "#<#{self.class} #{to_s.inspect}>"
end

#to_sObject

Returns the relative path name as a string.



53
54
55
56
57
# File 'lib/jsapi/meta/pathname.rb', line 53

def to_s
  @to_s ||= @segments.presence&.each_with_index&.map do |segment, index|
    index.zero? && segment.blank? ? '//' : "/#{segment}"
  end&.join || '/'
end