Sha256: 31ca86fd7e31dfe7dc3ba6e485e458ee5316d651735d17c7bd2c622dee3b1f4a

Contents?: true

Size: 1.02 KB

Versions: 5

Compression:

Stored size: 1.02 KB

Contents

# frozen_string_literal: true

module Micro
  module Cases
    class Map
      IsAUseCaseOrFlowWithDefaults = -> arg { arg.is_a?(Array) && Micro.case_or_flow?(arg[0]) && arg[1].is_a?(Hash) }
      IsAUseCaseOrFlow = -> arg { Micro.case_or_flow?(arg) || IsAUseCaseOrFlowWithDefaults[arg] }
      HasValidArgs = -> (args) { Kind.of(Array, args).all?(&IsAUseCaseOrFlow) }

      attr_reader :use_cases

      def self.build(args)
        raise Error::InvalidUseCases unless HasValidArgs[args]

        new(args)
      end

      def initialize(use_cases)
        @use_cases = use_cases
      end

      GetUseCaseResult = -> (hash) do
        -> (use_case) do
          return use_case.call(hash) unless use_case.is_a?(Array)

          use_case[0].call(hash.merge(use_case[1]))
        end
      end

      def call(arg = {})
        hash = Kind.of(Hash, arg)

        use_cases.map(&GetUseCaseResult[hash])
      end

      private_constant :HasValidArgs, :IsAUseCaseOrFlow, :IsAUseCaseOrFlowWithDefaults, :GetUseCaseResult
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
u-case-4.2.2 lib/micro/cases/map.rb
u-case-4.2.1 lib/micro/cases/map.rb
u-case-4.2.0 lib/micro/cases/map.rb
u-case-4.1.1 lib/micro/cases/map.rb
u-case-4.1.0 lib/micro/cases/map.rb