Sha256: 3f3c49769c6155f5463ed03be03043114a18125e2d0a373efb87eaf19996d593

Contents?: true

Size: 1.5 KB

Versions: 38

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module ActiveSupport
  module Messages
    module Rotator # :nodoc:
      def initialize(*args, on_rotation: nil, **options)
        super(*args, **options)
        @args = args
        @options = options
        @rotations = []
        @on_rotation = on_rotation
      end

      def rotate(*args, **options)
        fall_back_to build_rotation(*args, **options)
      end

      def fall_back_to(fallback)
        @rotations << fallback
        self
      end

      def read_message(message, on_rotation: @on_rotation, **options)
        if @rotations.empty?
          super(message, **options)
        else
          thrown, error = catch_rotation_error do
            return super(message, **options)
          end

          @rotations.each do |rotation|
            catch_rotation_error do
              value = rotation.read_message(message, **options)
              on_rotation&.call
              return value
            end
          end

          throw thrown, error
        end
      end

      private
        def build_rotation(*args, **options)
          self.class.new(*args, *@args.drop(args.length), **@options, **options)
        end

        def catch_rotation_error(&block)
          error = catch :invalid_message_format do
            error = catch :invalid_message_serialization do
              return [nil, block.call]
            end
            return [:invalid_message_serialization, error]
          end
          [:invalid_message_format, error]
        end
    end
  end
end

Version data entries

38 entries across 38 versions & 6 rubygems

Version Path
activesupport-8.0.0 lib/active_support/messages/rotator.rb
activesupport-7.2.2 lib/active_support/messages/rotator.rb
activesupport-7.1.5 lib/active_support/messages/rotator.rb
activesupport-8.0.0.rc2 lib/active_support/messages/rotator.rb
activesupport-7.2.1.2 lib/active_support/messages/rotator.rb
activesupport-7.1.4.2 lib/active_support/messages/rotator.rb
activesupport-8.0.0.rc1 lib/active_support/messages/rotator.rb
activesupport-7.2.1.1 lib/active_support/messages/rotator.rb
activesupport-7.1.4.1 lib/active_support/messages/rotator.rb
activesupport-8.0.0.beta1 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha9 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha8 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha7 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha4 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha3 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha2 lib/active_support/messages/rotator.rb
omg-activesupport-8.0.0.alpha1 lib/active_support/messages/rotator.rb
activesupport-7.1.4 lib/active_support/messages/rotator.rb
activesupport-7.2.1 lib/active_support/messages/rotator.rb
activesupport-7.2.0 lib/active_support/messages/rotator.rb