Class: Decode::RBS::Class

Inherits:
Wrapper
  • Object
show all
Defined in:
lib/decode/rbs/class.rb

Overview

Represents a Ruby class definition wrapper for RBS generation.

Instance Method Summary collapse

Methods inherited from Wrapper

#comment, #tags

Constructor Details

#initialize(definition) ⇒ Class

Initialize a new class wrapper.



16
17
18
19
# File 'lib/decode/rbs/class.rb', line 16

def initialize(definition)
  super
  @generics = nil
end

Instance Method Details

#genericsObject

Extract generic type parameters from the class definition.



23
24
25
# File 'lib/decode/rbs/class.rb', line 23

def generics
  @generics ||= extract_generics
end

#to_rbs_ast(method_definitions = [], constant_definitions = [], attribute_definitions = [], index = nil) ⇒ Object

Convert the class definition to RBS AST



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/decode/rbs/class.rb', line 28

def to_rbs_ast(method_definitions = [], constant_definitions = [], attribute_definitions = [], index = nil)
  name = simple_name_to_rbs(@definition.name)
  comment = self.comment
  
  # Extract generics from RBS tags
  type_params = generics.map do |generic|
    ::RBS::AST::TypeParam.new(
      name: generic.to_sym,
      variance: nil,
      upper_bound: nil,
      location: nil
    )
  end
  
  # Build method definitions:
  methods = method_definitions.map{|method_def| Method.new(method_def).to_rbs_ast(index)}.compact
  
  # Build constant definitions:
  constants = constant_definitions.map{|const_def| build_constant_rbs(const_def)}.compact
  
  # Build attribute definitions and infer instance variable types:
  attributes, instance_variables = build_attributes_rbs(attribute_definitions)
  
  # Extract super class if present:
  super_class = if @definition.super_class
    ::RBS::AST::Declarations::Class::Super.new(
          name: qualified_name_to_rbs(@definition.super_class),
          args: [],
          location: nil
        )
  end
  
  # Create the class declaration with generics:
  ::RBS::AST::Declarations::Class.new(
    name: name,
    type_params: type_params,
    super_class: super_class,
    members: constants + attributes + instance_variables + methods,
    annotations: [],
    location: nil,
    comment: comment
  )
end