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 7

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

Class Method Details

.empty

[ GitHub ]

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

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

.parse(string)

[ GitHub ]

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

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 16

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

Instance Attribute Details

#absolute?Boolean (readonly)

[ GitHub ]

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

def absolute?
  @absolute
end

#empty?Boolean (readonly)

[ GitHub ]

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

def empty?
  path.empty?
end

#path (readonly)

[ GitHub ]

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

attr_reader :path

#relative?Boolean (readonly)

[ GitHub ]

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

def relative?
  !absolute?
end

Instance Method Details

#+(other)

[ GitHub ]

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

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 59

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

#absolute!

[ GitHub ]

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

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

#append(component)

[ GitHub ]

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

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

#ascend

[ GitHub ]

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

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 63

alias eql? ==

#hash

[ GitHub ]

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

def hash
  path.hash ^ absolute?.hash
end

#parent

[ GitHub ]

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

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

#relative!

[ GitHub ]

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

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

#split

[ GitHub ]

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

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

#to_s

[ GitHub ]

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

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 84

def to_type_name
  parent, name = split

  raise unless name
  raise unless parent

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