Class: ResqueDelayable::DelayedMethod

Inherits:
Object
  • Object
show all
Defined in:
lib/resque-delayable/delayed_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(klass, instance = nil, options = {}) ⇒ DelayedMethod

Returns a new instance of DelayedMethod.



14
15
16
17
18
19
20
21
22
# File 'lib/resque-delayable/delayed_method.rb', line 14

def initialize(klass, instance = nil, options = {})
  unless instance.nil? || ResqueDelayable::ACTIVE_SERIALIZERS.any? {|klass| klass.serialize_match(instance) }
    raise "Cannot serialize instances of this type (#{instance.class}). Consider creating a serializer."
  end

  self.klass = klass
  self.instance = instance
  self.options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *parameters) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/resque-delayable/delayed_method.rb', line 28

def method_missing(method, *parameters)
  if instance.nil?
    raise NoMethodError.new("undefined method '#{method}' for #{self.klass}", method, parameters) unless klass.respond_to?(method)
  else
    raise NoMethodError.new("undefined method '#{method}' for #{self.instance}", method, parameters) unless instance.respond_to?(method)
  end

  if self.options[:run_at]
    if ::Resque.respond_to?(:enqueue_at)
      ::Resque.enqueue_at(self.options[:run_at], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
    else
      raise "Cannot use run_at, resque-scheduler not installed"
    end
  elsif self.options[:run_in]
    if ::Resque.respond_to?(:enqueue_in)
      ::Resque.enqueue_in(self.options[:run_in], self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
    else
      raise "Cannot use run_in, resque-scheduler not installed"
    end
  else
    ::Resque.enqueue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
  end

  true
end

Instance Attribute Details

#instanceObject

Returns the value of attribute instance.



11
12
13
# File 'lib/resque-delayable/delayed_method.rb', line 11

def instance
  @instance
end

#klassObject

Returns the value of attribute klass.



10
11
12
# File 'lib/resque-delayable/delayed_method.rb', line 10

def klass
  @klass
end

#optionsObject

Returns the value of attribute options.



12
13
14
# File 'lib/resque-delayable/delayed_method.rb', line 12

def options
  @options
end

Instance Method Details

#dequeue(method, *parameters) ⇒ Object



24
25
26
# File 'lib/resque-delayable/delayed_method.rb', line 24

def dequeue(method, *parameters)
  ::Resque.dequeue(self.klass, method, ::ResqueDelayable::serialize_object(self.instance), *::ResqueDelayable::serialize_object(parameters))
end