Class: Delayed::PerformableMethod
- Inherits:
-
Struct
- Object
- Struct
- Delayed::PerformableMethod
- Defined in:
- lib/delayed/performable_method.rb
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#fail_cb ⇒ Object
Returns the value of attribute fail_cb.
-
#kwargs ⇒ Object
Returns the value of attribute kwargs.
-
#method ⇒ Object
Returns the value of attribute method.
-
#object ⇒ Object
Returns the value of attribute object.
-
#permanent_fail_cb ⇒ Object
Returns the value of attribute permanent_fail_cb.
-
#sender ⇒ Object
Returns the value of attribute sender.
-
#sender_is_class ⇒ Object
Returns the value of attribute sender_is_class.
-
#sender_is_object ⇒ Object
Returns the value of attribute sender_is_object.
Instance Method Summary collapse
- #deep_de_ar_ize(arg) ⇒ Object
- #display_name ⇒ Object (also: #tag)
- #full_name ⇒ Object
-
#initialize(object, method, args: [], kwargs: {}, on_failure: nil, on_permanent_failure: nil, sender: nil) ⇒ PerformableMethod
constructor
A new instance of PerformableMethod.
- #on_failure(error) ⇒ Object
- #on_permanent_failure(error) ⇒ Object
- #perform ⇒ Object
Constructor Details
#initialize(object, method, args: [], kwargs: {}, on_failure: nil, on_permanent_failure: nil, sender: nil) ⇒ PerformableMethod
Returns a new instance of PerformableMethod.
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
#args ⇒ Object
Returns the value of attribute args
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def args @args end |
#fail_cb ⇒ Object
Returns the value of attribute fail_cb
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def fail_cb @fail_cb end |
#kwargs ⇒ Object
Returns the value of attribute kwargs
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def kwargs @kwargs end |
#method ⇒ Object
Returns the value of attribute method
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def method @method end |
#object ⇒ Object
Returns the value of attribute object
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def object @object end |
#permanent_fail_cb ⇒ Object
Returns the value of attribute permanent_fail_cb
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def permanent_fail_cb @permanent_fail_cb end |
#sender ⇒ Object
Returns the value of attribute sender
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def sender @sender end |
#sender_is_class ⇒ Object
Returns the value of attribute sender_is_class
4 5 6 |
# File 'lib/delayed/performable_method.rb', line 4 def sender_is_class @sender_is_class end |
#sender_is_object ⇒ Object
Returns the value of attribute 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_name ⇒ Object 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_name ⇒ Object
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 |
#perform ⇒ Object
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 |