Class: RBS::Definition

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

Defined Under Namespace

Modules: Ancestor Classes: InstanceAncestors, Method, SingletonAncestors, Variable

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_name:, entry:, self_type:, ancestors:) ⇒ Definition

Returns a new instance of Definition.



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/rbs/definition.rb', line 180

def initialize(type_name:, entry:, self_type:, ancestors:)
  case entry
  when Environment::ClassEntry, Environment::ModuleEntry
    # ok
  else
    unless entry.decl.is_a?(AST::Declarations::Interface)
      raise "Declaration should be a class, module, or interface: #{type_name}"
    end
  end

  unless self_type.is_a?(Types::ClassSingleton) || self_type.is_a?(Types::Interface) || self_type.is_a?(Types::ClassInstance)
    raise "self_type should be the type of declaration: #{self_type}"
  end

  @type_name = type_name
  @self_type = self_type
  @entry = entry
  @methods = {}
  @instance_variables = {}
  @class_variables = {}
  @ancestors = ancestors
end

Instance Attribute Details

#ancestorsObject (readonly)

Returns the value of attribute ancestors.



174
175
176
# File 'lib/rbs/definition.rb', line 174

def ancestors
  @ancestors
end

#class_variablesObject (readonly)

Returns the value of attribute class_variables.



178
179
180
# File 'lib/rbs/definition.rb', line 178

def class_variables
  @class_variables
end

#entryObject (readonly)

Returns the value of attribute entry.



173
174
175
# File 'lib/rbs/definition.rb', line 173

def entry
  @entry
end

#instance_variablesObject (readonly)

Returns the value of attribute instance_variables.



177
178
179
# File 'lib/rbs/definition.rb', line 177

def instance_variables
  @instance_variables
end

#methodsObject (readonly)

Returns the value of attribute methods.



176
177
178
# File 'lib/rbs/definition.rb', line 176

def methods
  @methods
end

#self_typeObject (readonly)

Returns the value of attribute self_type.



175
176
177
# File 'lib/rbs/definition.rb', line 175

def self_type
  @self_type
end

#type_nameObject (readonly)

Returns the value of attribute type_name.



172
173
174
# File 'lib/rbs/definition.rb', line 172

def type_name
  @type_name
end

Instance Method Details

#class?Boolean

Returns:

  • (Boolean)


203
204
205
# File 'lib/rbs/definition.rb', line 203

def class?
  entry.is_a?(Environment::ClassEntry)
end

#class_type?Boolean

Returns:

  • (Boolean)


215
216
217
# File 'lib/rbs/definition.rb', line 215

def class_type?
  self_type.is_a?(Types::ClassSingleton)
end

#each_type(&block) ⇒ Object



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/rbs/definition.rb', line 260

def each_type(&block)
  if block_given?
    methods.each_value do |method|
      if method.defined_in == type_name
        method.method_types.each do |method_type|
          method_type.each_type(&block)
        end
      end
    end

    instance_variables.each_value do |var|
      if var.declared_in == type_name
        yield var.type
      end
    end

    class_variables.each_value do |var|
      if var.declared_in == type_name
        yield var.type
      end
    end
  else
    enum_for :each_type
  end
end

#instance_type?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/rbs/definition.rb', line 219

def instance_type?
  self_type.is_a?(Types::ClassInstance)
end

#interface?Boolean

Returns:

  • (Boolean)


211
212
213
# File 'lib/rbs/definition.rb', line 211

def interface?
  entry.is_a?(Environment::SingleEntry) && entry.decl.is_a?(AST::Declarations::Interface)
end

#interface_type?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/rbs/definition.rb', line 223

def interface_type?
  self_type.is_a?(Types::Interface)
end

#map_method_type(&block) ⇒ Object



250
251
252
253
254
255
256
257
258
# File 'lib/rbs/definition.rb', line 250

def map_method_type(&block)
  definition = self.class.new(type_name: type_name, self_type: self_type, ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.map_method_type(&block) })
  definition.instance_variables.merge!(instance_variables)
  definition.class_variables.merge!(class_variables)

  definition
end

#module?Boolean

Returns:

  • (Boolean)


207
208
209
# File 'lib/rbs/definition.rb', line 207

def module?
  entry.is_a?(Environment::ModuleEntry)
end

#sub(s) ⇒ Object



240
241
242
243
244
245
246
247
248
# File 'lib/rbs/definition.rb', line 240

def sub(s)
  definition = self.class.new(type_name: type_name, self_type: self_type.sub(s), ancestors: ancestors, entry: entry)

  definition.methods.merge!(methods.transform_values {|method| method.sub(s) })
  definition.instance_variables.merge!(instance_variables.transform_values {|v| v.sub(s) })
  definition.class_variables.merge!(class_variables.transform_values {|v| v.sub(s) })

  definition
end

#type_paramsObject



227
228
229
# File 'lib/rbs/definition.rb', line 227

def type_params
  type_params_decl.each.map(&:name)
end

#type_params_declObject



231
232
233
234
235
236
237
238
# File 'lib/rbs/definition.rb', line 231

def type_params_decl
  case entry
  when Environment::ClassEntry, Environment::ModuleEntry
    entry.type_params
  when Environment::SingleEntry
    entry.decl.type_params
  end
end