Sha256: cf001f4e345855444cec7d84dc99faacdebb3077f42aad5690c9b0dadc2d416c

Contents?: true

Size: 1.76 KB

Versions: 12

Compression:

Stored size: 1.76 KB

Contents

module Quebert
  module AsyncSender
    # Decorates AsyncSender classes with an #async() proxy. This seperates
    # the concern of configuring job specific parameters, like :ttr, :delay, etc.
    # from calling the method.
    class Promise
      attr_reader :target, :opts, :job

      def initialize(target, opts={}, &block)
        @target, @opts, @block = target, opts, block
        self
      end

      # Proxies the method call from async to the target, then tries
      # to build a job with the targets `build_job` function.
      def method_missing(meth, *args)
        if @target.respond_to? meth, true # The second `true` argument checks private methods.
          # Create an instance of the job through the proxy and 
          # configure it with the options given to the proxy.
          @block.call configure @target.build_job(meth, *args)
        else
          super
        end
      end

      # Configures a job with the options provied to the Promise
      # upon initialization.
      def configure(job)
        @opts.each do |attr, val|
          job.send("#{attr}=", val)
        end
        job
      end

      # Methods/DSL that we mix into classes, objects, etc. so that we
      # can easily enqueue jobs to these.
      module DSL
        def async(opts={})
          Promise.new(self, opts) { |job| job.enqueue }
        end

        # Legacy way of enqueueing jobs.
        def async_send(*args)
          meth = args.shift
          beanstalk = args.last.delete(:beanstalk) if args.last.is_a?(::Hash)

          Quebert.deprecate "#async_send should be called via #{self.class.name}.async(#{beanstalk.inspect}).#{args.first}(#{args.map(&:inspect).join(', ')})" do
            async(beanstalk || {}).send(meth, *args)
          end
        end
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
quebert-3.3.0 lib/quebert/async_sender/promise.rb
quebert-3.2.1 lib/quebert/async_sender/promise.rb
quebert-3.2.0 lib/quebert/async_sender/promise.rb
quebert-3.0.3 lib/quebert/async_sender/promise.rb
quebert-3.0.2 lib/quebert/async_sender/promise.rb
quebert-3.0.1 lib/quebert/async_sender/promise.rb
quebert-3.0.0 lib/quebert/async_sender/promise.rb
quebert-2.0.4 lib/quebert/async_sender/promise.rb
quebert-2.0.3 lib/quebert/async_sender/promise.rb
quebert-2.0.2 lib/quebert/async_sender/promise.rb
quebert-2.0.1 lib/quebert/async_sender/promise.rb
quebert-2.0.0 lib/quebert/async_sender/promise.rb