Sha256: 1f46d02b18c60b062a16a59aae43ee1a343a6c5242c3fdd57f10f8ae23319ae4

Contents?: true

Size: 1.14 KB

Versions: 2

Compression:

Stored size: 1.14 KB

Contents

# frozen_string_literal: true

module ConvenientService
  module Service
    module Plugins
      module CanHaveSteps
        class Middleware < Core::MethodChainMiddleware
          ##
          # @return [ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step]
          #
          def next(...)
            return chain.next(...) if entity.steps.none?

            last_step.result.copy
          end

          private

          ##
          # @return [ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step]
          #
          def last_step
            @last_step ||= entity.steps.find.with_index { |step, index| not_success?(step, index) } || entity.steps.last
          end

          ##
          # @return [ConvenientService::Service::Plugins::CanHaveSteps::Entities::Step]
          #
          # @internal
          #   NOTE: `entity.step(index)` is used as a hook (callbacks trigger).
          #   IMPORTANT: `step` status MUST be checked before triggering callbacks.
          #
          def not_success?(step, index)
            step.not_success?.tap { entity.step(index) }
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
convenient_service-0.12.0 lib/convenient_service/service/plugins/can_have_steps/middleware.rb
convenient_service-0.11.0 lib/convenient_service/service/plugins/can_have_steps/middleware.rb