Class: RBS::TypeName
- Inherits:
-
Object
- Object
- RBS::TypeName
- Defined in:
- lib/rbs/type_name.rb
Instance Attribute Summary collapse
-
#kind ⇒ Object
readonly
Returns the value of attribute kind.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#namespace ⇒ Object
readonly
Returns the value of attribute namespace.
Class Method Summary collapse
-
.[](namespace, name) ⇒ Object
Returns a canonical ‘TypeName` instance for the given `namespace` / `name` pair.
- .parse(string) ⇒ Object
Instance Method Summary collapse
- #+(other) ⇒ Object
- #==(other) ⇒ Object (also: #eql?)
- #absolute! ⇒ Object
- #absolute? ⇒ Boolean
- #alias? ⇒ Boolean
- #class? ⇒ Boolean
- #hash ⇒ Object
-
#initialize(namespace:, name:) ⇒ TypeName
constructor
A new instance of TypeName.
- #interface? ⇒ Boolean
- #relative! ⇒ Object
- #split ⇒ Object
- #to_json(state = nil) ⇒ Object
- #to_namespace ⇒ Object
- #to_s ⇒ Object
- #with_prefix(namespace) ⇒ Object
Constructor Details
#initialize(namespace:, name:) ⇒ TypeName
Returns a new instance of TypeName.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# 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 |
Instance Attribute Details
#kind ⇒ Object (readonly)
Returns the value of attribute kind.
7 8 9 |
# File 'lib/rbs/type_name.rb', line 7 def kind @kind end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/rbs/type_name.rb', line 6 def name @name end |
#namespace ⇒ Object (readonly)
Returns the value of attribute namespace.
5 6 7 |
# File 'lib/rbs/type_name.rb', line 5 def namespace @namespace end |
Class Method Details
.[](namespace, name) ⇒ Object
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.
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 |
Instance Method Details
#+(other) ⇒ Object
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) ⇒ Object 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! ⇒ Object
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
77 78 79 |
# File 'lib/rbs/type_name.rb', line 77 def alias? kind == :alias end |
#class? ⇒ Boolean
73 74 75 |
# File 'lib/rbs/type_name.rb', line 73 def class? kind == :class end |
#hash ⇒ Object
57 58 59 |
# File 'lib/rbs/type_name.rb', line 57 def hash @hash ||= namespace.hash ^ name.hash end |
#interface? ⇒ Boolean
93 94 95 |
# File 'lib/rbs/type_name.rb', line 93 def interface? kind == :interface end |
#relative! ⇒ Object
89 90 91 |
# File 'lib/rbs/type_name.rb', line 89 def relative! TypeName[namespace.relative!, name] end |
#split ⇒ Object
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 ⇒ Object
69 70 71 |
# File 'lib/rbs/type_name.rb', line 69 def to_namespace namespace.append(self.name) end |
#to_s ⇒ Object
61 62 63 |
# File 'lib/rbs/type_name.rb', line 61 def to_s "#{namespace.to_s}#{name}" end |
#with_prefix(namespace) ⇒ Object
97 98 99 |
# File 'lib/rbs/type_name.rb', line 97 def with_prefix(namespace) TypeName[namespace + self.namespace, name] end |