Class: RBS::TypeName
- Inherits:
-
Object
- Object
- RBS::TypeName
- 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
-
#kind ⇒ kind
readonly
Kind of the type.
-
#name ⇒ Symbol
readonly
Name of type name.
-
#namespace ⇒ Namespace
readonly
The namespace the type name is defined in.
Class Method Summary collapse
-
.[](namespace, name) ⇒ TypeName
Returns an interned
TypeNameinstance. -
.parse(string) ⇒ RBS::TypeName
Returns type name with given string representation.
Instance Method Summary collapse
- #+(other) ⇒ TypeName
- #==(other) ⇒ Boolean (also: #eql?)
- #absolute! ⇒ TypeName
- #absolute? ⇒ Boolean
-
#alias? ⇒ Boolean
Returns true when self is an alias type name.
-
#class? ⇒ Boolean
Returns true when self is a class type name.
- #hash ⇒ Integer
-
#initialize(namespace:, name:) ⇒ TypeName
constructor
Initializer accepts two keyword args,
namespaceandname. -
#interface? ⇒ Boolean
Returns true when self is an interface type name.
- #relative! ⇒ TypeName
- #split ⇒ Array[Symbol]
- #to_json(state = nil) ⇒ Object
-
#to_namespace ⇒ Namespace
Returns a namespace with same components of self.
- #to_s ⇒ ::String
-
#with_prefix(namespace) ⇒ TypeName
Returns a new type name with a namespace appended to given namespace.
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.
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
#kind ⇒ kind (readonly)
Kind of the type.
20 21 22 |
# File 'sig/typename.rbs', line 20 def kind @kind end |
#name ⇒ Symbol (readonly)
Name of type name.
17 18 19 |
# File 'sig/typename.rbs', line 17 def name @name end |
#namespace ⇒ Namespace (readonly)
The namespace the type name is defined in.
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.
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.
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
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?
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
81 82 83 |
# File 'lib/rbs/type_name.rb', line 81 def absolute! TypeName[namespace.absolute!, name] end |
#absolute? ⇒ 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.
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.
58 59 60 |
# File 'sig/typename.rbs', line 58 def class? kind == :class end |
#hash ⇒ 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.
70 71 72 |
# File 'sig/typename.rbs', line 70 def interface? kind == :interface end |
#relative! ⇒ TypeName
89 90 91 |
# File 'lib/rbs/type_name.rb', line 89 def relative! TypeName[namespace.relative!, name] end |
#split ⇒ 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_namespace ⇒ Namespace
Returns a namespace with same components of self.
55 56 57 |
# File 'sig/typename.rbs', line 55 def to_namespace namespace.append(self.name) end |
#to_s ⇒ ::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
80 81 82 |
# File 'sig/typename.rbs', line 80 def with_prefix(namespace) TypeName[namespace + self.namespace, name] end |