Class: ResqueExtensions::AsyncMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/resque_extensions/async_method.rb

Constant Summary collapse

OPTIONAL_SETTINGS =
[:queue]
ACTIVERECORD_PREFIX =
"_ActiveRecord::"
CLASS_PREFIX =
"_Class::"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(caller, method, *args) ⇒ AsyncMethod

Constructor

Parameters:

  • caller (Object)

    The class or instance that is doing work

  • method (String, Symbol)

    The method we are calling

  • args

    Additional arguments we pass in



20
21
22
23
24
25
26
27
# File 'lib/resque_extensions/async_method.rb', line 20

def initialize(caller, method, *args)
  @caller = caller
  @method = method
  # set up our options
  self.set_options(args)
  # leftover args are assigned
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args.



10
11
12
# File 'lib/resque_extensions/async_method.rb', line 10

def args
  @args
end

Class Method Details

.perform(*args) ⇒ Object



12
13
14
# File 'lib/resque_extensions/async_method.rb', line 12

def self.perform(*args)
  self.new(*self.reify_data(args)).perform
end

Instance Method Details

#class_method?Boolean

Is the caller a class or an instance of a class

Returns:

  • (Boolean)


31
32
33
# File 'lib/resque_extensions/async_method.rb', line 31

def class_method?
  @caller.is_a?(Class)
end

#enqueue!Object

enqueue the job so that it can be performed



37
38
39
40
41
# File 'lib/resque_extensions/async_method.rb', line 37

def enqueue!
  Resque::Job.create(
    self.queue, self.class, *self.data_to_enqueue
  )
end

#instance_method?Boolean

Is the caller an instance or a class

Returns:

  • (Boolean)


44
45
46
# File 'lib/resque_extensions/async_method.rb', line 44

def instance_method?
  !self.class_method?
end

#performObject

Run our method



49
50
51
# File 'lib/resque_extensions/async_method.rb', line 49

def perform
  @caller.send(@method, *@args)
end

#queueObject

the queue for this job



54
55
56
# File 'lib/resque_extensions/async_method.rb', line 54

def queue
  @queue ||= "default"
end