Sha256: 71e5f46d44274ef9f8d16d7c35f73c82e69ed9993b437af3b7de1c284361c56e

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

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.04 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.1.03 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.1.02 lib/mini_kraken/core/duck_fiber.rb