Sha256: 5fcac4c8eb35ff65b659300853ba184f4509e932cbac6a76794ca962a660e97e

Contents?: true

Size: 1.63 KB

Versions: 4

Compression:

Stored size: 1.63 KB

Contents

# frozen_string_literal: true

module MiniKraken
  module Core
    # A wrapper around a "solver" object.
    # A solver in MiniKraken is Fiber or an object that quacks like
    # a Fiber in that its has a 'resume' method.
    # To be a solver, the adaptee object must yield a Context or nil.
    # A SolverAdapter implements a customized resume method that
    # acts as an "execute around" method for the adaptee's ´resume´ method.
    class SolverAdapter
      # @return [#resume] A Fiber-like object as adaptee
      attr_reader :adaptee

      # ->(adapter, aContext) do
        # aContext.push_bookmark(adapter.adaptee)
        # result = adapter.adaptee.resume
        # aContext.pop_bookmark
        # result
      # end
      # @return [Proc] lambda to execute when resume is called
      attr_reader :around_resume

      # Constructor.
      # @param fib [#resume] A Fiber-like object
      def initialize(fib, &blk)
        @adaptee = validated_adaptee(fib)
        @around_resume = blk if block_given?
      end

      # Update the bookmarks and resume the Fiber
      # @param aContext [Core::Context]
      # @return [Core::Context, NilClass]
      def resume(aContext)
        ctx = validated_ctx(aContext)
        if around_resume
          around_resume.call(self, ctx)
        else
          adaptee.resume
        end
      end

      private

      def validated_adaptee(fib)
        raise StandardError, 'No resume method' unless fib.respond_to?(:resume)

        fib
      end

      def validated_ctx(aContext)
        raise StandardError unless aContext.kind_of?(Context)

        aContext
      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/solver_adapter.rb
mini_kraken-0.3.02 lib/mini_kraken/core/solver_adapter.rb
mini_kraken-0.3.01 lib/mini_kraken/core/solver_adapter.rb
mini_kraken-0.3.00 lib/mini_kraken/core/solver_adapter.rb