Sha256: 7b0ee6c81f2bc90138142e63f4c240f5a3ae0388ca7d74fc0078a777db1685f9

Contents?: true

Size: 1.27 KB

Versions: 4

Compression:

Stored size: 1.27 KB

Contents

module Asynchronic
  module QueueEngine
    class Synchronic

      attr_reader :stubs

      def initialize(options={})
        @environment = options[:environment]
        @stubs = {}
      end

      def default_queue
        Asynchronic.default_queue
      end

      def environment
        @environment ||= Asynchronic.environment
      end

      def [](name)
        Queue.new self
      end

      def stub(job, &block)
        @stubs[job] = block
      end

      def asynchronic?
        false
      end

      class Queue

        def initialize(engine)
          @engine = engine
        end

        def push(message)
          process = @engine.environment.load_process(message)

          if @engine.stubs[process.type]
            job = process.job
            block = @engine.stubs[process.type]
            process.define_singleton_method :job do
              MockJob.new job, process, &block
            end
          end

          process.execute
        end

      end


      class MockJob < TransparentProxy

        def initialize(job, process, &block)
          super job
          @process = process
          @block = block
        end

        def call
          @block.call @process
        end

        def before_finalize
        end

      end

    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
asynchronic-1.6.3 lib/asynchronic/queue_engine/synchronic.rb
asynchronic-1.6.2 lib/asynchronic/queue_engine/synchronic.rb
asynchronic-1.6.1 lib/asynchronic/queue_engine/synchronic.rb
asynchronic-1.6.0 lib/asynchronic/queue_engine/synchronic.rb