Class: Fable::Path

Inherits:
Object
  • Object
show all
Defined in:
lib/fable/path.rb

Defined Under Namespace

Classes: Component

Constant Summary collapse

PARENT_ID =
"^".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(components, relative = false) ⇒ Path

Returns a new instance of Path.



39
40
41
42
43
44
45
46
# File 'lib/fable/path.rb', line 39

def initialize(components, relative= false)
  if components.is_a?(String)
    parse_components_string(components)
  else
    self.components = components
    self.relative = relative
  end
end

Instance Attribute Details

#componentsObject

Returns the value of attribute components.



5
6
7
# File 'lib/fable/path.rb', line 5

def components
  @components
end

#relativeObject

Returns the value of attribute relative.



5
6
7
# File 'lib/fable/path.rb', line 5

def relative
  @relative
end

Class Method Details

.selfObject



35
36
37
# File 'lib/fable/path.rb', line 35

def self.self
  Path.new(".")
end

Instance Method Details

#==(other_path) ⇒ Object



119
120
121
122
123
124
# File 'lib/fable/path.rb', line 119

def ==(other_path)
  return false if other_path.nil?
  return false if other_path.components.size != components.size
  return false if other_path.relative? != relative?
  return other_path.components == components
end

#components_stringObject



89
90
91
92
93
94
95
96
# File 'lib/fable/path.rb', line 89

def components_string
  string = components.map{|x| x.to_s}.join('.')
  if relative?
    string = ".#{string}"
  end

  string
end

#contains_named_component?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/fable/path.rb', line 31

def contains_named_component?
  components.any?{|x| !x.is_index? }
end

#empty?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/fable/path.rb', line 23

def empty?
  length == 0
end

#headObject



11
12
13
# File 'lib/fable/path.rb', line 11

def head
  components.first
end

#lengthObject



27
28
29
# File 'lib/fable/path.rb', line 27

def length
  components.size
end

#parse_components_string(components_string) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/fable/path.rb', line 98

def parse_components_string(components_string)
  self.components = []
  return if components_string.strip.empty?

  # Relative path when components staet with "."
  # example: .^.^.hello.5 is equivalent to filesystem path
  # ../../hello/5

  if components_string.start_with?(".")
    @relative = true
  else
    @relative = false
  end

  components_string.split('.').each do |section|
    next if section.empty? #usually the first item in a relative path

    components << Component.new(Component.component_type(section))
  end
end

#path_by_appending_component(component) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
# File 'lib/fable/path.rb', line 77

def path_by_appending_component(component)
  if !component.is_a?(Path::Component)
    component = Component.new(Component.component_type(component))
  end
  new_path = Path.new("")

  new_path.components += self.components

  new_path.components << component
  new_path
end

#path_by_appending_path(path_to_append) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/fable/path.rb', line 48

def path_by_appending_path(path_to_append)
  new_path = Path.new("")

  upward_moves = 0

  path_to_append.components.each do |component|
    if component.is_parent?
      upward_moves += 1
    else
      break
    end
  end

  upward_jumps_to_make = (components.size - upward_moves-1)
  components_to_add_at_this_level = (0..upward_jumps_to_make)

  components_to_add_at_this_level.each do |i|
    new_path.components << components[i]
  end

  components_to_add_after_upward_move = (upward_moves..path_to_append.components.size)

  components_to_add_after_upward_move.each do |i|
    new_path.components << path_to_append.components[i]
  end

  new_path
end

#relative?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/fable/path.rb', line 7

def relative?
  relative == true
end

#tailObject



15
16
17
18
19
20
21
# File 'lib/fable/path.rb', line 15

def tail
  if components.size >= 2
    self.class.new(components[1..])
  else
    Path.self
  end
end

#to_sObject



126
127
128
# File 'lib/fable/path.rb', line 126

def to_s
  components_string
end