tiny_hooks 0.2.0 → 0.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 +4 -4
- data/.rubocop.yml +4 -0
- data/CHANGELOG.md +4 -0
- data/README.md +10 -0
- data/lib/tiny_hooks.rb +21 -0
- data/lib/tiny_hooks/version.rb +1 -1
- data/sider.yml +7 -7
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0c9ab59bc23d29d510f328d7b6c0f19bfc9b561b37b6865174cccb1470e76513
|
4
|
+
data.tar.gz: 58f946a1c561d3a52d4f3cb5b1943a0af75cd20e7c6fb1db3386406b55266f3c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2faa91f190024c927083e05a50861c4a367aa7f1a16dd2d88e2925081b96b91ea5e235eff0e7e7ec9d85a778dd5bfbcbc20a93e77050fa0aa8d4562dc3ebc13f
|
7
|
+
data.tar.gz: db4bc764b7e46918680cd552cf7fdde2b968fe728c30e5e67a0da896a4b6a0a24d72e62b5b4e9100ce65f7cca28c31effe460aff82d2f949c6f9fcf18c929543
|
data/.rubocop.yml
CHANGED
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -41,6 +41,16 @@ MyClass.new.my_method
|
|
41
41
|
|
42
42
|
TinyHooks shines when the class/module is the base class/module of your library and your users will inherit/include it. In these cases, end users can define hooks to the methods you provide. The only thing you have to do is to provide the list of methods.
|
43
43
|
|
44
|
+
## Difference between TinyHooks and ActiveSupport::Callbacks
|
45
|
+
|
46
|
+
While `TinyHooks` and `ActiveSupport::Callbacks` share the same purpose, there are a few major differences.
|
47
|
+
|
48
|
+
* `TinyHooks` doesn’t support halting, but will support in the future.
|
49
|
+
* While `ActiveSupport::Callbacks` has a set of methods for callbacks to work, `TinyHooks` has only one method.
|
50
|
+
* You can apply callbacks/hooks into any existing methods without any changes with `TinyHooks`, while you need to change methods to call `run_callbacks` method within them to apply callbacks with `ActiveSupport::Callbacks`.
|
51
|
+
|
52
|
+
In short, `TinyHooks` is simpler while `ActiveSupport::Callbacks` allows more control over callbacks.
|
53
|
+
|
44
54
|
## Development
|
45
55
|
|
46
56
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
data/lib/tiny_hooks.rb
CHANGED
@@ -8,6 +8,15 @@ require_relative 'tiny_hooks/version'
|
|
8
8
|
module TinyHooks
|
9
9
|
class Error < StandardError; end
|
10
10
|
|
11
|
+
# @api private
|
12
|
+
def self.extended(mod)
|
13
|
+
mod.class_eval { @@_originals ||= {} }
|
14
|
+
# mod.instance_variable_set(:@_originals, {}) unless mod.instance_variable_defined?(:@_originals)
|
15
|
+
# mod.define_singleton_method(:_originals) do
|
16
|
+
# mod.instance_variable_get(:@_originals)
|
17
|
+
# end
|
18
|
+
end
|
19
|
+
|
11
20
|
# Define hook with kind and target method
|
12
21
|
#
|
13
22
|
# @param [Symbol, String] kind the kind of the hook, possible values are: :before, :after and :around
|
@@ -16,6 +25,8 @@ module TinyHooks
|
|
16
25
|
raise ArgumentError, 'You must provide a block' unless block
|
17
26
|
|
18
27
|
original_method = instance_method(target)
|
28
|
+
@@_originals[target.to_sym] = original_method unless @@_originals[target.to_sym]
|
29
|
+
|
19
30
|
body = case kind.to_sym
|
20
31
|
when :before
|
21
32
|
_before(original_method, &block)
|
@@ -32,6 +43,16 @@ module TinyHooks
|
|
32
43
|
|
33
44
|
module_function :define_hook
|
34
45
|
|
46
|
+
# Restore original method
|
47
|
+
#
|
48
|
+
# @param [Symbol, String] target
|
49
|
+
def restore_original(target)
|
50
|
+
original_method = @@_originals[target.to_sym] || instance_method(target)
|
51
|
+
|
52
|
+
undef_method(target)
|
53
|
+
define_method(target, original_method)
|
54
|
+
end
|
55
|
+
|
35
56
|
private
|
36
57
|
|
37
58
|
def _before(original_method, &block)
|
data/lib/tiny_hooks/version.rb
CHANGED
data/sider.yml
CHANGED
@@ -23,13 +23,13 @@ linter:
|
|
23
23
|
# config: config/reek.yml
|
24
24
|
|
25
25
|
# # RuboCop example. See https://help.sider.review/tools/ruby/rubocop
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
26
|
+
rubocop:
|
27
|
+
dependencies:
|
28
|
+
- rubocop-minitest
|
29
|
+
- rubocop-performance
|
30
|
+
- rubocop-rake
|
31
|
+
- rubocop-sensible
|
32
|
+
safe: true
|
33
33
|
|
34
34
|
# # ShellCheck example. See https://help.sider.review/tools/shellscript/shellcheck
|
35
35
|
# shellcheck:
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tiny_hooks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- OKURA Masafumi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-04-
|
11
|
+
date: 2021-04-25 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Simple, tiny and general hooks control.
|
14
14
|
email:
|
@@ -54,7 +54,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
54
54
|
- !ruby/object:Gem::Version
|
55
55
|
version: '0'
|
56
56
|
requirements: []
|
57
|
-
rubygems_version: 3.2.
|
57
|
+
rubygems_version: 3.2.16
|
58
58
|
signing_key:
|
59
59
|
specification_version: 4
|
60
60
|
summary: Simple, tiny and general hooks control.
|