Class: Tapioca::Compilers::RailsOnSorbetCurrentAttributes

Inherits:
Dsl::Compiler
  • Object
show all
Defined in:
lib/tapioca/dsl/compilers/rails_on_sorbet_currrent_attributes.rb

Overview

: [ConstantType = singleton(::Rails::On::Sorbet::CurrentAttributes)]

Constant Summary collapse

MOD_NAME =
'RailsOnSorbetCurrentAttributeMethods'

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gather_constantsObject

: -> Enumerable



12
13
14
15
16
# File 'lib/tapioca/dsl/compilers/rails_on_sorbet_currrent_attributes.rb', line 12

def gather_constants
  all_classes.select do |klass|
    klass < ::Rails::On::Sorbet::CurrentAttributes
  end
end

Instance Method Details

#decorateObject

: -> void



23
24
25
26
27
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
# File 'lib/tapioca/dsl/compilers/rails_on_sorbet_currrent_attributes.rb', line 23

def decorate
  root.create_path(constant) do |klass|
    mod = klass.create_module(MOD_NAME)
    klass.create_include(MOD_NAME)
    klass.create_extend(MOD_NAME)
    attrs = constant.attribute_map.sort_by do |key, _|
      key.to_s
    end
    # For each setting accessor defined in the class
    attrs.each do |attr_name, attr_type|
      attr_type_str = attr_type.type.to_s
      doc = attr_type.doc
      comments = []
      comments << RBI::Comment.new(doc) if doc
      # Create the RBI definitions for all the missing methods
      mod.create_method(
        attr_name.to_s,
        comments:,
        return_type: attr_type_str,
      )
      mod.create_method(
        "#{attr_name}=",
        comments:,
        parameters:  [create_param('value', type: attr_type_str)],
        return_type: attr_type_str,
      )
    end

  end

end