class Fable::Path

Constants

PARENT_ID

Attributes

components[RW]
relative[RW]

Public Class Methods

new(components, relative= false) click to toggle source
# 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
self() click to toggle source
# File lib/fable/path.rb, line 35
def self.self
  Path.new(".")
end

Public Instance Methods

==(other_path) click to toggle source
# 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_string() click to toggle source
# 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?() click to toggle source
# File lib/fable/path.rb, line 31
def contains_named_component?
  components.any?{|x| !x.is_index? }
end
empty?() click to toggle source
# File lib/fable/path.rb, line 23
def empty?
  length == 0
end
head() click to toggle source
# File lib/fable/path.rb, line 11
def head
  components.first
end
length() click to toggle source
# File lib/fable/path.rb, line 27
def length
  components.size
end
parse_components_string(components_string) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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?() click to toggle source
# File lib/fable/path.rb, line 7
def relative?
  relative == true
end
tail() click to toggle source
# File lib/fable/path.rb, line 15
def tail
  if components.size >= 2
    self.class.new(components[1..])
  else
    Path.self
  end
end
to_s() click to toggle source
# File lib/fable/path.rb, line 126
def to_s
  components_string
end