class RBS::Namespace

def self.empty

def self.empty
  @empty ||= new(path: [], absolute: false)
end

def self.parse(string)

def self.parse(string)
  if string.start_with?("::")
    new(path: string.split("::").drop(1).map(&:to_sym), absolute: true)
  else
    new(path: string.split("::").map(&:to_sym), absolute: false)
  end
end

def self.root

def self.root
  @root ||= new(path: [], absolute: true)
end

def +(other)

def +(other)
  if other.absolute?
    other
  else
    self.class.new(path: path + other.path, absolute: absolute?)
  end
end

def ==(other)

def ==(other)
  other.is_a?(Namespace) && other.path == path && other.absolute? == absolute?
end

def absolute!

def absolute!
  self.class.new(path: path, absolute: true)
end

def absolute?

def absolute?
  @absolute
end

def append(component)

def append(component)
  self.class.new(path: path + [component], absolute: absolute?)
end

def ascend

def ascend
  if block_given?
    current = self
    until current.empty?
      yield current
      current = _ = current.parent
    end
    yield current
    self
  else
    enum_for(:ascend)
  end
end

def empty?

def empty?
  path.empty?
end

def hash

def hash
  path.hash ^ absolute?.hash
end

def initialize(path:, absolute:)

def initialize(path:, absolute:)
  @path = path
  @absolute = absolute ? true : false
end

def parent

def parent
  @parent ||= begin
    raise "Parent with empty namespace" if empty?
    self.class.new(path: path.take(path.size - 1), absolute: absolute?)
  end
end

def relative!

def relative!
  self.class.new(path: path, absolute: false)
end

def relative?

def relative?
  !absolute?
end

def split

def split
  last = path.last or return
  parent = self.parent
  [parent, last]
end

def to_s

def to_s
  if empty?
    absolute? ? "::" : ""
  else
    s = path.join("::")
    absolute? ? "::#{s}::" : "#{s}::"
  end
end

def to_type_name

def to_type_name
  parent, name = split
  raise unless name
  raise unless parent
  TypeName.new(name: name, namespace: parent)
end