tiny_hooks 0.1.0 → 0.2.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: 03bd1054909658e3d514127bfccb44fe64f987840a3a0b55873cc0a2dae53fb7
4
- data.tar.gz: d052ca97c9e5b12efab72932e989d039a4dc72bb9f5911bd0a100fd97f95cf37
3
+ metadata.gz: 5b53066517bab59cf681b081724a473ea7eeaa18ad4cf94ce8d448af9a7f01a5
4
+ data.tar.gz: c1d4e8b2927345164162c5b76c1e9a743d4ca6bc0566d89b2c98feecb988bd04
5
5
  SHA512:
6
- metadata.gz: c2bf43f43d5fa16caa64caff95e1b6dd2831f5a242e22cf01f732ffc3d312336e6b2a9f5b32cb24f9c8f774fe2d16e0de9ef547453760d8bf800723f31c7f9a0
7
- data.tar.gz: e0f77aaf3299b760d8b6497850d35c99436b9d9e93d437c3b9cbdd309b865bc1ff5d3a8255c710a0c4aa039b04b56d0b5ae3117c44136a355f76166f4db5fba6
6
+ metadata.gz: 2d60d516f4d7e347504e7f167c0bc8e47c05b55a44acfc9265c9223400289aadbb720ce76a3f50938d8215fd8cb1fd26be8bc3a9fcf82af4ab61febc9c9a0aed
7
+ data.tar.gz: b05b43930d586e1f2c4bb6419c0353026e294bc3ef05b18d01a0e9fc414bd7cd69b1a3335ed8d7d8300c62bc9678a7923d35a9dc48c8413dd843a1f517ff7a95
@@ -4,15 +4,20 @@ on: [push,pull_request]
4
4
 
5
5
  jobs:
6
6
  build:
7
- runs-on: ubuntu-latest
7
+ strategy:
8
+ fail-fast: false
9
+ matrix:
10
+ os: [ubuntu-latest, windows-latest, macos-latest]
11
+ ruby: [2.5, 2.6, 2.7, 3.0, head]
12
+ runs-on: ${{ matrix.os }}
8
13
  steps:
9
14
  - uses: actions/checkout@v2
10
15
  - name: Set up Ruby
11
16
  uses: ruby/setup-ruby@v1
12
17
  with:
13
- ruby-version: 2.6.6
18
+ ruby-version: ${{ matrix.ruby }}
19
+ bundler-cache: true
14
20
  - name: Run the default task
15
21
  run: |
16
- gem install bundler -v 2.2.11
17
- bundle install
18
22
  bundle exec rake
23
+
data/.rubocop.yml CHANGED
@@ -15,7 +15,7 @@ AllCops:
15
15
  - 'tiny_hooks.gemspec'
16
16
  NewCops: enable
17
17
  EnabledByDefault: true
18
- TargetRubyVersion: 2.7
18
+ TargetRubyVersion: 2.5
19
19
 
20
20
  # Oneline comment is not valid so until it gets valid, we disable it
21
21
  Bundler/GemComment:
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2021-04-10
4
+
5
+ - Support Ruby 2.5 and 2.6
6
+
3
7
  ## [0.1.0] - 2021-04-09
4
8
 
