Sha256: 1e1529978e2a88c0af45903f38d67ffe6c660047bbc11ebb7efe3b0160311136

Contents?: true

Size: 848 Bytes

Versions: 2

Compression:

Stored size: 848 Bytes

Contents

module BloodContracts
  module Storages
    class Serializer
      extend Dry::Initializer

      param :serializer

      def self.call(*args)
        new(*args).call
      end

      def call
        return object_serializer_to_hash if object_serializer?
        return serializer.to_hash if hash_serializer?

        raise "Both #dump and #load methods should be defined for serialization"
      end

      private

      def object_serializer?
        serializer.respond_to?(:dump) && serializer.respond_to?(:load)
      end

      def object_serializer_to_hash
        {
          load: serializer.method(:load),
          dump: serializer.method(:dump),
        }
      end

      def hash_serializer?
        return unless serializer.respond_to?(:to_hash)
        (%i[dump load] - serializer.to_hash.keys).empty?
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
blood_contracts-0.2.1 lib/blood_contracts/storages/serializer.rb
blood_contracts-0.2.0 lib/blood_contracts/storages/serializer.rb