hanami-utils 3.0.1 → 3.1.0.rc1

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: 0dddd1fbe527104648385b8391dc1a17e4bad39e640425b9307518bf3eda6e47
4
- data.tar.gz: b41e17209ef900280cd91e1774207d880be94955ef0e75bf9fc6124210422a6c
3
+ metadata.gz: 657a294bd7584897a29fe3f2d893f8edc38d9f8d864a58ebe2ad6d97d4ad991a
4
+ data.tar.gz: e1ce607909b6b2232d318be0472bbf138579c8216b7d62efa83baf71c23cda99
5
5
  SHA512:
6
- metadata.gz: b8f4acc8aff8c52a22a4887f775d5a29306af4bd6bc3679adbbf6b65117033c8b69bb18f4750f35004241cb2d1c80df57a10699d930a9c666fb4062db5fbf76b
7
- data.tar.gz: 3d0d64c2077131ef6c386aa5a37bae55dc89a7d05c6e1ec52e8eba3079289b3de3b8d272d965868fd58c9d81d463d430f821c569319d04ce29c7445e6240aeb4
6
+ metadata.gz: 5f136f5b12c05372a795bbf4cf3a78d58562195ea16fe3d33f1a9c6af0cace4639e39c774fd1a202d144fb1c5b92d499c62e7b3ec812f56f808e4c62571bd091
7
+ data.tar.gz: 973aabde17cef7e5db1b2466d2f46102ee795f385fbde9a03f4fe08f100c491bb39836c37aac59f211cbb7c5efad05b22bc56199192a3a41bc21413dde04956a
data/CHANGELOG.md CHANGED
@@ -19,7 +19,20 @@ and this project adheres to [Break Versioning](https://www.taoensso.com/break-ve
19
19
 
20
20
  ### Security
21
21
 
22
- [Unreleased]: https://github.com/hanami/hanami-utils/compare/v3.0.1...main
22
+ [Unreleased]: https://github.com/hanami/hanami-utils/compare/v3.1.0.rc1...main
23
+
24
+ ## [3.1.0.rc1] - 2026-11-27
25
+
26
+ ### Changed
27
+
28
+ - To be consistent, lambdas and callables in callbacks no longer enforce function arity (just like procs/blocks). (@ziggythehamster in #421)
29
+
30
+ ### Fixed
31
+
32
+ - Callbacks now support non-proc callables as intended. (@ziggythehamster in #421, @timriley in #424)
33
+ - `Hanami::Utils::Deprecation` reports the correct calling location on all Ruby engines and versions, including the latest JRuby. (@timriley in #423)
34
+
35
+ [3.1.0.rc1]: https://github.com/hanami/hanami-utils/compare/v3.0.1...v3.1.0.rc1
23
36
 
24
37
  ## [3.0.1] - 2026-07-03
25
38
 
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  [actions]: https://github.com/hanami/hanami-utils/actions
4
4
  [chat]: https://discord.gg/naQApPAsZB
5
- [forum]: https://discourse.hanamirb.org
5
+ [forum]: https://discourse.hanakai.org
6
6
  [rubygem]: https://rubygems.org/gems/hanami-utils
7
7
 
8
8
  # Hanami Utils [![Gem Version](https://badge.fury.io/rb/hanami-utils.svg)][rubygem] [![CI Status](https://github.com/hanami/hanami-utils/workflows/CI/badge.svg)][actions]
@@ -53,6 +53,15 @@ module Hanami
53
53
  # chain.append { Authenticator.authenticate! }
54
54
  # chain.append { |params| ArticleRepository.new.find(params[:id]) }
55
55
  #
56
+ # # Add a callable that isn't a proc, and it will be wrapped by `Callback` as well.
57
+ # module LivenessProbeCallback
58
+ # extend self
59
+ #
60
+ # def call = LivenessProbe.alive!
61
+ # end
62
+ #
63
+ # chain.append LivenessProbeCallback
64
+ #
56
65
  # # Append a Symbol as a reference to a method name that will be used as a callback.
57
66
  # # It will wrapped by `MethodCallback`
58
67
  # # If the #notificate method accepts some argument(s) they should be passed when `run` is invoked.
@@ -92,6 +101,15 @@ module Hanami
92
101
  # chain.prepend { Authenticator.authenticate! }
93
102
  # chain.prepend { |params| ArticleRepository.new.find(params[:id]) }
94
103
  #
104
+ # # Add a callable that isn't a proc, and it will be wrapped by `Callback` as well.
105
+ # module LivenessProbeCallback
106
+ # extend self
107
+ #
108
+ # def call = LivenessProbe.alive!
109
+ # end
110
+ #
111
+ # chain.prepend LivenessProbeCallback
112
+ #
95
113
  # # Add a Symbol as a reference to a method name that will be used as a callback.
96
114
  # # It will wrapped by `MethodCallback`
97
115
  # # If the #notificate method accepts some argument(s) they should be passed when `run` is invoked.
@@ -146,6 +164,8 @@ module Hanami
146
164
  # # some other logic that requires `params`
147
165
  # end
148
166
  #
167
+ # chain.append SomeEncapsulatedLogic.new # if `call` takes arguments, it will get passed params
168
+ #
149
169
  # chain.run(action, params)
150
170
  #
151
171
  # Those callbacks will be invoked within the context of `action`.
@@ -215,8 +235,8 @@ module Hanami
215
235
  # @example
216
236
  # require 'hanami/utils/callbacks'
217
237
  #
218
- # callable = Proc.new{} # it responds to #call
219
- # method = :upcase # it doesn't responds to #call
238
+ # callable = Proc.new {} # it responds to #call
239
+ # method = :upcase # it doesn't respond to #call
220
240
  #
221
241
  # Hanami::Utils::Callbacks::Factory.fabricate(callable).class
222
242
  # # => Hanami::Utils::Callbacks::Callback
@@ -232,7 +252,8 @@ module Hanami
232
252
  end
233
253
  end
234
254
 
235
- # Proc callback
255
+ # Proc and other callable callback.
256
+ #
236
257
  # It wraps an object that responds to #call
237
258
  #
238
259
  # @since 0.1.0
@@ -267,7 +288,34 @@ module Hanami
267
288
  #
268
289
  # @see Hanami::Utils::Callbacks::Chain#run
269
290
  def call(context, *args)
270
- context.instance_exec(*args, &callback)
291
+ callback_proc =
292
+ if callback.respond_to?(:to_proc)
293
+ # Procs and 100% compatibles
294
+ callback
295
+ else
296
+ # Anything else that is callable
297
+ # NB: we convert to a proc because it's basically free and
298
+ # simplifies the code below, see: https://github.com/ruby/ruby/blob/973c45fcb3eb56df4f13d6aa54499e6ccb02809a/proc.c#L4204
299
+ callback.method(:call).to_proc
300
+ end
301
+
302
+ # Procs don't enforce arity, but lambdas and methods converted to procs do
303
+ if callback_proc.lambda?
304
+ context.instance_exec(*accepted_args(callback_proc, args), &callback_proc)
305
+ else
306
+ context.instance_exec(*args, &callback_proc)
307
+ end
308
+ end
309
+
310
+ private
311
+
312
+ # Returns the args that the given lambda accepts: all of them if it takes a splat, otherwise
313
+ # as many as its required and optional positional params.
314
+ def accepted_args(callback_proc, args)
315
+ params = callback_proc.parameters
316
+ return args if params.any? { |type, _| type == :rest }
317
+
318
+ args.take(params.count { |type, _| type == :req || type == :opt })
271
319
  end
272
320
  end
273
321
 
@@ -60,18 +60,17 @@ module Hanami
60
60
  # # => old_method is deprecated, please use new_method - called from: test.rb:20:in `start'.
61
61
  # # => started
62
62
  def initialize(message)
63
- ::Kernel.warn("#{message} - called from: #{caller[caller_index]}.")
64
- end
65
-
66
- private
67
-
68
- # @api private
69
- def caller_index
70
- if RUBY_VERSION >= "4.0" && RUBY_ENGINE != "jruby"
71
- 1
72
- else
73
- 2
63
+ # Some Rubies include a frame for our own `.new` call at the top of the stack and some do
64
+ # not. Skip it if it's present (detect it through the frame having the same path and line as
65
+ # the frame after it). After that, the first frame is the deprecated method, and the next is
66
+ # its caller.
67
+ locations = caller_locations
68
+ first, second = locations
69
+ if first.base_label == "new" && first.path == second.path && first.lineno == second.lineno
70
+ locations.shift
74
71
  end
72
+
73
+ ::Kernel.warn("#{message} - called from: #{locations[1]}.")
75
74
  end
76
75
  end
77
76
  end
@@ -5,6 +5,6 @@ module Hanami
5
5
  # The current hanami-utils version.
6
6
  #
7
7
  # @api public
8
- VERSION = "3.0.1"
8
+ VERSION = "3.1.0.rc1"
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-utils
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.0.1
4
+ version: 3.1.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hanakai team