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 +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +33 -1
- data/lib/hidden_hooks/version.rb +1 -1
- data/lib/hidden_hooks.rb +10 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b408f5e35a24cb32e4f5d2927174c96e10a2220c654d5a8900a89e4e2970152
|
4
|
+
data.tar.gz: 81f34fb4792cc92fdbbc41d5ccb0279ea423316f03cc58e806c6d3f922cc903f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2d2c91bb68d8175daecb0e13beafec167bb39c88a9d1b49468d56074874912c945bd517f9a4db9bc8172e1df4640ab7b25aaf6f03921328d7d14befc9fbe3b66
|
7
|
+
data.tar.gz: dbdc9b528501321089404046528a6665189c95ea81f4b871ea187be3a45599b83271e60739bcbb7bbc050d782dc58e4fc3d60541d59f896ca81a8403c75cf6f4
|
data/CHANGELOG.md
CHANGED
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:
|
data/lib/hidden_hooks/version.rb
CHANGED
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] <<
|
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.
|
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-
|
12
|
+
date: 2025-02-04 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activesupport
|