Class: RBS::TypeName

Inherits:
Object
  • Object
show all
Includes:
_ToJson
Defined in:
sig/typename.rbs,
lib/rbs/type_name.rb

Overview

TypeName represents name of types in RBS.

TypeNames are one of the three kind, class, alias, and interface. class type names corresponds to Ruby classes and modules. There are no corresponding Ruby value to alias and interface type names.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(namespace:, name:) ⇒ TypeName

Initializer accepts two keyword args, namespace and name. Note that kind is automatically determined from its name.

If the name starts with capital alphabet, it is class. If the name starts with lower case alphabet, it is alias. If the name starts with an underscore, it is interface.

Parameters:



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'sig/typename.rbs', line 29

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

Instance Attribute Details

#kindkind (readonly)

Kind of the type.

Returns:



20
21
22
# File 'sig/typename.rbs', line 20

def kind
  @kind
end

#nameSymbol (readonly)

Name of type name.

Returns:

  • (Symbol)


17
18
19
# File 'sig/typename.rbs', line 17

def name
  @name
end

#namespaceNamespace (readonly)

The namespace the type name is defined in.

Returns:



14
15
16
# File 'sig/typename.rbs', line 14

def namespace
  @namespace
end

Class Method Details

.[](namespace, name) ⇒ TypeName

Returns an interned TypeName instance.

The given namespace is canonicalized so cached instances always hold an interned namespace. Subsequent calls with structurally equal arguments return the same object.

Parameters:

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
# 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) ⇒ RBS::TypeName

Returns type name with given string representation.

Parameters:

  • name (String)

Returns:



87
88
89
90
91
92
93
94
# File 'sig/typename.rbs', line 87

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 Method Details

#+(other) ⇒ TypeName

Parameters:

Returns:



105
106
107
108
109
110
111
# 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) ⇒ Boolean Also known as: eql?

Parameters:

  • other (Object)

Returns:

  • (Boolean)


50
51
52
53
# 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!TypeName

Returns:



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

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

#absolute?Boolean

Returns:

  • (Boolean)


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

def absolute?
  namespace.absolute?
end

#alias?Boolean

Returns true when self is an alias type name.

Returns:

  • (Boolean)


61
62
63
# File 'sig/typename.rbs', line 61

def alias?
  kind == :alias
end

#class?Boolean

Returns true when self is a class type name.

Returns:

  • (Boolean)


58
59
60
# File 'sig/typename.rbs', line 58

def class?
  kind == :class
end

#hashInteger

Returns:

  • (Integer)


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

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

#interface?Boolean

Returns true when self is an interface type name.

Returns:

  • (Boolean)


70
71
72
# File 'sig/typename.rbs', line 70

def interface?
  kind == :interface
end

#relative!TypeName

Returns:



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

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

#splitArray[Symbol]

Returns:

  • (Array[Symbol])


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

def split
  namespace.path + [name]
end

#to_json(state = nil) ⇒ Object



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

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

#to_namespaceNamespace

Returns a namespace with same components of self.

Returns:



55
56
57
# File 'sig/typename.rbs', line 55

def to_namespace
  namespace.append(self.name)
end

#to_s::String

Returns:

  • (::String)


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

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

#with_prefix(namespace) ⇒ TypeName

Returns a new type name with a namespace appended to given namespace.

TypeName.parse("Hello").with_prefix(Namespace("World"))           # => World::Hello
TypeName.parse("Foo::Bar").with_prefix(Namespace("::Hello"))      # => ::Hello::Foo::Bar
TypeName.parse("::A::B").with_prefix(Namespace("C"))              # => ::A::B

Parameters:

Returns:



80
81
82
# File 'sig/typename.rbs', line 80

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