5
9
  - Initial release
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # TinyHooks
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/tiny_hooks`. To experiment with that code, run `bin/console` for an interactive prompt.
4
-
5
- TODO: Delete this and the text above, and describe your gem
3
+ A tiny gem to define hooks.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,7 +20,26 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ `extend TinyHooks` in your class/module and you're all set to use `define_hook`!
24
+
25
+ ```ruby
26
+ class MyClass
27
+ extend TinyHooks
28
+
29
+ def my_method
30
+ puts 'my method'
31
+ end
32
+
33
+ define_hook :before, :my_method do
34
+ puts 'my before hook'
35
+ end
36
+ end
37
+
38
+ MyClass.new.my_method
39
+ # => "my before hook\nmy method\n"
40
+ ```
41
+
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.
26
43
 
27
44
  ## Development
28
45
 
data/Rakefile CHANGED
@@ -13,4 +13,5 @@ require "rubocop/rake_task"
13
13
 
14
14
  RuboCop::RakeTask.new
15
15
 
16
- task default: %i[test rubocop]
16
+ default = ENV['CI'] ? %i[test] : %i[test rubocop]
17
+ task default: default
data/lib/tiny_hooks.rb CHANGED
@@ -8,51 +8,71 @@ require_relative 'tiny_hooks/version'
8
8
  module TinyHooks
9
9
  class Error < StandardError; end
10
10
 
11
- # rubocop:disable Metrics/MethodLength
12
11
  # Define hook with kind and target method
13
12
  #
14
13
  # @param [Symbol, String] kind the kind of the hook, possible values are: :before, :after and :around
15
14
  # @param [Symbol, String] target the name of the targeted method
16
15
  def define_hook(kind, target, &block)
16
+ raise ArgumentError, 'You must provide a block' unless block
17
+
17
18
  original_method = instance_method(target)
18
19
  body = case kind.to_sym
19
20
  when :before
20
- proc do |*args, **kwargs, &blk|
21
- instance_exec(*args, **kwargs, &block)
22
- original_method.bind_call(self, *args, **kwargs, &blk)
23
- end
21
+ _before(original_method, &block)
24
22
  when :after
25
- proc do |*args, **kwargs, &blk|
26
- original_method.bind_call(self, *args, **kwargs, &blk)
27
- instance_exec(*args, **kwargs, &block)
28
- end
23
+ _after(original_method, &block)
29
24
  when :around
30
- proc do |*args, **kwargs, &blk|
31
- wrapper = -> { original_method.bind_call(self, *args, **kwargs, &blk) }
32
- instance_exec(wrapper, *args, **kwargs, &block)
33
- end
25
+ _around(original_method, &block)
34
26
  else
35
27
  raise Error, "#{kind} is not supported."
36
28
  end
37
29
  undef_method(target)
38
30
  define_method(target, &body)
39
31
  end
40
- # rubocop:enable Metrics/MethodLength
41
32
 
42
33
  module_function :define_hook
43
34
 
44
- # Restore original method from the registry
45
- #
46
- # @param [Symbol, String] target the name of the method to restore
47
- def restore_original(target)
48
- original = registry.fetch(target.to_sym) { instance_method(target) }
49
- undef_method(target)
50
- define_method(target, original)
35
+ private
36
+
37
+ def _before(original_method, &block)
38
+ if RUBY_VERSION >= '2.7'
39
+ proc do |*args, **kwargs, &blk|
40
+ instance_exec(*args, **kwargs, &block)
41
+ original_method.bind_call(self, *args, **kwargs, &blk)
42
+ end
43
+ else
44
+ proc do |*args, &blk|
45
+ instance_exec(*args, &block)
46
+ original_method.bind(self).call(*args, &blk)
47
+ end
48
+ end
51
49
  end
52
50
 
53
- private
51
+ def _after(original_method, &block)
52
+ if RUBY_VERSION >= '2.7'
53
+ proc do |*args, **kwargs, &blk|
54
+ original_method.bind_call(self, *args, **kwargs, &blk)
55
+ instance_exec(*args, **kwargs, &block)
56
+ end
57
+ else
58
+ proc do |*args, &blk|
59
+ original_method.bind(self).call(*args, &blk)
60
+ instance_exec(*args, &block)
61
+ end
62
+ end
63
+ end
54
64
 
55
- def registry
56
- @registry ||= {}
65
+ def _around(original_method, &block)
66
+ if RUBY_VERSION >= '2.7'
67
+ proc do |*args, **kwargs, &blk|
68
+ wrapper = -> { original_method.bind_call(self, *args, **kwargs, &blk) }
69
+ instance_exec(wrapper, *args, **kwargs, &block)
70
+ end
71
+ else
72
+ proc do |*args, &blk|
73
+ wrapper = -> { original_method.bind(self).call(*args, &blk) }
74
+ instance_exec(wrapper, *args, &block)
75
+ end
76
+ end
57
77
  end
58
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module TinyHooks
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/sider.yml ADDED
@@ -0,0 +1,59 @@
1
+ # This is a configuration file to customize code analysis by Sider.
2
+ #
3
+ # For more information, see the documentation:
4
+ # https://help.sider.review/getting-started/custom-configuration
5
+
6
+ # Customize each tool. If analyses fail, try adjusting each option referencing the following example.
7
+ linter:
8
+
9
+ # # Misspell example. See https://help.sider.review/tools/others/misspell
10
+ # misspell:
11
+ # root_dir: project/
12
+ # target: [src/, test/]
13
+ # exclude: ["**/*.min.*"]
14
+ # locale: UK
15
+ # ignore: [center, behavior]
16
+
17
+ # # Reek example. See https://help.sider.review/tools/ruby/reek
18
+ # reek:
19
+ # root_dir: project/
20
+ # dependencies:
21
+ # - { name: "reek", version: "6.0.0" }
22
+ # target: [lib/, test/]
23
+ # config: config/reek.yml
24
+
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
33
+
34
+ # # ShellCheck example. See https://help.sider.review/tools/shellscript/shellcheck
35
+ # shellcheck:
36
+ # root_dir: project/
37
+ # target:
38
+ # - "**/*.{sh,bash}"
39
+ # - shebang: true
40
+ # include: [SC2104, SC2105]
41
+ # exclude: [SC1000, SC1118]
42
+ # enable: all
43
+ # shell: bash
44
+ # severity: error
45
+ # norc: true
46
+
47
+ # Ignore specific files. Example:
48
+ # ignore:
49
+ # - "*.pdf"
50
+ # - "*.mp4"
51
+ # - "*.min.*"
52
+ # - "images/**"
53
+
54
+ # Exclude specific branches. Example:
55
+ # branches:
56
+ # exclude:
57
+ # - master
58
+ # - development
59
+ # - /^release-.*$/
data/tiny_hooks.gemspec CHANGED
@@ -12,7 +12,7 @@ Gem::Specification.new do |spec|
12
12
  spec.description = "Simple, tiny and general hooks control."
13
13
  spec.homepage = "https://github.com/okuramasafumi/tiny_hooks"
14
14
  spec.license = "MIT"
15
- spec.required_ruby_version = Gem::Requirement.new(">= 2.7.0")
15
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
16
16
 
17
17
  spec.metadata["homepage_uri"] = spec.homepage
18
18
  spec.metadata["source_code_uri"] = spec.homepage
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tiny_hooks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OKURA Masafumi
@@ -30,6 +30,7 @@ files:
30
30
  - bin/setup
31
31
  - lib/tiny_hooks.rb
32
32
  - lib/tiny_hooks/version.rb
33
+ - sider.yml
33
34
  - tiny_hooks.gemspec
34
35
  homepage: https://github.com/okuramasafumi/tiny_hooks
35
36
  licenses:
@@ -46,14 +47,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
46
47
  requirements:
47
48
  - - ">="
48
49
  - !ruby/object:Gem::Version
49
- version: 2.7.0
50
+ version: 2.5.0
50
51
  required_rubygems_version: !ruby/object:Gem::Requirement
51
52
  requirements:
52
53
  - - ">="
53
54
  - !ruby/object:Gem::Version
54
55
  version: '0'
55
56
  requirements: []
56
- rubygems_version: 3.1.6
57
+ rubygems_version: 3.2.11
57
58
  signing_key:
58
59
  specification_version: 4
59
60
  summary: Simple, tiny and general hooks control.