hidden_hooks 1.2.0 → 1.3.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: 4b408f5e35a24cb32e4f5d2927174c96e10a2220c654d5a8900a89e4e2970152
4
- data.tar.gz: 81f34fb4792cc92fdbbc41d5ccb0279ea423316f03cc58e806c6d3f922cc903f
3
+ metadata.gz: '0738dd3eb4b4db1b211e7cf2f93837ae8b5e23e47dd1b5108b8590fc8eea870c'
4
+ data.tar.gz: 4c208ee06f8e57a049daaf990727ccd9d6cba79c422ec2efa3ec7340443d08b1
5
5
  SHA512:
6
- metadata.gz: 2d2c91bb68d8175daecb0e13beafec167bb39c88a9d1b49468d56074874912c945bd517f9a4db9bc8172e1df4640ab7b25aaf6f03921328d7d14befc9fbe3b66
7
- data.tar.gz: dbdc9b528501321089404046528a6665189c95ea81f4b871ea187be3a45599b83271e60739bcbb7bbc050d782dc58e4fc3d60541d59f896ca81a8403c75cf6f4
6
+ metadata.gz: 107ba5f389928174c5fbd28a402224b639c45ba52f9bec7e619cbba5c61c78a8088d55c4430bac076abd68383c932b225b1891b7f3f8774806eebef58ec5bc9b
7
+ data.tar.gz: 443fe108faca6c89796b5dcb5772ffad28e87c9c457b8f109b8077f3cf3a73911063059703e26401b037d1d15b3f03c6bfae253b3103b8f39a1e8e79b29f23f5
data/CHANGELOG.md CHANGED
@@ -8,6 +8,13 @@
8
8
  ### Bug fixes
9
9
  )-->
10
10
 
11
+ ## 1.3.0 2025-03-03
12
+
13
+ ### New features
14
+
15
+ - Added `sole` and `present` keyword parameter for hook invocation.
16
+ - Made hook invocation return the result values.
17
+
11
18
  ## 1.2.0 2025-02-04
12
19
 
13
20
  ### New features
data/README.md CHANGED
@@ -132,6 +132,39 @@ class User
132
132
  end
133
133
  ```
134
134
 
135
+ The results are returned as an array:
136
+
137
+ ```ruby
138
+ def valid_password? password
139
+ HiddenHooks[User].valid_password?(self, password).all?
140
+ end
141
+ ```
142
+
143
+ You can enforce that at least one hook be present passing `present: true` to `HiddenHooks.[]`:
144
+
145
+ ```ruby
146
+ HiddenHooks[User, present: true].valid_password?(self, password).all?
147
+ # => There must be at least one hook `valid_password?` defined for class `User` (HiddenHooks::AtLeastOneHookRequired)
148
+ ```
149
+
150
+ You can also enforce that no more than one hook be present passing `sole: true`:
151
+
152
+ ```ruby
153
+ HiddenHooks[User, sole: true].valid_password?(self, password).all?
154
+ # => There must be at most one hook `valid_password?` defined for class `User` (HiddenHooks::SoleHookExceeded)
155
+ ```
156
+
157
+ `sole:` defaults to `false`, and `present` defaults to whatever value `sole` has, so by default nothing is checked, and if you pass `sole: true` both checks are performed.
158
+
159
+ If both `sole:` and `present:` are true, then we can be certain that there is one and only one hook, so the single result value is unwrapped:
160
+
161
+ ```ruby
162
+ HiddenHooks[URI].signature_code(uri)
163
+ # => ["1234"]
164
+ HiddenHooks[URI, sole: true].signature_code(uri)
165
+ # => "1234"
166
+ ```
167
+
135
168
  #### Hook Definition
136
169
 
137
170
  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.
@@ -0,0 +1,4 @@
1
+ module HiddenHooks
2
+ class AtLeastOneHookRequired < StandardError
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module HiddenHooks
2
+ class SoleHookExceeded < StandardError
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module HiddenHooks
2
- VERSION = '1.2.0'
2
+ VERSION = '1.3.0'
3
3
  end
data/lib/hidden_hooks.rb CHANGED
@@ -1,5 +1,8 @@
1
1
  require_relative 'hidden_hooks/version'
2
2
 
3
+ require_relative 'hidden_hooks/at_least_one_hook_required'
4
+ require_relative 'hidden_hooks/sole_hook_exceeded'
5
+
3
6
  require 'active_support/core_ext/module/attribute_accessors'
4
7
 
5
8
  module HiddenHooks
@@ -9,9 +12,9 @@ module HiddenHooks
9
12
  instance_reader: false
10
13
 
11
14
  class SetUpProxy
12
- private
15
+ private
13
16
 
14
- def method_missing hook, klass, context: nil, &block
17
+ def method_missing hook, klass=nil, context: nil, &block
15
18
  ::HiddenHooks.hooks[klass][hook] << if context.nil?
16
19
  block
17
20
  else
@@ -29,14 +32,31 @@ module HiddenHooks
29
32
  end
30
33
 
31
34
  class LookUpProxy
32
- def initialize klass
35
+ def initialize klass=nil, sole: false, present: sole
36
+ @klass = klass
33
37
  @hooks = ::HiddenHooks.hooks[klass]
38
+ @sole = sole
39
+ @present = present
34
40
  end
35
41
 
36
- private
42
+ private
43
+
44
+ def method_missing(hook_name, *args, **kwargs, &block)
45
+ hooks = @hooks[hook_name]
46
+
47
+ if @present && hooks.empty?
48
+ raise HiddenHooks::AtLeastOneHookRequired,
49
+ "There must be at least one hook `#{hook_name}` defined for class `#{@klass}`"
50
+ end
51
+
52
+ if @sole && hooks.size > 1
53
+ raise HiddenHooks::SoleHookExceeded,
54
+ "There must be at most one hook `#{hook_name}` defined for class `#{@klass}`"
55
+ end
37
56
 
38
- def method_missing(hook, *args, **kwargs, &block)
39
- @hooks[hook].each { _1.call(*args, **kwargs, &block) }
57
+ hooks
58
+ .map { |hook| hook.call(*args, **kwargs, &block) }
59
+ .then { |results| (@sole && @present) ? results.first : results }
40
60
  end
41
61
 
42
62
  def respond_to_missing? _, _=false
@@ -44,12 +64,12 @@ module HiddenHooks
44
64
  end
45
65
  end
46
66
 
47
- def self.hook_up(&block)
67
+ def self.hook_up &block
48
68
  SetUpProxy.new.instance_exec(&block)
49
69
  end
50
70
 
51
- def self.[] klass
52
- LookUpProxy.new klass
71
+ def self.[](...)
72
+ LookUpProxy.new(...)
53
73
  end
54
74
  end
55
75
 
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.2.0
4
+ version: 1.3.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-02-04 00:00:00.000000000 Z
12
+ date: 2025-03-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -42,6 +42,8 @@ files:
42
42
  - hidden_hooks.gemspec
43
43
  - lib/hidden_hooks.rb
44
44
  - lib/hidden_hooks/active_record.rb
45
+ - lib/hidden_hooks/at_least_one_hook_required.rb
46
+ - lib/hidden_hooks/sole_hook_exceeded.rb
45
47
  - lib/hidden_hooks/version.rb
46
48
  homepage: https://github.com/moku-io/hidden_hooks
47
49
  licenses: