Class: ActiveRecord::Coders::MarshalColumn
- Inherits:
-
Object
- Object
- ActiveRecord::Coders::MarshalColumn
- Defined in:
- lib/serialize-rails/coders/marshal_column.rb
Overview
:nodoc:
Constant Summary collapse
- RESCUE_ERRORS =
[ ArgumentError, TypeError ]
Instance Attribute Summary collapse
-
#object_class ⇒ Object
Returns the value of attribute object_class.
Instance Method Summary collapse
- #dump(obj) ⇒ Object
-
#initialize(object_class = Object) ⇒ MarshalColumn
constructor
A new instance of MarshalColumn.
- #load(data) ⇒ Object
Constructor Details
#initialize(object_class = Object) ⇒ MarshalColumn
Returns a new instance of MarshalColumn.
8 9 10 |
# File 'lib/serialize-rails/coders/marshal_column.rb', line 8 def initialize(object_class = Object) @object_class = object_class end |
Instance Attribute Details
#object_class ⇒ Object
Returns the value of attribute object_class.
6 7 8 |
# File 'lib/serialize-rails/coders/marshal_column.rb', line 6 def object_class @object_class end |
Instance Method Details
#dump(obj) ⇒ Object
12 13 14 15 16 17 18 19 20 |
# File 'lib/serialize-rails/coders/marshal_column.rb', line 12 def dump(obj) return if obj.nil? unless obj.is_a?(object_class) raise SerializationTypeMismatch, "Attribute was supposed to be a #{object_class}, but was a #{obj.class}. -- #{obj.inspect}" end Marshal.dump obj end |
#load(data) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/serialize-rails/coders/marshal_column.rb', line 22 def load(data) return object_class.new if object_class != Object && data.nil? return data unless data.is_a?(String) begin obj = Marshal.load(data) unless obj.is_a?(object_class) || obj.nil? raise SerializationTypeMismatch, "Attribute was supposed to be a #{object_class}, but was a #{obj.class}" end obj ||= object_class.new if object_class != Object obj rescue *RESCUE_ERRORS data end end |