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

Methods inherited from ActiveModel::Type::Value

#==, #binary?, #cast, #changed?, #hash, #map, #type, #type_cast_for_schema

Constructor Details

#initialize(subtype, coder) ⇒ Serialized

Returns a new instance of Serialized.


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

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

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder


6
7
8
# File 'activerecord/lib/active_record/type/serialized.rb', line 6

def coder
  @coder
end

#subtypeObject (readonly)

Returns the value of attribute subtype


6
7
8
# File 'activerecord/lib/active_record/type/serialized.rb', line 6

def subtype
  @subtype
end

Instance Method Details

#accessorObject


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

def accessor
  ActiveRecord::Store::IndifferentHashAccessor
end

#assert_valid_value(value) ⇒ Object


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

def assert_valid_value(value)
  if coder.respond_to?(:assert_valid_value)
    coder.assert_valid_value(value)
  end
end

#changed_in_place?(raw_old_value, value) ⇒ Boolean

Returns:


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

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


14
15
16
17
18
19
20
# File 'activerecord/lib/active_record/type/serialized.rb', line 14

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

#inspectObject


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

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

#serialize(value) ⇒ Object


22
23
24
25
26
27
# File 'activerecord/lib/active_record/type/serialized.rb', line 22

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