Class: RBS::TypeName

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/type_name.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#kindObject (readonly)

Returns the value of attribute kind.



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

def kind
  @kind
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#namespaceObject (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

.parse(string) ⇒ Object



113
114
115
116
117
118
119
120
# 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 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

Returns:

  • (Boolean)


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

def absolute?
  namespace.absolute?
end

#alias?Boolean

Returns:

  • (Boolean)


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

def alias?
  kind == :alias
end

#class?Boolean

Returns:

  • (Boolean)


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

def class?
  kind == :class
end

#hashObject



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

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

#interface?Boolean

Returns:

  • (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

#splitObject



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_namespaceObject



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

def to_namespace
  namespace.append(self.name)
end

#to_sObject



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