Class: ActiveRecord::Coders::JSONColumn
- Inherits:
-
Object
- Object
- ActiveRecord::Coders::JSONColumn
- Defined in:
- lib/serialize-rails/coders/json_column.rb
Overview
:nodoc:
Constant Summary collapse
- RESCUE_ERRORS =
[ ArgumentError, JSON::JSONError ]
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) ⇒ JSONColumn
constructor
A new instance of JSONColumn.
- #load(json) ⇒ Object
Constructor Details
#initialize(object_class = Object) ⇒ JSONColumn
Returns a new instance of JSONColumn.
10 11 12 |
# File 'lib/serialize-rails/coders/json_column.rb', line 10 def initialize(object_class = Object) @object_class = object_class end |
Instance Attribute Details
#object_class ⇒ Object
Returns the value of attribute object_class.
8 9 10 |
# File 'lib/serialize-rails/coders/json_column.rb', line 8 def object_class @object_class end |
Instance Method Details
#dump(obj) ⇒ Object
14 15 16 17 18 19 20 21 22 |
# File 'lib/serialize-rails/coders/json_column.rb', line 14 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 JSON.dump obj end |
#load(json) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/serialize-rails/coders/json_column.rb', line 24 def load(json) return object_class.new if object_class != Object && json.nil? return json unless json.is_a?(String) begin obj = JSON.load(json) 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 json end end |