123456789_123456789_123456789_123456789_123456789_

Class: RBS::TypeName

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

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(namespace:, name:) ⇒ TypeName

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 9

def initialize(namespace:, name:)
  @namespace = namespace
  @name = name
  @kind = case
          when name.match?(/\A[A-Z]/)
            :class
          when name.match?(/\A[a-z]/)
            :alias
          when name.start_with?("_")
            :interface
          else
            # Defaults to :class
            :class
          end
end

Class Method Details

.[](namespace, name)

Returns a canonical TypeName instance for the given #namespace / #name pair. The namespace is canonicalized through Namespace.[] so identity-based lookup works regardless of the caller passing a fresh Namespace.new or an already-interned instance.

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 36

def self.[](namespace, name)
  ns = Namespace[namespace.path, namespace.absolute?]

  inner = @intern_cache[ns]
  if inner && (cached = inner[name])
    return cached
  end

  @intern_mutex.synchronize do
    inner = (@intern_cache[ns] ||= {})
    inner[name] ||= new(namespace: ns, name: name)
  end
end

.parse(string)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 113

def self.parse(string)
  absolute = string.start_with?("::")

  *path, name = string.delete_prefix("::").split("::").map(&:to_sym)
  raise unless name

  TypeName[Namespace[path, absolute], name]
end

Instance Attribute Details

#absolute?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 85

def absolute?
  namespace.absolute?
end

#alias?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 77

def alias?
  kind == :alias
end

#class?Boolean (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 73

def class?
  kind == :class
end

#interface?Boolean (readonly)

[ GitHub ]

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

def interface?
  kind == :interface
end

#kind (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 7

attr_reader :kind

#name (readonly)

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 6

attr_reader :name

#namespace (readonly)

[ GitHub ]

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

attr_reader :namespace

Instance Method Details

#+(other)

[ GitHub ]

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

def +(other)
  if other.absolute?
    other
  else
    TypeName[self.to_namespace + other.namespace, other.name]
  end
end

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

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 50

def ==(other)
  return true if equal?(other)
  other.is_a?(self.class) && other.namespace == namespace && other.name == name
end

#absolute!

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 81

def absolute!
  TypeName[namespace.absolute!, name]
end

#eql?(other)

Alias for #==.

[ GitHub ]

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

alias eql? ==

#hash

[ GitHub ]

  
# File 'lib/rbs/type_name.rb', line 57

def hash
  @hash ||= namespace.hash ^ name.hash
end

#relative!

[ GitHub ]

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

def relative!
  TypeName[namespace.relative!, name]
end

#split

[ GitHub ]

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

def split
  namespace.path + [name]
end

#to_json(state = nil)

[ GitHub ]

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

def to_json(state = nil)
  to_s.to_json(state)
end

#to_namespace

[ GitHub ]

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

def to_namespace
  namespace.append(self.name)
end

#to_s

[ GitHub ]

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

def to_s
  "#{namespace.to_s}#{name}"
end

#with_prefix(namespace)

[ GitHub ]

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

def with_prefix(namespace)
  TypeName[namespace + self.namespace, name]
end