Sha256: 2a07a0837993cfb7ef5f01e498c7d03225170ac26fda2712fb29ef6b7f05c9e9

Contents?: true

Size: 1.3 KB

Versions: 93

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(json)
        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

93 entries across 88 versions & 8 rubygems

Version Path
minato_ruby_api_client-0.2.2 vendor/bundle/ruby/3.2.0/gems/activejob-7.1.3.4/lib/active_job/serializers/object_serializer.rb
activejob-7.1.5.1 lib/active_job/serializers/object_serializer.rb
activejob-7.0.8.7 lib/active_job/serializers/object_serializer.rb
activejob-7.1.5 lib/active_job/serializers/object_serializer.rb
activejob-7.1.4.2 lib/active_job/serializers/object_serializer.rb
activejob-7.0.8.6 lib/active_job/serializers/object_serializer.rb
activejob-6.1.7.10 lib/active_job/serializers/object_serializer.rb
activejob-6.1.7.9 lib/active_job/serializers/object_serializer.rb
activejob-7.1.4.1 lib/active_job/serializers/object_serializer.rb
activejob-7.0.8.5 lib/active_job/serializers/object_serializer.rb
activejob-7.1.4 lib/active_job/serializers/object_serializer.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activejob-7.0.8.4/lib/active_job/serializers/object_serializer.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activejob-7.1.3.4/lib/active_job/serializers/object_serializer.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activejob-7.0.5.1/lib/active_job/serializers/object_serializer.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activejob-7.0.5.1/lib/active_job/serializers/object_serializer.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activejob-7.0.5.1/lib/active_job/serializers/object_serializer.rb
tinymce-rails-7.1.2 vendor/bundle/ruby/3.3.0/gems/activejob-7.1.3.4/lib/active_job/serializers/object_serializer.rb
activejob-7.1.3.4 lib/active_job/serializers/object_serializer.rb
activejob-7.0.8.4 lib/active_job/serializers/object_serializer.rb
activejob-6.1.7.8 lib/active_job/serializers/object_serializer.rb