empty()
click to toggle source
def self.empty
new(path: [], absolute: false)
end
new(path:, absolute:)
click to toggle source
def initialize(path,, absolute))
@path = path
@absolute = absolute
end
parse(string)
click to toggle source
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
root()
click to toggle source
def self.root
new(path: [], absolute: true)
end
+(other)
click to toggle source
def +(other)
if other.absolute?
other
else
self.class.new(path: path + other.path, absolute: absolute?)
end
end
==(other)
click to toggle source
def ==(other)
other.is_a?(Namespace) && other.path == path && other.absolute? == absolute?
end
absolute!()
click to toggle source
def absolute!
self.class.new(path: path, absolute: true)
end
absolute?()
click to toggle source
def absolute?
@absolute
end
append(component)
click to toggle source
def append(component)
self.class.new(path: path + [component], absolute: absolute?)
end
ascend()
click to toggle source
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
empty?()
click to toggle source
def empty?
path.empty?
end
eql?(other)
click to toggle source
hash()
click to toggle source
def hash
self.class.hash ^ path.hash ^ absolute?.hash
end
parent()
click to toggle source
def parent
raise "Parent with empty namespace" if empty?
self.class.new(path: path.take(path.size - 1), absolute: absolute?)
end
relative!()
click to toggle source
def relative!
self.class.new(path: path, absolute: false)
end
relative?()
click to toggle source
def relative?
!absolute?
end
split()
click to toggle source
def split
last = path.last or return
parent = self.parent
[parent, last]
end
to_s()
click to toggle source
def to_s
if empty?
absolute? ? "::" : ""
else
s = path.join("::")
absolute? ? "::#{s}::" : "#{s}::"
end
end
to_type_name()
click to toggle source
def to_type_name
parent, name = split
raise unless name
raise unless parent
TypeName.new(name: name, namespace: parent)
end