tiny_hooks 0.2.0 → 0.3.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: 5b53066517bab59cf681b081724a473ea7eeaa18ad4cf94ce8d448af9a7f01a5
4
- data.tar.gz: c1d4e8b2927345164162c5b76c1e9a743d4ca6bc0566d89b2c98feecb988bd04
3
+ metadata.gz: 0c9ab59bc23d29d510f328d7b6c0f19bfc9b561b37b6865174cccb1470e76513
4
+ data.tar.gz: 58f946a1c561d3a52d4f3cb5b1943a0af75cd20e7c6fb1db3386406b55266f3c
5
5
  SHA512:
6
- metadata.gz: 2d60d516f4d7e347504e7f167c0bc8e47c05b55a44acfc9265c9223400289aadbb720ce76a3f50938d8215fd8cb1fd26be8bc3a9fcf82af4ab61febc9c9a0aed
7
- data.tar.gz: b05b43930d586e1f2c4bb6419c0353026e294bc3ef05b18d01a0e9fc414bd7cd69b1a3335ed8d7d8300c62bc9678a7923d35a9dc48c8413dd843a1f517ff7a95
6
+ metadata.gz: 2faa91f190024c927083e05a50861c4a367aa7f1a16dd2d88e2925081b96b91ea5e235eff0e7e7ec9d85a778dd5bfbcbc20a93e77050fa0aa8d4562dc3ebc13f
7
+ data.tar.gz: db4bc764b7e46918680cd552cf7fdde2b968fe728c30e5e67a0da896a4b6a0a24d72e62b5b4e9100ce65f7cca28c31effe460aff82d2f949c6f9fcf18c929543
data/.rubocop.yml CHANGED
@@ -33,6 +33,10 @@ Lint/ConstantResolution:
33
33
  Metrics/MethodLength:
34
34
  Max: 15
35
35
 
36
+ Security/Eval:
37
+ Exclude:
38
+ - 'test/**/*.rb'
39
+
36
40
  Style/ConstantVisibility:
37
41
  Exclude:
38
42
  - 'lib/tiny_hooks/version.rb'
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.3.0] - 2021-04-10
4
+
5
+ - Implement restoration of original method
6
+
3
7
  ## [0.2.0] - 2021-04-10
4
8
 
5
9
  - Support Ruby 2.5 and 2.6
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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TinyHooks
4
- VERSION = '0.2.0'
4
+ VERSION = '0.3.0'
5
5
  end
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
- # rubocop:
27
- # root_dir: project/
28
- # dependencies:
29
- # - rubocop-rails
30
- # - { name: "rubocop-rspec", version: "2.1.0" }
31
- # config: config/.rubocop.yml
32
- # safe: true
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.2.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-09 00:00:00.000000000 Z
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.11
57
+ rubygems_version: 3.2.16
58
58
  signing_key:
59
59
  specification_version: 4
60
60
  summary: Simple, tiny and general hooks control.