Sha256: bc0f67cdd2290974f5ee0a43ed66cc3faa69478cab1a55eb1e5b99437897e637

Contents?: true

Size: 1.3 KB

Versions: 17

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module ActiveJob
  module Serializers
    # Base class for serializing and deserializing custom objects.
    #
    # Example:
    #
    #   class MoneySerializer < ActiveJob::Serializers::ObjectSerializer
    #     def serialize(money)
    #       super("amount" => money.amount, "currency" => money.currency)
    #     end
    #
    #     def deserialize(hash)
    #       Money.new(hash["amount"], hash["currency"])
    #     end
    #
    #     private
    #
    #       def klass
    #         Money
    #       end
    #   end
    class ObjectSerializer
      include Singleton

      class << self
        delegate :serialize?, :serialize, :deserialize, to: :instance
      end

      # Determines if an argument should be serialized by a serializer.
      def serialize?(argument)
        argument.is_a?(klass)
      end

      # Serializes an argument to a JSON primitive type.
      def serialize(hash)
        { Arguments::OBJECT_SERIALIZER_KEY => self.class.name }.merge!(hash)
      end

      # Deserializes an argument from a JSON primitive type.
      def deserialize(_argument)
        raise NotImplementedError
      end

      private

        # The class of the object that will be serialized.
        def klass # :doc:
          raise NotImplementedError
        end
    end
  end
end

Version data entries

17 entries across 17 versions & 4 rubygems

Version Path
activejob-6.0.2.2 lib/active_job/serializers/object_serializer.rb
argon-1.3.1 vendor/bundle/ruby/2.7.0/gems/activejob-6.0.2.1/lib/active_job/serializers/object_serializer.rb
symbolic_enum-1.1.5 vendor/bundle/ruby/2.7.0/gems/activejob-6.0.2.1/lib/active_job/serializers/object_serializer.rb
activejob-6.0.2.1 lib/active_job/serializers/object_serializer.rb
activejob-6.0.2 lib/active_job/serializers/object_serializer.rb
activejob-6.0.2.rc2 lib/active_job/serializers/object_serializer.rb
activejob-6.0.2.rc1 lib/active_job/serializers/object_serializer.rb
activejob-6.0.1 lib/active_job/serializers/object_serializer.rb
chatops-rpc-0.0.2 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activejob-6.0.0/lib/active_job/serializers/object_serializer.rb
activejob-6.0.1.rc1 lib/active_job/serializers/object_serializer.rb
chatops-rpc-0.0.1 fixtures/chatops-controller-example/vendor/bundle/ruby/2.5.0/gems/activejob-6.0.0/lib/active_job/serializers/object_serializer.rb
activejob-6.0.0 lib/active_job/serializers/object_serializer.rb
activejob-6.0.0.rc2 lib/active_job/serializers/object_serializer.rb
activejob-6.0.0.rc1 lib/active_job/serializers/object_serializer.rb
activejob-6.0.0.beta3 lib/active_job/serializers/object_serializer.rb
activejob-6.0.0.beta2 lib/active_job/serializers/object_serializer.rb
activejob-6.0.0.beta1 lib/active_job/serializers/object_serializer.rb