require_callbacks 0.1.1 → 0.1.2
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/.github/workflows/ruby.yml +8 -7
- data/README.md +1 -1
- data/lib/require_callbacks/version.rb +1 -1
- data/lib/require_callbacks.rb +8 -4
- data/spec/callbacks_spec.rb +29 -2
- metadata +6 -3
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 20c641df8e9750c77ba8096475d84a8d4176007f90fa8780b2fa5ca1dd325eb9
|
|
4
|
+
data.tar.gz: c195251e7ec6e8ac7116b6efbfce7bba9f04c3bfb67f4c1db49273c5f1d70a89
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bb90faf6bd036fd8e037df2f704cdad88a73b82d579ffc434f9b3a21d345e32fd1818891d6669a0dd8bffd866a911dcd4fb650ca679cb7a5152a1613b94e0e13
|
|
7
|
+
data.tar.gz: 25c6fbe3db956961aaa6ec00cdb00c9dc3000f923757bd1abc9f758e976f5ea8b0fde2611fe73185679f2b14efc0df4b57f6ba8a070a1a994f52849c22bd241c
|
data/.github/workflows/ruby.yml
CHANGED
|
@@ -2,20 +2,21 @@ name: Ruby tests
|
|
|
2
2
|
|
|
3
3
|
on:
|
|
4
4
|
push:
|
|
5
|
-
branches: [
|
|
5
|
+
branches: [main]
|
|
6
6
|
pull_request:
|
|
7
7
|
|
|
8
8
|
jobs:
|
|
9
9
|
test:
|
|
10
10
|
runs-on: ubuntu-latest
|
|
11
|
+
strategy:
|
|
12
|
+
fail-fast: false
|
|
13
|
+
matrix:
|
|
14
|
+
ruby: ['3.2', '3.3', '3.4', '4.0']
|
|
11
15
|
steps:
|
|
12
16
|
- uses: actions/checkout@v4
|
|
13
17
|
- uses: ruby/setup-ruby@v1
|
|
14
18
|
with:
|
|
15
|
-
ruby-version:
|
|
16
|
-
|
|
17
|
-
run: |
|
|
18
|
-
gem install rspec -v '~> 3.3' --no-document
|
|
19
|
-
gem install simplecov --no-document
|
|
19
|
+
ruby-version: ${{ matrix.ruby }}
|
|
20
|
+
bundler-cache: true
|
|
20
21
|
- name: Run specs
|
|
21
|
-
run:
|
|
22
|
+
run: bundle exec rspec spec
|
data/README.md
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# require_callbacks [](https://github.com/smudge/require_callbacks/actions/workflows/ruby.yml) [](https://rubygems.org/gems/require_callbacks)
|
|
2
2
|
|
|
3
3
|
This gem gives you convenient hooks around calls to `require`, which can be used to define
|
|
4
4
|
configuration or setup code that will eventually be run when the library is actually loaded.
|
data/lib/require_callbacks.rb
CHANGED
|
@@ -4,20 +4,24 @@ module RequireCallbacks
|
|
|
4
4
|
BEFORE_CALLBACKS = {}
|
|
5
5
|
AFTER_CALLBACKS = {}
|
|
6
6
|
|
|
7
|
+
def callback_name(name)
|
|
8
|
+
name.to_s.sub(/\.rb\z/, '')
|
|
9
|
+
end
|
|
10
|
+
|
|
7
11
|
def after_require(name, &block)
|
|
8
12
|
raise ArgumentError, 'callback block required' unless block
|
|
9
|
-
(AFTER_CALLBACKS[name] ||= []) << block
|
|
13
|
+
(AFTER_CALLBACKS[callback_name(name)] ||= []) << block
|
|
10
14
|
end
|
|
11
15
|
|
|
12
16
|
def before_require(name, &block)
|
|
13
17
|
raise ArgumentError, 'callback block required' unless block
|
|
14
|
-
(BEFORE_CALLBACKS[name] ||= []) << block
|
|
18
|
+
(BEFORE_CALLBACKS[callback_name(name)] ||= []) << block
|
|
15
19
|
end
|
|
16
20
|
|
|
17
21
|
def require(*args)
|
|
18
|
-
(BEFORE_CALLBACKS[args.first] || []).each(&:call)
|
|
22
|
+
(BEFORE_CALLBACKS[callback_name(args.first)] || []).each(&:call)
|
|
19
23
|
loaded = super
|
|
20
|
-
(AFTER_CALLBACKS[args.first] || []).each(&:call) if loaded
|
|
24
|
+
(AFTER_CALLBACKS[callback_name(args.first)] || []).each(&:call) if loaded
|
|
21
25
|
loaded
|
|
22
26
|
end
|
|
23
27
|
end
|
data/spec/callbacks_spec.rb
CHANGED
|
@@ -8,8 +8,11 @@ describe RequireCallbacks do
|
|
|
8
8
|
File.open("spec/#{file_name}.rb", 'w') do |f|
|
|
9
9
|
f.write("class #{file_name.upcase}; end")
|
|
10
10
|
end
|
|
11
|
-
|
|
12
|
-
|
|
11
|
+
begin
|
|
12
|
+
example.run
|
|
13
|
+
ensure
|
|
14
|
+
File.delete("spec/#{file_name}.rb") if File.exist?("spec/#{file_name}.rb")
|
|
15
|
+
end
|
|
13
16
|
end
|
|
14
17
|
|
|
15
18
|
it 'has a version number' do
|
|
@@ -68,6 +71,30 @@ describe RequireCallbacks do
|
|
|
68
71
|
expect(@count).to eq(2)
|
|
69
72
|
end
|
|
70
73
|
end
|
|
74
|
+
describe 'callbacks with .rb suffixes' do
|
|
75
|
+
it 'runs callbacks registered without the suffix when requiring with it' do
|
|
76
|
+
before_count = 0
|
|
77
|
+
after_count = 0
|
|
78
|
+
before_require(file_name) { before_count += 1 }
|
|
79
|
+
after_require(file_name) { after_count += 1 }
|
|
80
|
+
|
|
81
|
+
expect(require("#{file_name}.rb")).to eq(true)
|
|
82
|
+
expect(before_count).to eq(1)
|
|
83
|
+
expect(after_count).to eq(1)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
it 'runs callbacks registered with the suffix when requiring without it' do
|
|
87
|
+
before_count = 0
|
|
88
|
+
after_count = 0
|
|
89
|
+
before_require("#{file_name}.rb") { before_count += 1 }
|
|
90
|
+
after_require("#{file_name}.rb") { after_count += 1 }
|
|
91
|
+
|
|
92
|
+
expect(require(file_name)).to eq(true)
|
|
93
|
+
expect(before_count).to eq(1)
|
|
94
|
+
expect(after_count).to eq(1)
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
|
|
71
98
|
describe 'callbacks across receivers' do
|
|
72
99
|
let(:loader) { Object.new }
|
|
73
100
|
|
metadata
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: require_callbacks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.1.
|
|
4
|
+
version: 0.1.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- smudge
|
|
8
|
+
autorequire:
|
|
8
9
|
bindir: bin
|
|
9
10
|
cert_chain: []
|
|
10
|
-
date: 1980-01-
|
|
11
|
+
date: 1980-01-01 00:00:00.000000000 Z
|
|
11
12
|
dependencies:
|
|
12
13
|
- !ruby/object:Gem::Dependency
|
|
13
14
|
name: rake
|
|
@@ -79,6 +80,7 @@ homepage: ''
|
|
|
79
80
|
licenses:
|
|
80
81
|
- MIT
|
|
81
82
|
metadata: {}
|
|
83
|
+
post_install_message:
|
|
82
84
|
rdoc_options: []
|
|
83
85
|
require_paths:
|
|
84
86
|
- lib
|
|
@@ -93,7 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
93
95
|
- !ruby/object:Gem::Version
|
|
94
96
|
version: '0'
|
|
95
97
|
requirements: []
|
|
96
|
-
rubygems_version:
|
|
98
|
+
rubygems_version: 3.5.22
|
|
99
|
+
signing_key:
|
|
97
100
|
specification_version: 4
|
|
98
101
|
summary: Convenient hooks to help prevent loading unnecessary gems and configuration
|
|
99
102
|
code.
|