Sha256: dd8d81a037c1fd9a89f1747bfd58e1f546c9aff2f38cd804fc26c1572bb3fcb8

Contents?: true

Size: 1.47 KB

Versions: 8

Compression:

Stored size: 1.47 KB

Contents

# Example
#     class Processor
#       include Magent::Async
#       [..snip..]
#
#       def process
#         puts "Processing #{@params}"
#       end
#     end
#
#     async call:
#         Processor.find(id).async(:my_queue).process.commit!(priority)
#     chained methods:
#         Processor.async(:my_queue, true).find(1).process.commit!(priority)
#

module Magent
  module Async
    def self.included(base)
      base.class_eval do
        include Methods
        extend Methods
      end
    end

    class Proxy
      instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|proxy_|^object_id$)/ }

      def initialize(queue, target, test = false)
        @queue = queue
        @method_chain = []
        @target = target
        @test = test

        @channel = Magent::AsyncChannel.new(@queue)
      end

      def commit!(priority = 3)
        @channel.push(@target, @method_chain, priority)

        if @test
          target = @target
          @method_chain.each do |c|
            target = target.send(c[0], *c[1])
          end

          target
        end
      end

      def method_missing(m, *args, &blk)
        raise ArgumentError, "ruby blocks are not supported yet" if !blk.nil?
        @method_chain << [m, args]
        self
      end
    end

    module Methods
      # @question.async(:judge).on_view_question.commit!(1)
      def async(queue = :default, test = false)
        Magent::Async::Proxy.new(queue, self, test)
      end
    end
  end # Async
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
magent-0.6.2 lib/magent/async.rb
magent-0.6.1 lib/magent/async.rb
magent-0.6.0 lib/magent/async.rb
magent-0.5.4 lib/magent/async.rb
magent-0.5.3 lib/magent/async.rb
magent-0.5.2 lib/magent/async.rb
magent-0.5.1 lib/magent/async.rb
magent-0.5.0 lib/magent/async.rb