Class: RBS::Definition::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/rbs/definition.rb,
sig/definition.rbs

Defined Under Namespace

Classes: TypeDef

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(super_method:, defs:, accessibility:, annotations: [], alias_of:, alias_member: nil) ⇒ Object

Note that the annotations given through annotations: keyword is ignored.



110
111
112
113
114
115
116
117
118
# File 'sig/definition.rbs', line 110

def initialize(super_method:, defs:, accessibility:, annotations: [], alias_of:, alias_member: nil)
  @super_method = super_method
  @defs = defs
  @accessibility = accessibility
  @extra_annotations = []
  @annotations = []
  @alias_of = alias_of
  @alias_member = alias_member
end

Instance Attribute Details

#accessibilityaccessibility (readonly)

Returns the value of attribute accessibility.

Returns:



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

def accessibility
  @accessibility
end

#alias_memberAST::Members::Alias? (readonly)

Present if the method is defined with alias syntax

Returns:



97
98
99
# File 'sig/definition.rbs', line 97

def alias_member
  @alias_member
end

#alias_ofMethod? (readonly)

The original method when the method is defined with alias syntax

Returns:



94
95
96
# File 'sig/definition.rbs', line 94

def alias_of
  @alias_of
end

#annotationsArray[AST::Annotation] (readonly)

Union of annotations given to defs and alias, not contains annotations given to each overload

The elements will be updated during Method object setup.

Returns:



106
107
108
# File 'sig/definition.rbs', line 106

def annotations
  @annotations
end

#commentsArray[AST::Comment] (readonly)

Returns the value of attribute comments.

Returns:



151
152
153
# File 'lib/rbs/definition.rb', line 151

def comments
  @comments ||= defs.map(&:comment).compact.uniq
end

#defined_inTypeName? (readonly)

Returns the value of attribute defined_in.

Returns:



133
134
135
136
137
138
# File 'lib/rbs/definition.rb', line 133

def defined_in
  @defined_in ||= begin
    last_def = defs.last or raise
    last_def.defined_in
  end
end

#defsArray[TypeDef] (readonly)

Returns the value of attribute defs.

Returns:



100
101
102
# File 'lib/rbs/definition.rb', line 100

def defs
  @defs
end

#extra_annotationsObject (readonly)

Returns the value of attribute extra_annotations.



102
103
104
# File 'lib/rbs/definition.rb', line 102

def extra_annotations
  @extra_annotations
end

#implemented_inTypeName? (readonly)

Returns the value of attribute implemented_in.

Returns:



140
141
142
143
144
145
# File 'lib/rbs/definition.rb', line 140

def implemented_in
  @implemented_in ||= begin
    last_def = defs.last or raise
    last_def.implemented_in
  end
end

#membersArray[method_member] (readonly)

Returns the value of attribute members.

Returns:

  • (Array[method_member])


155
156
157
# File 'lib/rbs/definition.rb', line 155

def members
  @members ||= defs.map(&:member).uniq
end

#method_typesArray[MethodType] (readonly)

Returns the value of attribute method_types.

Returns:



147
148
149
# File 'lib/rbs/definition.rb', line 147

def method_types
  @method_types ||= defs.map(&:type)
end

#super_methodMethod? (readonly)

Returns the value of attribute super_method.

Returns:



99
100
101
# File 'lib/rbs/definition.rb', line 99

def super_method
  @super_method
end

Instance Method Details

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



117
118
119
120
121
122
123
124
125
# File 'lib/rbs/definition.rb', line 117

def ==(other)
  other.is_a?(Method) &&
    other.super_method == super_method &&
    other.defs == defs &&
    other.accessibility == accessibility &&
    other.annotations == annotations &&
    other.alias_of == alias_of &&
    other.alias_member == alias_member
end

#hashObject



129
130
131
# File 'lib/rbs/definition.rb', line 129

def hash
  self.class.hash ^ super_method.hash ^ defs.hash ^ accessibility.hash ^ annotations.hash ^ alias_of.hash
end

#map_method_type {|arg0| ... } ⇒ Method

Yields:

Yield Parameters:

Yield Returns:

Returns:



190
191
192
193
194
# File 'lib/rbs/definition.rb', line 190

def map_method_type(&block)
  update(
    defs: defs.map {|defn| defn.update(type: yield(defn.type)) },
  )
end

#map_type {|arg0| ... } ⇒ Method

Applies the mapping from Types::t to Types::t.

Note this method doesn't handle upper bound in type params. You may want to use #map_type_bound explicitly, or #sub for simple substitution.

Yields:

Yield Parameters:

  • arg0 (Types::t)

Yield Returns:

  • (Types::t)

Returns:



139
140
141
142
143
144
# File 'sig/definition.rbs', line 139

def map_type(&block)
  update(
    super_method: super_method&.map_type(&block),
    defs: defs.map {|defn| defn.update(type: defn.type.map_type(&block)) }
  )
end

#map_type_bound {|arg0| ... } ⇒ Method

Yields:

Yield Parameters:

  • arg0 (Types::t)

Yield Returns:

  • (Types::t)

Returns:



183
184
185
186
187
188
# File 'lib/rbs/definition.rb', line 183

def map_type_bound(&block)
  update(
    super_method: super_method&.map_type_bound(&block),
    defs: defs.map {|defn| defn.update(type: defn.type.map_type_bound(&block)) }
  )
end

#private?Boolean

Returns:

  • (Boolean)


163
164
165
# File 'lib/rbs/definition.rb', line 163

def private?
  @accessibility == :private
end

#public?Boolean

Returns:

  • (Boolean)


159
160
161
# File 'lib/rbs/definition.rb', line 159

def public?
  @accessibility == :public
end

#sub(s) ⇒ Method

Substitutes type variables to some types. Takes care of type parameter bounds.

Parameters:

Returns:



132
133
134
135
136
137
138
139
# File 'sig/definition.rbs', line 132

def sub(s)
  return self if s.empty?

  update(
    super_method: super_method&.sub(s),
    defs: defs.map {|defn| defn.update(type: defn.type.sub(s)) }
  )
end

#update(super_method: self.super_method, defs: self.defs, accessibility: self.accessibility, alias_of: self.alias_of, annotations: self.annotations, alias_member: self.alias_member) ⇒ Object



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/rbs/definition.rb', line 196

def update(super_method: self.super_method, defs: self.defs, accessibility: self.accessibility, alias_of: self.alias_of, annotations: self.annotations, alias_member: self.alias_member)
  self.class.new(
    super_method: super_method,
    defs: defs,
    accessibility: accessibility,
    alias_of: alias_of,
    alias_member: alias_member
  ).tap do |method|
    method.annotations.replace(annotations)
  end
end