123456789_123456789_123456789_123456789_123456789_

Class: RBS::Namespace

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

Constant Summary

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

.[](path, absolute)

Returns a canonical Namespace instance for the given #path / absolute pair. Repeated calls with structurally equal arguments return the same object, so callers can rely on equal? for fast equality. The path Array is duplicated and frozen on insert.

[ GitHub ]

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

def self.[](path, absolute)
  absolute = absolute ? true : false

  # Lock-free fast path.
  node = absolute ? @intern_trie_absolute : @intern_trie_relative
  path.each do |sym|
    node = node[sym]
    break unless node
  end
  if node && (cached = node[INTERN_LEAF])
    return cached
  end

  @intern_mutex.synchronize do
    node = absolute ? @intern_trie_absolute : @intern_trie_relative
    path.each { |sym| node = (node[sym] ||= {}) }
    node[INTERN_LEAF] ||= begin
      frozen_path = path.frozen? ? path : path.dup.freeze
      new(path: frozen_path, absolute: absolute)
    end
  end
end

.empty

[ GitHub ]

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

def self.empty
  @empty ||= self[[], false]
end

.parse(string)

[ GitHub ]

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

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

.root

[ GitHub ]

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

def self.root
  @root ||= self[[], true]
end

Instance Attribute Details

#absolute?Boolean (readonly)

[ GitHub ]

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

def absolute?
  @absolute
end

#empty?Boolean (readonly)

[ GitHub ]

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

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 78

def relative?
  !absolute?
end

Instance Method Details

#+(other)

[ GitHub ]

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

def +(other)
  if other.absolute?
    other
  else
    Namespace[path + other.path, absolute?]
  end
end

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

[ GitHub ]

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

def ==(other)
  return true if equal?(other)
  other.is_a?(Namespace) && other.path == path && other.absolute? == absolute?
end

#absolute!

[ GitHub ]

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

def absolute!
  Namespace[path, true]
end

#append(component)

[ GitHub ]

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

def append(component)
  Namespace[path + [component], absolute?]
end

#ascend

[ GitHub ]

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

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 99

alias eql? ==

#hash

[ GitHub ]

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

def hash
  @hash ||= path.hash ^ absolute?.hash
end

#parent

[ GitHub ]

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

def parent
  @parent ||= begin
    raise "Parent with empty namespace" if empty?
    Namespace[path.take(path.size - 1), absolute?]
  end
end

#relative!

[ GitHub ]

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

def relative!
  Namespace[path, false]
end

#split

[ GitHub ]

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

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

#to_s

[ GitHub ]

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

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 120

def to_type_name
  parent, name = split

  raise unless name
  raise unless parent

  TypeName[parent, name]
end