123456789_123456789_123456789_123456789_123456789_

Class: ActiveRecord::Coders::ColumnSerializer

Do not use. This class is for internal use only.
Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Subclasses:
Inherits: Object
Defined in: activerecord/lib/active_record/coders/column_serializer.rb

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(attr_name, coder, object_class = Object) ⇒ ColumnSerializer

[ GitHub ]

  
# 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

#coder (readonly)

[ GitHub ]

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

attr_reader :coder

#object_class (readonly)

[ GitHub ]

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

attr_reader :object_class

Instance Method Details

#assert_valid_value(object, action:)

Public because it’s called by ::ActiveRecord::Type::Serialized

[ GitHub ]

  
# 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

#check_arity_of_constructor (private)

[ GitHub ]

  
# File 'activerecord/lib/active_record/coders/column_serializer.rb', line 54

def check_arity_of_constructor
  load(nil)
rescue ArgumentError
  raise ArgumentError, "Cannot serialize #{object_class}. Classes passed to `serialize` must have a 0 argument constructor."
end

#dump(object)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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)

[ GitHub ]

  
# 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