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.



12
13
14
15
16
# File '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 'lib/active_record/type/serialized.rb', line 10

def coder
  @coder
end

#subtypeObject (readonly)

Returns the value of attribute subtype.



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

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



18
19
20
21
22
23
24
# File '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:



52
53
54
# File 'lib/active_record/type/serialized.rb', line 52

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

#serialize(value) ⇒ Object



26
27
28
29
30
31
# File '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

#serialized?Boolean

:nodoc:

Returns:



56
57
58
# File 'lib/active_record/type/serialized.rb', line 56

def serialized? # :nodoc:
  true
end