Module: RactorRailsShim::ActiveRecordModelSchemaPatch::InstanceMethods

Defined in:
lib/ractor_rails_shim/patches/active_record_model_schema.rb

Instance Method Summary collapse

Instance Method Details

#_returning_columns_for_insert(connection) ⇒ Object

:nodoc:



26
27
28
29
30
31
32
33
34
35
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 26

def _returning_columns_for_insert(connection) # :nodoc:
  key = :"rrs_returning_cols_#{object_id}"
  RactorRailsShim.storage[key] ||= begin
    auto_populated_columns = columns.filter_map do |c|
      c.name if connection.return_value_after_insert?(c)
    end

    auto_populated_columns.empty? ? Array(primary_key) : auto_populated_columns
  end
end

#attributes_builderObject

:nodoc:



37
38
39
40
41
42
43
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 37

def attributes_builder # :nodoc:
  key = :"rrs_attributes_builder_#{object_id}"
  RactorRailsShim.storage[key] ||= begin
    defaults = _default_attributes.except(*(column_names - [primary_key]))
    ::ActiveModel::AttributeSet::Builder.new(attribute_types, defaults)
  end
end

#column_defaultsObject

:nodoc:



45
46
47
48
49
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 45

def column_defaults # :nodoc:
  key = :"rrs_column_defaults_#{object_id}"
  RactorRailsShim.storage[key] ||=
    _default_attributes.deep_dup.to_hash.freeze
end

#column_namesObject



81
82
83
84
85
86
87
88
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 81

def column_names
  if ::Ractor.main?
    @column_names ||= columns.map(&:name).freeze
  else
    key = :"rrs_column_names_#{object_id}"
    RactorRailsShim.storage[key] ||= columns.map(&:name).freeze
  end
end

#columnsObject

columns / column_names / symbol_column_to_string / content_columns / attribute_names all memoize via a class ivar (@columns, @column_names, ...). Writing that ivar from a worker Ractor raises "can not set instance variables of classes/modules by non-main Ractors" — and kino's worker Ractors do not share main's class-ivar space (unlike Ractor.new workers), so the ||= can never populate it. Route each cache through per-Ractor IES, keyed by the class, mirroring the other ModelSchema patches above. Main keeps the original class-ivar path.



72
73
74
75
76
77
78
79
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 72

def columns
  if ::Ractor.main?
    @columns ||= columns_hash.values.freeze
  else
    key = :"rrs_columns_#{object_id}"
    RactorRailsShim.storage[key] ||= columns_hash.values.freeze
  end
end

#content_columnsObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 101

def content_columns
  if ::Ractor.main?
    @content_columns ||= columns.reject do |c|
      c.name == primary_key ||
        c.name == inheritance_column ||
        c.name.end_with?("_id", "_count")
    end.freeze
  else
    key = :"rrs_content_cols_#{object_id}"
    RactorRailsShim.storage[key] ||= columns.reject do |c|
      c.name == primary_key ||
        c.name == inheritance_column ||
        c.name.end_with?("_id", "_count")
    end.freeze
  end
end

#symbol_column_to_string(name_symbol) ⇒ Object

:nodoc:



90
91
92
93
94
95
96
97
98
99
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 90

def symbol_column_to_string(name_symbol) # :nodoc:
  if ::Ractor.main?
    @symbol_column_to_string_name_hash ||= column_names.index_by(&:to_sym)
    @symbol_column_to_string_name_hash[name_symbol]
  else
    key = :"rrs_symbol_col_#{object_id}"
    hash = RactorRailsShim.storage[key] ||= column_names.index_by(&:to_sym)
    hash[name_symbol]
  end
end

#yaml_encoderObject

:nodoc:



51
52
53
54
55
# File 'lib/ractor_rails_shim/patches/active_record_model_schema.rb', line 51

def yaml_encoder # :nodoc:
  key = :"rrs_yaml_encoder_#{object_id}"
  RactorRailsShim.storage[key] ||=
    ::ActiveModel::AttributeSet::YAMLEncoder.new(attribute_types)
end