Class: ActiveRecord::Coders::ColumnSerializer

Inherits:
Object
  • Object
show all
Defined in:
activerecord/lib/active_record/coders/column_serializer.rb

Overview

:nodoc:

Direct Known Subclasses

YAMLColumn

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attr_name, coder, object_class = Object) ⇒ ColumnSerializer

Returns a new instance of ColumnSerializer.



9
10
11
12
13
14
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 9

def initialize(attr_name, coder, object_class = Object)
  @attr_name = attr_name
  @object_class = object_class
  @coder = coder
  check_arity_of_constructor
end

Instance Attribute Details

#coderObject (readonly)

Returns the value of attribute coder



7
8
9
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 7

def coder
  @coder
end

#object_classObject (readonly)

Returns the value of attribute object_class



6
7
8
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 6

def object_class
  @object_class
end

Instance Method Details

#assert_valid_value(object, action:) ⇒ Object

Public because it’s called by Type::Serialized



46
47
48
49
50
51
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 46

def assert_valid_value(object, action:)
  unless object.nil? || object_class === object
    raise SerializationTypeMismatch,
      "can't #{action} `#{@attr_name}`: was supposed to be a #{object_class}, but was a #{object.class}. -- #{object.inspect}"
  end
end

#dump(object) ⇒ Object



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

def dump(object)
  return if object.nil?

  assert_valid_value(object, action: "dump")
  coder.dump(object)
end

#init_with(coder) ⇒ Object

:nodoc:



16
17
18
19
20
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 16

def init_with(coder) # :nodoc:
  @attr_name = coder["attr_name"]
  @object_class = coder["object_class"]
  @coder = coder["coder"]
end

#load(payload) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 29

def load(payload)
  if payload.nil?
    if @object_class != ::Object
      return @object_class.new
    end
    return nil
  end

  object = coder.load(payload)

  assert_valid_value(object, action: "load")
  object ||= object_class.new if object_class != Object

  object
end