Class: AttrJson::SerializationCoderFromType
- Inherits:
-
Object
- Object
- AttrJson::SerializationCoderFromType
- Defined in:
- lib/attr_json/serialization_coder_from_type.rb
Overview
A little wrapper to provide an object that provides #dump and #load method for use as a coder second-argument for ActiveRecord Serialization, that simply delegates to #serialize and #deserialize from a ActiveModel::Type object.
Created to be used with an AttrJson::Model type (AttrJson::Type::Model), but hypothetically could be a shim from anything with serialize/deserialize to dump/load instead.
class ValueModel include AttrJson::Model attr_json :some_string, :string end
class SomeModel < ApplicationRecord serialize :some_json_column, ValueModel.to_serialize_coder end
Note when used with an AttrJson::Model, it will dump/load from a HASH, not a string. It assumes it's writing to a Json(b) column that wants/provides hashes, not strings.
Instance Attribute Summary collapse
-
#type ⇒ Object
readonly
Returns the value of attribute type.
Instance Method Summary collapse
-
#dump(value) ⇒ Object
Dump and load methods to support ActiveRecord Serialization too.
-
#initialize(type) ⇒ SerializationCoderFromType
constructor
A new instance of SerializationCoderFromType.
-
#load(value) ⇒ Object
Dump and load methods to support ActiveRecord Serialization too.
Constructor Details
#initialize(type) ⇒ SerializationCoderFromType
Returns a new instance of SerializationCoderFromType.
26 27 28 |
# File 'lib/attr_json/serialization_coder_from_type.rb', line 26 def initialize(type) @type = type end |
Instance Attribute Details
#type ⇒ Object (readonly)
Returns the value of attribute type.
25 26 27 |
# File 'lib/attr_json/serialization_coder_from_type.rb', line 25 def type @type end |
Instance Method Details
#dump(value) ⇒ Object
Dump and load methods to support ActiveRecord Serialization too.
32 33 34 |
# File 'lib/attr_json/serialization_coder_from_type.rb', line 32 def dump(value) type.serialize(value) end |
#load(value) ⇒ Object
Dump and load methods to support ActiveRecord Serialization too. https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html
38 39 40 |
# File 'lib/attr_json/serialization_coder_from_type.rb', line 38 def load(value) type.deserialize(value) end |