Class: Debounced::Callback

Inherits:
Object
  • Object
show all
Defined in:
lib/debounced/callback.rb

Overview

Represents a callback to be executed by the debounce service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(class_name:, method_name:, args: [], kwargs: {}, method_args: [], method_kwargs: {}) ⇒ Callback

Note:

if the class implements the method_name as a class method, the message will be sent to the class with args and kwargs.

Note:

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.

Parameters:

  • class_name (String)

    the name of the class that will receive the callback

  • method_name (String)

    the name of the method that will be called

  • args (Array) (defaults to: [])

    positional arguments passed to the static method, or to the initializer for instance methods (optional)

  • kwargs (Hash) (defaults to: {})

    keyword arguments passed to the static method, or to the initializer for instance methods (optional)

  • method_args (Array) (defaults to: [])

    positional arguments passed to the instance method (optional, ignored for static methods)

  • method_kwargs (Hash) (defaults to: {})

    keyword arguments passed to the instance method (optional, ignored for static methods)



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

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/debounced/callback.rb', line 6

def args
  @args
end

#class_nameObject

Returns the value of attribute class_name.



6
7
8
# File 'lib/debounced/callback.rb', line 6

def class_name
  @class_name
end

#kwargsObject

Returns the value of attribute kwargs.



6
7
8
# File 'lib/debounced/callback.rb', line 6

def kwargs
  @kwargs
end

#method_argsObject

Returns the value of attribute method_args.



6
7
8
# File 'lib/debounced/callback.rb', line 6

def method_args
  @method_args
end

#method_kwargsObject

Returns the value of attribute method_kwargs.



6
7
8
# File 'lib/debounced/callback.rb', line 6

def method_kwargs
  @method_kwargs
end

#method_nameObject

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_jsonObject



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

#callObject



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.message}")
  Debounced.configuration.logger.warn(e.backtrace.join("\n"))
end