Class: Delayed::PerformableMethod
- Inherits:
-
Object
- Object
- Delayed::PerformableMethod
- Defined in:
- lib/delayed/psych_ext.rb,
lib/delayed/performable_method.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#kwargs ⇒ Object
Returns the value of attribute kwargs.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
-
#object ⇒ Object
Returns the value of attribute object.
Instance Method Summary collapse
- #display_name ⇒ Object
-
#encode_with(coder) ⇒ Object
serialize to YAML.
-
#initialize(object, method_name, args, kwargs) ⇒ PerformableMethod
constructor
A new instance of PerformableMethod.
- #method(sym) ⇒ Object
- #method_missing(symbol, *args) ⇒ Object
- #perform ⇒ Object
- #respond_to?(symbol, include_private = false) ⇒ Boolean
Constructor Details
#initialize(object, method_name, args, kwargs) ⇒ PerformableMethod
Returns a new instance of PerformableMethod.
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/delayed/performable_method.rb', line 5 def initialize(object, method_name, args, kwargs) raise NoMethodError, "undefined method `#{method_name}' for #{object.inspect}" unless object.respond_to?(method_name, true) if !her_model?(object) && object.respond_to?(:persisted?) && !object.persisted? raise(ArgumentError, "job cannot be created for non-persisted record: #{object.inspect}") end self.object = object self.args = args self.kwargs = kwargs self.method_name = method_name.to_sym end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(symbol, *args) ⇒ Object
40 41 42 |
# File 'lib/delayed/performable_method.rb', line 40 def method_missing(symbol, *args) object.send(symbol, *args) end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
3 4 5 |
# File 'lib/delayed/performable_method.rb', line 3 def args @args end |
#kwargs ⇒ Object
Returns the value of attribute kwargs.
3 4 5 |
# File 'lib/delayed/performable_method.rb', line 3 def kwargs @kwargs end |
#method_name ⇒ Object
Returns the value of attribute method_name.
3 4 5 |
# File 'lib/delayed/performable_method.rb', line 3 def method_name @method_name end |
#object ⇒ Object
Returns the value of attribute object.
3 4 5 |
# File 'lib/delayed/performable_method.rb', line 3 def object @object end |
Instance Method Details
#display_name ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/delayed/performable_method.rb', line 18 def display_name if object.is_a?(Class) "#{object}.#{method_name}" else "#{object.class}##{method_name}" end end |
#encode_with(coder) ⇒ Object
serialize to YAML
4 5 6 7 8 9 10 11 |
# File 'lib/delayed/psych_ext.rb', line 4 def encode_with(coder) coder.map = { 'object' => object, 'method_name' => method_name, 'args' => args, 'kwargs' => kwargs, } end |
#method(sym) ⇒ Object
36 37 38 |
# File 'lib/delayed/performable_method.rb', line 36 def method(sym) object.method(sym) end |
#perform ⇒ Object
26 27 28 29 30 31 32 33 34 |
# File 'lib/delayed/performable_method.rb', line 26 def perform return unless object if kwargs.nil? || (RUBY_VERSION < '2.7' && kwargs.empty?) object.send(method_name, *args) else object.send(method_name, *args, **kwargs) end end |
#respond_to?(symbol, include_private = false) ⇒ Boolean
44 45 46 |
# File 'lib/delayed/performable_method.rb', line 44 def respond_to?(symbol, include_private = false) super || object.respond_to?(symbol, include_private) end |