Sha256: 5aba8f9655d83a3c02ea52dbd6363f954583f748065f9062564996fdda87be14

Contents?: true

Size: 1.3 KB

Versions: 4

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

require_relative 'context'

module MiniKraken
  module Core
    # A mock class that mimicks the behavior of a Fiber instance.
    # More specifically, it responds to `resume` message & returns a Context.
    class DuckFiber
      # @return [Proc, #call] The callable object to yield.
      attr_reader :callable

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

      # Constructor.
      # @param aCallable [Proc, #call] The receiver of the 'call' message.
      def initialize(aCallable)
        @callable = valid_callable(aCallable)
        @state = :initial
      end

      # Quacks like a Fiber object.
      # The first time, this method will return a Context objet.
      # Subsequents calls just return nil (= no other solution available)
      # @return [Core::Context, NilClass]
      def resume(*_args)
        if state == :initial
          @state = :yielded
          return callable.call
        else
          return nil
        end
      end

      private

      def valid_callable(aCallable)
        unless aCallable.kind_of?(Proc) || aCallable.respond_to?(:call)
          err_msg = "Expected a Proc instead of #{aCallable.class}."
          raise StandardError, err_msg
        end

        aCallable
      end
    end # class
  end # module
end # module

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
mini_kraken-0.3.03 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.3.02 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.3.01 lib/mini_kraken/core/duck_fiber.rb
mini_kraken-0.3.00 lib/mini_kraken/core/duck_fiber.rb