Sha256: 992db1edc989d1984a73bd312f9c1bea37295a00244210317d1cfe1086ade4e4

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

# frozen_string_literal: true

module Micro
  class Case
    class Safe
      module Flow
        def self.included(base)
          base.send(:include, ::Micro::Case::Flow)

          def base.flow_reducer; Reducer; end
        end

        def self.Flow(args)
          Reducer.build(Array(args))
        end

        class Reducer < ::Micro::Case::Flow::Reducer
          def call(arg = {})
            @use_cases.reduce(initial_result(arg)) do |result, use_case|
              break result if result.failure?

              use_case_result(use_case, result)
            end
          end

          alias_method :&, :>>

          def >>(arg)
            raise NoMethodError, "undefined method `>>' for #{self.inspect}. Please, use the method `&' to avoid this error."
          end

          private

            def use_case_result(use_case, result)
              begin
                instance = use_case.__new__(result, result.value)
                instance.call
              rescue => exception
                raise exception if Error::ByWrongUsage.check(exception)

                result.__set__(false, exception, :exception, instance)
              end
            end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
u-case-2.0.0.pre.2 lib/micro/case/safe/flow.rb