hidden_hooks 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8c3de95afe7b03bb8fc886bb721ef7af2db8d5c8284c7aa7c0d331e33f5b5751
4
- data.tar.gz: 02f901e311228c2db78c00c1d6f7cdc6e4ef4b6c56ca048c85c7976a2721170b
3
+ metadata.gz: 4b408f5e35a24cb32e4f5d2927174c96e10a2220c654d5a8900a89e4e2970152
4
+ data.tar.gz: 81f34fb4792cc92fdbbc41d5ccb0279ea423316f03cc58e806c6d3f922cc903f
5
5
  SHA512:
6
- metadata.gz: 5d5c2d1346b269bd2f49a45c5cb419ad3cedd239d8f5c2c46fc6f5b95c7ccfe18d5c52f700c63e0a133f4ff81498afbca6917b1449a2e315fcef9dc1024a338f
7
- data.tar.gz: f5020d12589ece606c447d45bef122fde445d4136cce36e2120cd9ed23d835615c3cab2f47de1b50d1a9b1fbe2326f97cd08e47f3a47704a3be6f37ee7706fcb
6
+ metadata.gz: 2d2c91bb68d8175daecb0e13beafec167bb39c88a9d1b49468d56074874912c945bd517f9a4db9bc8172e1df4640ab7b25aaf6f03921328d7d14befc9fbe3b66
7
+ data.tar.gz: dbdc9b528501321089404046528a6665189c95ea81f4b871ea187be3a45599b83271e60739bcbb7bbc050d782dc58e4fc3d60541d59f896ca81a8403c75cf6f4
data/CHANGELOG.md CHANGED
@@ -8,6 +8,20 @@
8
8
  ### Bug fixes
9
9
  )-->
10
10
 
11
+ ## 1.2.0 2025-02-04
12
+
13
+ ### New features
14
+
15
+ - Added the `context` keyword parameter for hooks.
16
+
17
+ ## 1.1.2 2025-01-28
18
+
19
+ ### Bug fixes
20
+
21
+ - Fixed Active Record integration not setting callbacks for derived classes.
22
+ - Removed `after_*_commit` shortcut hooks.
23
+ - These aren't actually callbacks, but little more than wrappers for `after_commit` itself, so the method that gets called on the callback object is always `#after_commit`, causing some weird behaviors.
24
+
11
25
  ## 1.1.1 2025-01-28
12
26
 
13
27
  ### Bug fixes
data/README.md CHANGED
@@ -120,12 +120,44 @@ end
120
120
 
121
121
  #### Interface Declaration
122
122
 
123
- A class `C` declares the interface through `HiddenHooks[C]`. Calling a method on the returned proxy will call every hook that someone else defined, forwarding any argument.
123
+ A class `C` declares the interface through `HiddenHooks[C]`. Calling a method on the returned proxy will call every hook that someone else defined, forwarding any argument.
124
+
125
+ ```ruby
126
+ class User
127
+ def confirm!
128
+ HiddenHooks[User].before_confirmation self
129
+ @confirmed = true
130
+ HiddenHooks[User].after_confirmation self
131
+ end
132
+ end
133
+ ```
124
134
 
125
135
  #### Hook Definition
126
136
 
127
137
  Whenever you want to define a hook, you simply call `HiddenHooks.hook_up`. Inside the block, you can call any method and pass it a class and a block: the block will become a hook for that class.
128
138
 
139
+ ```ruby
140
+ class Admin
141
+ HiddenHooks.hook_up do
142
+ before_confirmation User do |user|
143
+ Admin.first.notify! "#{user.name} is being confirmed."
144
+ end
145
+ end
146
+ end
147
+ ```
148
+
149
+ You can provide a callable `context`, which will be invoked with the same parameters as the hook. Its result will be bound to `self` inside the hook.
150
+
151
+ ```ruby
152
+ class Admin
153
+ HiddenHooks.hook_up do
154
+ before_confirmation User, context: proc { Admin.first } do |user|
155
+ notify! "#{user.name} is being confirmed."
156
+ end
157
+ end
158
+ end
159
+ ```
160
+
129
161
  #### Rails Callbacks
130
162
 
131
163
  Thanks to the [callback objects](https://guides.rubyonrails.org/active_record_callbacks.html#callback-objects) system, in Rails you can simply pass the proxy to the callback methods:
@@ -4,30 +4,30 @@ module HiddenHooks
4
4
  module ActiveRecord
5
5
  extend ActiveSupport::Concern
6
6
 
7
- included do
8
- [
9
- :after_commit,
10
- :after_create,
11
- :after_create_commit,
12
- :after_destroy,
13
- :after_destroy_commit,
14
- :after_find,
15
- :after_initialize,
16
- :after_rollback,
17
- :after_save,
18
- :after_save_commit,
19
- :after_touch,
20
- :after_update,
21
- :after_update_commit,
22
- :after_validation,
23
- :before_commit,
24
- :before_create,
25
- :before_destroy,
26
- :before_save,
27
- :before_update,
28
- :before_validation
29
- ].each do |callback|
30
- send callback, HiddenHooks[self], prepend: true
7
+ class_methods do
8
+ def inherited subclass
9
+ super
10
+
11
+ [
12
+ :after_commit,
13
+ :after_create,
14
+ :after_destroy,
15
+ :after_find,
16
+ :after_initialize,
17
+ :after_rollback,
18
+ :after_save,
19
+ :after_touch,
20
+ :after_update,
21
+ :after_validation,
22
+ :before_commit,
23
+ :before_create,
24
+ :before_destroy,
25
+ :before_save,
26
+ :before_update,
27
+ :before_validation
28
+ ].each do |callback|
29
+ subclass.send callback, HiddenHooks[subclass], prepend: true
30
+ end
31
31
  end
32
32
  end
33
33
  end
@@ -1,3 +1,3 @@
1
1
  module HiddenHooks
2
- VERSION = '1.1.1'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/hidden_hooks.rb CHANGED
@@ -11,8 +11,16 @@ module HiddenHooks
11
11
  class SetUpProxy
12
12
  private
13
13
 
14
- def method_missing hook, klass, &block
15
- ::HiddenHooks.hooks[klass][hook] << block
14
+ def method_missing hook, klass, context: nil, &block
15
+ ::HiddenHooks.hooks[klass][hook] << if context.nil?
16
+ block
17
+ else
18
+ proc do |*args, **kwargs, &hook_block|
19
+ context
20
+ .call(*args, **kwargs, &hook_block)
21
+ .instance_exec(*args, hook_block, **kwargs, &block)
22
+ end
23
+ end
16
24
  end
17
25
 
18
26
  def respond_to_missing? _, _=false
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hidden_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moku S.r.l.
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2025-01-28 00:00:00.000000000 Z
12
+ date: 2025-02-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport