Class: Debounced::Callback
- Inherits:
-
Object
- Object
- Debounced::Callback
- Defined in:
- lib/debounced/callback.rb
Overview
Represents a callback to be executed by the debounce service
Instance Attribute Summary collapse
-
#args ⇒ Object
Returns the value of attribute args.
-
#class_name ⇒ Object
Returns the value of attribute class_name.
-
#kwargs ⇒ Object
Returns the value of attribute kwargs.
-
#method_args ⇒ Object
Returns the value of attribute method_args.
-
#method_kwargs ⇒ Object
Returns the value of attribute method_kwargs.
-
#method_name ⇒ Object
Returns the value of attribute method_name.
Class Method Summary collapse
Instance Method Summary collapse
- #as_json ⇒ Object
- #call ⇒ Object
-
#initialize(class_name:, method_name:, args: [], kwargs: {}, method_args: [], method_kwargs: {}) ⇒ Callback
constructor
otherwise, an instance of the class will be created using args and kwargs, and the message will be sent to the instance with method_args and method_kwargs.
Constructor Details
#initialize(class_name:, method_name:, args: [], kwargs: {}, method_args: [], method_kwargs: {}) ⇒ Callback
if the class implements the method_name as a class method, the message will be sent to the class with args and kwargs.
args and kwargs values must be JSON-native types (String, Numeric, Boolean, Array, Hash, nil).
otherwise, an instance of the class will be created using args and kwargs, and the message will be sent to the instance with method_args and method_kwargs.
Symbol values, Date, Time, and other Ruby-specific types will not survive JSON round-trip serialization through the debounce server. Hash keys are deep-symbolized on parse, but values are preserved as-is.
23 24 25 26 27 28 29 30 |
# File 'lib/debounced/callback.rb', line 23 def initialize(class_name:, method_name:, args: [], kwargs: {}, method_args: [], method_kwargs: {}) @class_name = class_name.to_s @method_name = method_name.to_s @args = args @kwargs = kwargs @method_args = method_args @method_kwargs = method_kwargs end |
Instance Attribute Details
#args ⇒ Object
Returns the value of attribute args.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def args @args end |
#class_name ⇒ Object
Returns the value of attribute class_name.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def class_name @class_name end |
#kwargs ⇒ Object
Returns the value of attribute kwargs.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def kwargs @kwargs end |
#method_args ⇒ Object
Returns the value of attribute method_args.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def method_args @method_args end |
#method_kwargs ⇒ Object
Returns the value of attribute method_kwargs.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def method_kwargs @method_kwargs end |
#method_name ⇒ Object
Returns the value of attribute method_name.
6 7 8 |
# File 'lib/debounced/callback.rb', line 6 def method_name @method_name end |
Class Method Details
.parse(data) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/debounced/callback.rb', line 32 def self.parse(data) new( class_name: data['class_name'], method_name: data['method_name'], args: data['args'] || [], kwargs: deep_symbolize_keys(data['kwargs'] || {}), method_args: data['method_args'] || [], method_kwargs: deep_symbolize_keys(data['method_kwargs'] || {}), ) end |
Instance Method Details
#as_json ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/debounced/callback.rb', line 55 def as_json { class_name:, method_name:, args:, kwargs:, method_args:, method_kwargs:, } end |
#call ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/debounced/callback.rb', line 66 def call Debounced.configuration.logger.debug("Invoking callback #{method_name}") klass = Object.const_get(class_name) unless klass.ancestors.include?(Debounced::Callbackable) raise ArgumentError, "#{class_name} is not an allowed Debounced callback target. Include Debounced::Callbackable in the class." end if klass.respond_to?(method_name) klass.send(method_name, *args, **kwargs) else instance = klass.new(*args, **kwargs) instance.send(method_name, *method_args, **method_kwargs) end rescue StandardError => e Debounced.configuration.logger.warn("Unable to invoke callback #{as_json}: #{e.}") Debounced.configuration.logger.warn(e.backtrace.join("\n")) end |