Sha256: 6b63a5f861db138300ba15dbe5423e636ee70f3f59798eb95400b6a6d5f8f79f

Contents?: true

Size: 1.37 KB

Versions: 1

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

require 'bcdd/result'

require_relative 'ext/contract'
require_relative 'ext/data'

require_relative 'process/version'
require_relative 'process/output'

module BCDD
  class Process
    module Caller
      def call(**kargs)
        input = self.class::Input.new(**kargs)

        Result.transitions(name: self.class.name) do
          return Failure(:invalid_input, **input.errors) if input.errors.any?

          super(**input.attributes)
        end
      end
    end

    def self.input(&block)
      return if const_defined?(:Input)

      const_set(:Input, ::BCDD::Data.new(&block))
    end

    RESULT_CONFIG = { addon: { given: true, continue: true }.freeze }.freeze

    def self.output(expectations: true, &block)
      return if const_defined?(:Result, false)

      if expectations
        evaluator = Output::Evaluator.new
        evaluator.instance_eval(&block)

        success = evaluator.__properties__.success
        failure = evaluator.__properties__.failure

        include(Result::Context::Expectations.mixin(config: RESULT_CONFIG, success: success, failure: failure))
      else
        include(Result::Context.mixin(config: RESULT_CONFIG))
      end
    end

    def self.inherited(subclass)
      subclass.prepend(Caller)
    end

    def self.call(**input)
      new.call(**input)
    end

    private_constant :Caller, :RESULT_CONFIG
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
bcdd-process-0.4.0 lib/bcdd/process.rb