Sha256: 5a91a8bf13dde64e3ffd4b6b94048ca3107ed657efb545d2440e1567d343094b

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

require_relative 'outcome'

module MiniKraken
  module Core
    # A mock class that mimicks the behavior of a Fiber instance.
    class DuckFiber
      # @return [Outcome] The sole outcome to yield.
      attr_reader :outcome

      # @return [Symbol] one of: :initial, :yielded
      attr_reader :state

      # @param outcomeKind [Symbol] One of: :failure, :basic_success, :custom
      def initialize(outcomeKind, &customization)
        @state = :initial
        if outcomeKind == :custom && block_given?
          @outcome = customization.call
        else
          @outcome = valid_outcome(outcomeKind)
        end
      end

      def resume(*_args)
        if state == :initial
          @state = :yielded
          return outcome
        else
          return nil
        end
      end

      def valid_outcome(outcomeKind)
        case outcomeKind
          when :failure
            Failure
          when :success
            BasicSuccess
          else
            raise StandardError, "Unknonw outcome kind #{outcomeKind}"
        end
      end
    end # class
  end # module
end # module

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_kraken-0.1.07 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.1.06 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.1.05 lib/mini_kraken/core/duck_fiber.rb