debounced 1.0.8 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 221e144d2900e1237f3f06e9b9ef347eb2703f7c3b5d33242272ab5d1115c582
4
- data.tar.gz: 7757b7afd63ade6894f5dd62ac9a2855197f0ba14229bed563e3d4a3662ad2d1
3
+ metadata.gz: 23ca0ee42ff088cb85dd495dc84f5cffb38b2a6af402bab7b23424f962e8f002
4
+ data.tar.gz: 0e218d90fb5ad2d908cd20d3557dd6bf4aa7267f9461810c34d9f30b3c62d0ae
5
5
  SHA512:
6
- metadata.gz: f1e65faa3339ae66dce491120890d923213b8bbe49048525fc70ea022f6040ca9f2ab1fc78d28aa2905d0044bf8c78cffeb9811a6bfab6d01172334eb9273a0a
7
- data.tar.gz: 53c8676a30eba9f9ed493055e00a0fe9d72f760240626cc999c185d6ab57a3d2823cacc64da1d885b92425862b2e86a342934ef6ba185e109631601af05fb1ad
6
+ metadata.gz: 2f16ed706132085f5109692d233544893edb540b1500a498f5597caef5fa856e8e1bdc06c3eae9a46251e8104537908f7b7f24aca8356c16331465fd1c2de250
7
+ data.tar.gz: '02438414f936b2d72c5dd185288892a9e12501e734977ae3d2103ca458c22e628c8ff83ea809f68c3c21084888ebc2fd2a7c5a1752fd42d9bdba5ab29feb7444'
@@ -3,50 +3,77 @@ module Debounced
3
3
  ###
4
4
  # Represents a callback to be executed by the debounce service
5
5
  class Callback
6
- attr_accessor :class_name, :method_name, :args, :kwargs
6
+ attr_accessor :class_name, :method_name, :args, :kwargs, :method_args, :method_kwargs
7
7
 
8
8
  ###
9
9
  # @param [String] class_name the name of the class that will receive the callback
10
10
  # @param [String] method_name the name of the method that will be called
11
- # @param [Array] args the positional arguments to be passed to the method (optional)
12
- # @param [Hash] kwargs the keyword arguments to be passed to the method (optional)
11
+ # @param [Array] args positional arguments passed to the static method, or to the initializer for instance methods (optional)
12
+ # @param [Hash] kwargs keyword arguments passed to the static method, or to the initializer for instance methods (optional)
13
+ # @param [Array] method_args positional arguments passed to the instance method (optional, ignored for static methods)
14
+ # @param [Hash] method_kwargs keyword arguments passed to the instance method (optional, ignored for static methods)
13
15
  #
14
- # @note if the class implements the method_name, the message will be sent to the class with the args and kwargs.
15
- # otherwise, an instance of the class will be created and the message will be sent to the instance. in this case,
16
- # the args and kwargs will be passed to the initializer.
17
- def initialize(class_name:, method_name:, args: [], kwargs: {})
16
+ # @note if the class implements the method_name as a class method, the message will be sent to the class with args and kwargs.
17
+ # otherwise, an instance of the class will be created using args and kwargs, and the message will be sent to the instance
18
+ # with method_args and method_kwargs.
19
+ #
20
+ # @note args and kwargs values must be JSON-native types (String, Numeric, Boolean, Array, Hash, nil).
21
+ # Symbol values, Date, Time, and other Ruby-specific types will not survive JSON round-trip serialization
22
+ # through the debounce server. Hash keys are deep-symbolized on parse, but values are preserved as-is.
23
+ def initialize(class_name:, method_name:, args: [], kwargs: {}, method_args: [], method_kwargs: {})
18
24
  @class_name = class_name.to_s
19
25
  @method_name = method_name.to_s
20
26
  @args = args
21
27
  @kwargs = kwargs
28
+ @method_args = method_args
29
+ @method_kwargs = method_kwargs
22
30
  end
23
31
 
24
32
  def self.parse(data)
25
33
  new(
26
34
  class_name: data['class_name'],
27
35
  method_name: data['method_name'],
28
- args: data['args'],
29
- kwargs: data['kwargs'].transform_keys(&:to_sym),
36
+ args: data['args'] || [],
37
+ kwargs: deep_symbolize_keys(data['kwargs'] || {}),
38
+ method_args: data['method_args'] || [],
39
+ method_kwargs: deep_symbolize_keys(data['method_kwargs'] || {}),
30
40
  )
31
41
  end
32
42
 
43
+ def self.deep_symbolize_keys(object)
44
+ case object
45
+ when Hash
46
+ object.to_h { |key, value| [key.to_sym, deep_symbolize_keys(value)] }
47
+ when Array
48
+ object.map { |item| deep_symbolize_keys(item) }
49
+ else
50
+ object
51
+ end
52
+ end
53
+ private_class_method :deep_symbolize_keys
54
+
33
55
  def as_json
34
56
  {
35
57
  class_name:,
36
58
  method_name:,
37
59
  args:,
38
60
  kwargs:,
61
+ method_args:,
62
+ method_kwargs:,
39
63
  }
40
64
  end
41
65
 
42
66
  def call
43
67
  Debounced.configuration.logger.debug("Invoking callback #{method_name}")
44
68
  klass = Object.const_get(class_name)
69
+ unless klass.ancestors.include?(Debounced::Callbackable)
70
+ raise ArgumentError, "#{class_name} is not an allowed Debounced callback target. Include Debounced::Callbackable in the class."
71
+ end
45
72
  if klass.respond_to?(method_name)
46
73
  klass.send(method_name, *args, **kwargs)
47
74
  else
48
75
  instance = klass.new(*args, **kwargs)
49
- instance.send(method_name)
76
+ instance.send(method_name, *method_args, **method_kwargs)
50
77
  end
51
78
  rescue StandardError => e
52
79
  Debounced.configuration.logger.warn("Unable to invoke callback #{as_json}: #{e.message}")
@@ -0,0 +1,4 @@
1
+ module Debounced
2
+ module Callbackable
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module Debounced
2
- VERSION = "1.0.8"
2
+ VERSION = "2.0.0"
3
3
  end
data/lib/debounced.rb CHANGED
@@ -3,6 +3,7 @@ require 'debounced/railtie' if defined?(Rails)
3
3
  require 'debounced/no_server_error'
4
4
  require 'debounced/socket_conflict_error'
5
5
  require 'debounced/service_proxy'
6
+ require 'debounced/callbackable'
6
7
  require 'debounced/callback'
7
8
  require 'semantic_logger'
8
9
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: debounced
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.8
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary Passero
@@ -96,6 +96,7 @@ files:
96
96
  - README.md
97
97
  - lib/debounced.rb
98
98
  - lib/debounced/callback.rb
99
+ - lib/debounced/callbackable.rb
99
100
  - lib/debounced/javascript/server.mjs
100
101
  - lib/debounced/javascript/service.mjs
101
102
  - lib/debounced/no_server_error.rb
@@ -117,14 +118,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
117
118
  requirements:
118
119
  - - ">="
119
120
  - !ruby/object:Gem::Version
120
- version: 3.0.0
121
+ version: 3.1.0
121
122
  required_rubygems_version: !ruby/object:Gem::Requirement
122
123
  requirements:
123
124
  - - ">="
124
125
  - !ruby/object:Gem::Version
125
126
  version: '0'
126
127
  requirements: []
127
- rubygems_version: 3.6.9
128
+ rubygems_version: 4.0.10
128
129
  specification_version: 4
129
130
  summary: Efficient event debouncing in Ruby
130
131
  test_files: []