hidden_hooks 1.1.2 → 1.2.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: 88532a44bde6fdd20a6bcd02505060ec0abffd7fe55e815c3dd82a80d0be8819
4
- data.tar.gz: 118bd2af55f7472686deaf3c93f9008b8d3fd820dab2082d6f032103f4f72583
3
+ metadata.gz: 4b408f5e35a24cb32e4f5d2927174c96e10a2220c654d5a8900a89e4e2970152
4
+ data.tar.gz: 81f34fb4792cc92fdbbc41d5ccb0279ea423316f03cc58e806c6d3f922cc903f
5
5
  SHA512:
6
- metadata.gz: 6c7b53ac8c04c1b6d02dbd6af42d7c7d6e6d93a60faea4c23ffcbf615e6744d3626678b01a4d8d4c4b53846263e188aedfcfb39ea9e22d36c61ddbdb4c98fa9e
7
- data.tar.gz: c81d8c1c686af906141279a02067a90a131445506a6c83c9fd967bd582b7868a6da54e780f08c1cba3d44da920ae8796e2be2a8c2b47611792647eb92da39434
6
+ metadata.gz: 2d2c91bb68d8175daecb0e13beafec167bb39c88a9d1b49468d56074874912c945bd517f9a4db9bc8172e1df4640ab7b25aaf6f03921328d7d14befc9fbe3b66
7
+ data.tar.gz: dbdc9b528501321089404046528a6665189c95ea81f4b871ea187be3a45599b83271e60739bcbb7bbc050d782dc58e4fc3d60541d59f896ca81a8403c75cf6f4
data/CHANGELOG.md CHANGED
@@ -8,6 +8,12 @@
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
+
11
17
  ## 1.1.2 2025-01-28
12
18
 
13
19
  ### 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:
@@ -1,3 +1,3 @@
1
1
  module HiddenHooks
2
- VERSION = '1.1.2'
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.2
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