Class: ActiveRecord::Type::Serialized

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

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subtype, coder) ⇒ Serialized

Returns a new instance of Serialized.



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

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

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder.



8
9
10
# File 'lib/active_record/type/serialized.rb', line 8

def coder
  @coder
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



8
9
10
# File 'lib/active_record/type/serialized.rb', line 8

def subtype
  @subtype
end

Instance Method Details

#accessorObject



42
43
44
# File 'lib/active_record/type/serialized.rb', line 42

def accessor
  ActiveRecord::Store::IndifferentHashAccessor
end

#assert_valid_value(value) ⇒ Object



46
47
48
49
50
# File 'lib/active_record/type/serialized.rb', line 46

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:



35
36
37
38
39
40
# File 'lib/active_record/type/serialized.rb', line 35

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



16
17
18
19
20
21
22
# File 'lib/active_record/type/serialized.rb', line 16

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

#inspectObject



31
32
33
# File 'lib/active_record/type/serialized.rb', line 31

def inspect
  Kernel.instance_method(:inspect).bind(self).call
end

#serialize(value) ⇒ Object



24
25
26
27
28
29
# File 'lib/active_record/type/serialized.rb', line 24

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