Sha256: cd04f75cbf03b51f583a0e8bfb5a1e884b5941b25a5215d5927bfc7988cf3297

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module LightServiceExt
  class RecordActions
    extend WithErrorHandler

    def self.call(context)
      with_error_handler(ctx: context) do
        self.before_execute_block.call(context)

        result = yield || context

        self.after_execute_block.call(context)
        self.after_success_block.call(context) if result.success?
        self.after_failure_block.call(context) if result.failure?

        return context if outcomes_complete?(ctx: context, result: result)
        merge_api_responses!(ctx: context, result: result)
      end
    end

    class << self
      attr_writer :before_execute_block, :after_execute_block, :after_success_block, :after_failure_block

      def before_execute_block
        @before_execute_block ||= ->(_context) {}
      end

      def after_execute_block
        @after_execute_block ||= ->(_context) {}
      end

      def after_success_block
        @after_success_block ||= ->(_context) {}
      end

      def after_failure_block
        @after_failure_block ||= ->(_context) {}
      end
      def merge_api_responses!(ctx:, result:)
        invoked_action = result.invoked_action
        return if invoked_action.nil?

        ctx.add_to_successful_actions(invoked_action.name)
        ctx.add_to_api_responses(result.current_api_response)
        ctx
      end

      def outcomes_complete?(ctx:, result:)
        if result.status == Status::COMPLETE
          ctx.add_status(result.status)

          if ctx.errors.present?
            ctx.add_last_failed_context(result.to_h)
            ctx.fail!
          end
          true
        else
          false
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
light-service-ext-0.1.10 lib/light-service-ext/record_actions.rb