Class: ActiveRecord::Type::Serialized

Inherits:
ActiveModel::Type::Value show all
Includes:
ActiveModel::Type::Helpers::Mutable
Defined in:
activerecord/lib/active_record/type/serialized.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from ActiveModel::Type::Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from ActiveModel::Type::Helpers::Mutable

#cast, #immutable_value

Methods inherited from ActiveModel::Type::Value

#==, #as_json, #binary?, #cast, #changed?, #hash, #immutable_value, #map, #serializable?, #type, #type_cast_for_schema, #value_constructed_by_mass_assignment?

Constructor Details

#initialize(subtype, coder) ⇒ Serialized

Returns a new instance of Serialized.



12
13
14
15
16
# File 'activerecord/lib/active_record/type/serialized.rb', line 12

def initialize(subtype, coder)
  @subtype = subtype
  @coder = coder
  super(subtype)
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder



10
11
12
# File 'activerecord/lib/active_record/type/serialized.rb', line 10

def coder
  @coder
end

#subtypeObject (readonly)

Returns the value of attribute subtype



10
11
12
# File 'activerecord/lib/active_record/type/serialized.rb', line 10

def subtype
  @subtype
end

Instance Method Details

#accessorObject



44
45
46
# File 'activerecord/lib/active_record/type/serialized.rb', line 44

def accessor
  ActiveRecord::Store::IndifferentHashAccessor
end

#assert_valid_value(value) ⇒ Object



48
49
50
51
52
# File 'activerecord/lib/active_record/type/serialized.rb', line 48

def assert_valid_value(value)
  if coder.respond_to?(:assert_valid_value)
    coder.assert_valid_value(value, action: "serialize")
  end
end

#changed_in_place?(raw_old_value, value) ⇒ Boolean

Returns:



37
38
39
40
41
42
# File 'activerecord/lib/active_record/type/serialized.rb', line 37

def changed_in_place?(raw_old_value, value)
  return false if value.nil?
  raw_new_value = encoded(value)
  raw_old_value.nil? != raw_new_value.nil? ||
    subtype.changed_in_place?(raw_old_value, raw_new_value)
end

#deserialize(value) ⇒ Object



18
19
20
21
22
23
24
# File 'activerecord/lib/active_record/type/serialized.rb', line 18

def deserialize(value)
  if default_value?(value)
    value
  else
    coder.load(super)
  end
end

#force_equality?(value) ⇒ Boolean

Returns:



54
55
56
# File 'activerecord/lib/active_record/type/serialized.rb', line 54

def force_equality?(value)
  coder.respond_to?(:object_class) && value.is_a?(coder.object_class)
end

#inspectObject



33
34
35
# File 'activerecord/lib/active_record/type/serialized.rb', line 33

def inspect
  Kernel.instance_method(:inspect).bind_call(self)
end

#serialize(value) ⇒ Object



26
27
28
29
30
31
# File 'activerecord/lib/active_record/type/serialized.rb', line 26

def serialize(value)
  return if value.nil?
  unless default_value?(value)
    super coder.dump(value)
  end
end