Class: SorbetRails::ModelPlugins::ActiveRecordSerializedAttribute

Inherits:
Base
  • Object
show all
Defined in:
lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb

Constant Summary

Constants inherited from Base

Base::Parameter

Instance Attribute Summary

Attributes inherited from Base

#available_classes, #model_class

Instance Method Summary collapse

Methods inherited from 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

#any_serialized_columns?(columns_hash) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
47
48
# File 'lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb', line 44

def any_serialized_columns?(columns_hash)
  columns_hash.keys.any? do |column_name|
    !serialization_coder_for_column(column_name).nil?
  end
end

#attr_types_for_coder(serialization_coder) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb', line 51

def attr_types_for_coder(serialization_coder)
  case serialization_coder.to_s
  when 'Hash'
    # Hash uses YAML.load/YAML.dump, and permits pretty much any kind of Hash key
    'T::Hash[T.untyped, T.untyped]'
  when 'Array'
    # YAML.load/YAML.dump
    'T::Array[T.untyped]'
  when 'JSON'
    # ActiveSupport::JSON.encode/ActiveSupport::JSON.decode
    # note that Hash keys are Strings since this is JSON
    'T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[String, T.untyped], Integer, String)'
  when 'Object', ''
    'T.any(T::Array[T.untyped], T::Boolean, Float, T::Hash[T.untyped, T.untyped], Integer, String)'
  else
    serialization_coder.to_s
  end
end

#generate(root) ⇒ Object



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
34
35
36
37
38
39
40
41
# File 'lib/sorbet-rails/model_plugins/active_record_serialized_attribute.rb', line 6

def generate(root)
  columns_hash = @model_class.table_exists? ? @model_class.columns_hash : {}
  return unless any_serialized_columns?(columns_hash)

  serialize_module_name = self.model_module_name('GeneratedSerializedAttributeMethods')
  serialize_module_rbi = root.create_module(serialize_module_name)

  model_class_rbi = root.create_class(self.model_class_name)
  model_class_rbi.create_include(serialize_module_name)

  columns_hash.sort.each do |column_name, column_def|
    serialization_coder = serialization_coder_for_column(column_name)
    next unless serialization_coder

    nilable = nilable_column?(column_def)
    attr_type = attr_types_for_coder(serialization_coder)

    serialize_module_rbi.create_method(
      column_name.to_s,
      return_type: ColumnType.new(base_type: attr_type, nilable: nilable).to_s,
    )

    serialize_module_rbi.create_method(
      "#{column_name}=",
      parameters: [
        Parameter.new('value', type: ColumnType.new(base_type: attr_type, nilable: nilable).to_s)
      ],
      return_type: nil,
    )

    serialize_module_rbi.create_method(
      "#{column_name}?",
      return_type: 'T::Boolean',
    )
  end
end