Class: ActiveRecord::Type::Serialized

Inherits:
Value show all
Includes:
Decorator, Mutable
Defined in:
activerecord/lib/active_record/type/serialized.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Attributes inherited from Value

#limit, #precision, #scale

Instance Method Summary collapse

Methods included from Mutable

#type_cast_from_user

Methods inherited from Value

#==, #binary?, #changed?, #hash, #klass, #number?, #text?, #type, #type_cast_for_schema, #type_cast_from_user

Constructor Details

#initialize(subtype, coder) ⇒ Serialized

Returns a new instance of Serialized.


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

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

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder


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

def coder
  @coder
end

#subtypeObject (readonly)

Returns the value of attribute subtype


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

def subtype
  @subtype
end

Instance Method Details

#accessorObject


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

def accessor
  ActiveRecord::Store::IndifferentHashAccessor
end

#changed_in_place?(raw_old_value, value) ⇒ Boolean

Returns:


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

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

#encode_with(coder) ⇒ Object


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

def encode_with(coder)
  coder['coder'] = @coder
  super
end

#init_with(coder) ⇒ Object


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

def init_with(coder)
  @coder = coder['coder']
  super
end

#inspectObject


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

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

#type_cast_for_database(value) ⇒ Object


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

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

#type_cast_from_database(value) ⇒ Object


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

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