Sha256: a46624c9e596ffde5709834a31bdb620eaad2271d7b699faeff22572873547d3

Contents?: true

Size: 1.86 KB

Versions: 28

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/class/attribute"
require_relative "metadata"
require_relative "serializer_with_fallback"

module ActiveSupport
  module Messages # :nodoc:
    class Codec # :nodoc:
      include Metadata

      class_attribute :default_serializer, default: :marshal,
        instance_accessor: false, instance_predicate: false

      def initialize(**options)
        @serializer = options[:serializer] || self.class.default_serializer
        @serializer = SerializerWithFallback[@serializer] if @serializer.is_a?(Symbol)
        @url_safe = options[:url_safe]
        @force_legacy_metadata_serializer = options[:force_legacy_metadata_serializer]
      end

      private
        attr_reader :serializer

        def encode(data, url_safe: @url_safe)
          url_safe ? ::Base64.urlsafe_encode64(data, padding: false) : ::Base64.strict_encode64(data)
        end

        def decode(encoded, url_safe: @url_safe)
          url_safe ? ::Base64.urlsafe_decode64(encoded) : ::Base64.strict_decode64(encoded)
        rescue StandardError => error
          throw :invalid_message_format, error
        end

        def serialize(data)
          serializer.dump(data)
        end

        def deserialize(serialized)
          serializer.load(serialized)
        rescue StandardError => error
          throw :invalid_message_serialization, error
        end

        def catch_and_ignore(throwable, &block)
          catch throwable do
            return block.call
          end
          nil
        end

        def catch_and_raise(throwable, as: nil, &block)
          error = catch throwable do
            return block.call
          end
          error = as.new(error.to_s) if as
          raise error
        end

        def use_message_serializer_for_metadata?
          !@force_legacy_metadata_serializer && super
        end
    end
  end
end

Version data entries

28 entries across 28 versions & 2 rubygems

Version Path
activesupport-8.0.1 lib/active_support/messages/codec.rb
activesupport-8.0.0.1 lib/active_support/messages/codec.rb
activesupport-7.2.2.1 lib/active_support/messages/codec.rb
activesupport-7.1.5.1 lib/active_support/messages/codec.rb
activesupport-8.0.0 lib/active_support/messages/codec.rb
activesupport-7.2.2 lib/active_support/messages/codec.rb
activesupport-7.1.5 lib/active_support/messages/codec.rb
activesupport-8.0.0.rc2 lib/active_support/messages/codec.rb
activesupport-7.2.1.2 lib/active_support/messages/codec.rb
activesupport-7.1.4.2 lib/active_support/messages/codec.rb
activesupport-8.0.0.rc1 lib/active_support/messages/codec.rb
activesupport-7.2.1.1 lib/active_support/messages/codec.rb
activesupport-7.1.4.1 lib/active_support/messages/codec.rb
activesupport-8.0.0.beta1 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/messages/codec.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/messages/codec.rb