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)

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/delayed/performable_method.rb', line 5

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 = sender
  begin
    YAML.load(YAML.dump(sender))
  rescue
    # if for some reason you can't dump the sender, just drop it
    self.sender = nil
  end
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

Instance Method Details

#deep_de_ar_ize(arg) ⇒ Object


61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/delayed/performable_method.rb', line 61

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


23
24
25
26
27
28
29
# File 'lib/delayed/performable_method.rb', line 23

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

#full_nameObject


74
75
76
77
78
79
# File 'lib/delayed/performable_method.rb', line 74

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


53
54
55
# File 'lib/delayed/performable_method.rb', line 53

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

#on_permanent_failure(error) ⇒ Object


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

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

#performObject


32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/delayed/performable_method.rb', line 32

def perform
  kwargs = self.kwargs || {}

  sender_is_object = sender == object
  sender_is_class = sender.is_a?(object.class)

  if sender.nil? || 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
  else
    if kwargs.empty?
      object.public_send(method, *args)
    else
      object.public_send(method, *args, **kwargs)
    end
  end
end