123456789_123456789_123456789_123456789_123456789_

Class: RBS::Namespace

Relationships & Source Files
Inherits: Object
Defined in: lib/rbs/namespace.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(path:, absolute:) ⇒ Namespace

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 5

def initialize(path:, absolute:)
  @path = path
  @absolute = absolute
end

Class Method Details

.empty

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 10

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

.parse(string)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 89

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

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 14

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

Instance Attribute Details

#absolute?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 35

def absolute?
  @absolute
end

#empty?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 51

def empty?
  path.empty?
end

#path (readonly)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 3

attr_reader :path

#relative?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 39

def relative?
  !absolute?
end

Instance Method Details

#+(other)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 18

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

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

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 55

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

#absolute!

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 43

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

#append(component)

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 26

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

#ascend

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 97

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

#eql?(other)

Alias for #==.

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 59

alias eql? ==

#hash

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 61

def hash
  self.class.hash ^ path.hash ^ absolute?.hash
end

#parent

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 30

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

#relative!

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 47

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

#split

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 65

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

#to_s

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 71

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

#to_type_name

[ GitHub ]

  
# File 'lib/rbs/namespace.rb', line 80

def to_type_name
  parent, name = split

  raise unless name
  raise unless parent

  TypeName.new(name: name, namespace: parent)
end