Class: Delayed::PerformableMethod

Inherits:
Struct
  • Object
show all
Defined in:
lib/delayed/performable_method.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, method, args: [], kwargs: {}, on_failure: nil, on_permanent_failure: nil, sender: nil) ⇒ PerformableMethod

Returns a new instance of PerformableMethod.

Raises:

  • (NoMethodError)


13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/delayed/performable_method.rb', line 13

def initialize(object, method, args: [], kwargs: {}, on_failure: nil, on_permanent_failure: nil, sender: nil)
  raise NoMethodError, "undefined method `#{method}' for #{object.inspect}" unless object.respond_to?(method, true)

  self.object = object
  self.args   = args
  self.kwargs = kwargs
  self.method = method.to_sym
  self.fail_cb           = on_failure
  self.permanent_fail_cb = on_permanent_failure
  self.sender_is_object = sender.equal?(object)
  self.sender_is_class = sender.is_a?(object.class)
  # bypass visibility checks (see MessageSending#__calculate_sender_for_delay)
  self.sender_is_object = true if sender.nil?
end

Instance Attribute Details

#argsObject

Returns the value of attribute args

Returns:

  • (Object)

    the current value of args



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def args
  @args
end

#fail_cbObject

Returns the value of attribute fail_cb

Returns:

  • (Object)

    the current value of fail_cb



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def fail_cb
  @fail_cb
end

#kwargsObject

Returns the value of attribute kwargs

Returns:

  • (Object)

    the current value of kwargs



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def kwargs
  @kwargs
end

#methodObject

Returns the value of attribute method

Returns:

  • (Object)

    the current value of method



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def method
  @method
end

#objectObject

Returns the value of attribute object

Returns:

  • (Object)

    the current value of object



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def object
  @object
end

#permanent_fail_cbObject

Returns the value of attribute permanent_fail_cb

Returns:

  • (Object)

    the current value of permanent_fail_cb



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def permanent_fail_cb
  @permanent_fail_cb
end

#senderObject

Returns the value of attribute sender

Returns:

  • (Object)

    the current value of sender



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def sender
  @sender
end

#sender_is_classObject

Returns the value of attribute sender_is_class

Returns:

  • (Object)

    the current value of sender_is_class



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def sender_is_class
  @sender_is_class
end

#sender_is_objectObject

Returns the value of attribute sender_is_object

Returns:

  • (Object)

    the current value of sender_is_object



4
5
6
# File 'lib/delayed/performable_method.rb', line 4

def sender_is_object
  @sender_is_object
end

Instance Method Details

#deep_de_ar_ize(arg) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/delayed/performable_method.rb', line 65

def deep_de_ar_ize(arg)
  case arg
  when Hash
    "{#{arg.map { |k, v| "#{deep_de_ar_ize(k)} => #{deep_de_ar_ize(v)}" }.join(", ")}}"
  when Array
    "[#{arg.map { |a| deep_de_ar_ize(a) }.join(", ")}]"
  when ActiveRecord::Base
    "#{arg.class}.find(#{arg.id})"
  else
    arg.inspect
  end
end

#display_nameObject Also known as: tag



28
29
30
31
32
33
34
# File 'lib/delayed/performable_method.rb', line 28

def display_name
  if object.is_a?(Module)
    "#{object}.#{method}"
  else
    "#{object.class}##{method}"
  end
end

#full_nameObject



78
79
80
81
82
83
# File 'lib/delayed/performable_method.rb', line 78

def full_name
  obj_name = object.is_a?(ActiveRecord::Base) ? "#{object.class}.find(#{object.id}).#{method}" : display_name
  kgs = kwargs || {}
  kwargs_str = kgs.map { |(k, v)| ", #{k}: #{deep_de_ar_ize(v)}" }.join
  "#{obj_name}(#{args.map { |a| deep_de_ar_ize(a) }.join(", ")}#{kwargs_str})"
end

#on_failure(error) ⇒ Object



57
58
59
# File 'lib/delayed/performable_method.rb', line 57

def on_failure(error)
  object.send(fail_cb, error) if fail_cb
end

#on_permanent_failure(error) ⇒ Object



61
62
63
# File 'lib/delayed/performable_method.rb', line 61

def on_permanent_failure(error)
  object.send(permanent_fail_cb, error) if permanent_fail_cb
end

#performObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/delayed/performable_method.rb', line 37

def perform
  kwargs = self.kwargs || {}

  # back-compat for jobs queued before we assigned these in initialize
  self.sender_is_object = sender.equal?(object) if sender_is_object.nil?
  self.sender_is_class = sender.is_a?(object.class) if sender_is_class.nil?

  if sender_is_object || (sender_is_class && object.protected_methods.include?(method))
    if kwargs.empty?
      object.__send__(method, *args)
    else
      object.__send__(method, *args, **kwargs)
    end
  elsif kwargs.empty?
    object.public_send(method, *args)
  else
    object.public_send(method, *args, **kwargs)
  end
end