Class: Quebert::AsyncSender::Promise

Inherits:
Object
  • Object
show all
Defined in:
lib/quebert/async_sender/promise.rb

Overview

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

Defined Under Namespace

Modules: DSL

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target, opts = {}, &block) ⇒ Promise

Returns a new instance of Promise.



9
10
11
12
# File 'lib/quebert/async_sender/promise.rb', line 9

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Proxies the method call from async to the target, then tries to build a job with the targets ‘build_job` function.



16
17
18
19
20
21
22
23
24
# File 'lib/quebert/async_sender/promise.rb', line 16

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

Instance Attribute Details

#jobObject (readonly)

Returns the value of attribute job.



7
8
9
# File 'lib/quebert/async_sender/promise.rb', line 7

def job
  @job
end

#optsObject (readonly)

Returns the value of attribute opts.



7
8
9
# File 'lib/quebert/async_sender/promise.rb', line 7

def opts
  @opts
end

#targetObject (readonly)

Returns the value of attribute target.



7
8
9
# File 'lib/quebert/async_sender/promise.rb', line 7

def target
  @target
end

Instance Method Details

#configure(job) ⇒ Object

Configures a job with the options provied to the Promise upon initialization.



28
29
30
31
32
33
# File 'lib/quebert/async_sender/promise.rb', line 28

def configure(job)
  @opts.each do |attr, val|
    job.send("#{attr}=", val)
  end
  job
end