Sha256: 7bdcc23d27a172ebf49744633ddc4b1cf28c4c80006230d5670ef813f51a2af2

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

# frozen_string_literal: true

module BCDD
  class Process
    module Caller
      PrepareInputs = ->(spec, input) do
        spec.each_with_object({}) do |(name, options), result|
          value = input.fetch(name) do
            if options.key?(:default)
              default = options[:default]

              default.is_a?(::Proc) ? default.call : default
            end
          end

          value = options[:normalize].call(value) if options.key?(:normalize)

          result[name] = options.key?(:contract) ? options[:contract][value] : Contract.null(value)
        end
      end

      def call(**inputs)
        prepared_input = PrepareInputs[self.class.__input__, inputs]

        Result.transitions(name: self.class.name) do
          if self.class.__input_contract__
            invalid_input = prepared_input.select { |_key, value| value.invalid? }

            return Failure(:invalid_input, **invalid_input.transform_values(&:errors)) unless invalid_input.empty?
          end

          super(**prepared_input.transform_values!(&:value))
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bcdd-process-0.3.1 lib/bcdd/process/caller.rb
bcdd-process-0.3.0 lib/bcdd/process/caller.rb