Sha256: d31d91f40c62a97a4005f4bda5aca1fc0ce890abb23bb52187046909166893f2

Contents?: true

Size: 1.42 KB

Versions: 10

Compression:

Stored size: 1.42 KB

Contents

module AttrJson

  # A little wrapper to provide an object that provides #dump and #load method for use
  # as a coder second-argument for [ActiveRecord Serialization](https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html),
  # 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.
  class SerializationCoderFromType
    attr_reader :type
    def initialize(type)
      @type = type
    end

    # Dump and load methods to support ActiveRecord Serialization
    # too.
    def dump(value)
      type.serialize(value)
    end

    # Dump and load methods to support ActiveRecord Serialization
    # too. https://api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/Serialization/ClassMethods.html
    def load(value)
      type.deserialize(value)
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
attr_json-2.2.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-2.1.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-2.0.1 lib/attr_json/serialization_coder_from_type.rb
attr_json-2.0.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-2.0.0.rc1 lib/attr_json/serialization_coder_from_type.rb
attr_json-1.5.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-1.4.1 lib/attr_json/serialization_coder_from_type.rb
attr_json-1.4.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-1.3.0 lib/attr_json/serialization_coder_from_type.rb
attr_json-1.2.0 lib/attr_json/serialization_coder_from_type.rb