Class: AttrJsonPlugin

Inherits:
SorbetRails::ModelPlugins::Base show all
Defined in:
lib/sorbet-rails/gem_plugins/attr_json_plugin.rb

Overview

typed: false

Constant Summary

Constants inherited from SorbetRails::ModelPlugins::Base

SorbetRails::ModelPlugins::Base::Parameter

Instance Attribute Summary

Attributes inherited from SorbetRails::ModelPlugins::Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from SorbetRails::ModelPlugins::Base

#initialize, #serialization_coder_for_column

Methods included from SorbetRails::ModelUtils

#add_relation_query_method, #exists_class_method?, #exists_instance_method?, #habtm_class?, #model_assoc_proxy_class_name, #model_assoc_relation_class_name, #model_class_name, #model_module_name, #model_query_methods_returning_assoc_relation_module_name, #model_query_methods_returning_relation_module_name, #model_relation_class_name, #model_relation_type_alias, #model_relation_type_class_name

Methods included from SorbetRails::ModelColumnUtils

#active_record_type_to_sorbet_type, #attribute_has_unconditional_presence_validation?, #model_class, #nilable_column?, #time_zone_aware_column?, #type_for_column_def

Constructor Details

This class inherits a constructor from SorbetRails::ModelPlugins::Base

Instance Method Details

#generate(root) ⇒ Object


4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sorbet-rails/gem_plugins/attr_json_plugin.rb', line 4

def generate(root)
  return unless @model_class.include?(::AttrJson::Record)

  # Module for instance methods
  obj_custom_module_name = self.model_module_name("GeneratedAttrJsonMethods")
  obj_custom_module_rbi = root.create_module(obj_custom_module_name)

  # Module for class methods
  klass_custom_module_name = self.model_module_name("GeneratedAttrJsonClassMethods")
  klass_custom_module_rbi = root.create_module(klass_custom_module_name)

  # here we re-create the model class!
  model_class_rbi = root.create_class(self.model_class_name)
  model_class_rbi.create_include(obj_custom_module_name)
  model_class_rbi.create_extend(klass_custom_module_name)

  # then create custom methods, constants, etc. for this module.
  add_class_methods(klass_custom_module_rbi)

  @model_class.attr_json_registry.definitions.each do |definition|
    add_methods_for_attributes(definition, obj_custom_module_rbi)
  end

  if @model_class.include?(::AttrJson::Record::Dirty)
    obj_custom_module_rbi.create_method(
      'attr_json_changes',
      returns: 'AttrJson::Record::Dirty::Implementation'
    )
  end
end