require_callbacks 0.0.1 → 0.1.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 +5 -5
- data/.codeclimate.yml +11 -0
- data/.github/workflows/ruby.yml +21 -0
- data/.rubocop.yml +6 -0
- data/.travis.yml +8 -0
- data/README.md +17 -24
- data/Rakefile +5 -0
- data/lib/require_callbacks/version.rb +1 -1
- data/lib/require_callbacks.rb +14 -17
- data/require_callbacks.gemspec +7 -7
- data/spec/callbacks_spec.rb +79 -0
- data/spec/spec_helper.rb +5 -0
- metadata +40 -23
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
|
-
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: abadf1c04770cd5702f443da4d475fcd7e35929ea781c5611ec4765049fcce5d
|
|
4
|
+
data.tar.gz: 788717fdce91316dc9b18d61ced01fe616d19f598bd4e6288ce1e42227e88f5b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 4d8b5cfef188beb1bc58765fb7fc1550d2a4cd1cbf43f1ad763903e16f9569e61b47e22f22c000df01b9ca0dc60d115a97edde1420c7cfcf73126b46823cc37c
|
|
7
|
+
data.tar.gz: 63a87d2156fd5b4c318638dc90d923ce0d9d818a1e3d73cac357a16f4c3efe90358b9ef5a2b31a5dccfeaf5002cd6ad282ef8ed516108c16e7243a394c931c3f
|
data/.codeclimate.yml
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
name: Ruby tests
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: [master]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
test:
|
|
10
|
+
runs-on: ubuntu-latest
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
- uses: ruby/setup-ruby@v1
|
|
14
|
+
with:
|
|
15
|
+
ruby-version: '3.2'
|
|
16
|
+
- name: Install test dependencies
|
|
17
|
+
run: |
|
|
18
|
+
gem install rspec -v '~> 3.3' --no-document
|
|
19
|
+
gem install simplecov --no-document
|
|
20
|
+
- name: Run specs
|
|
21
|
+
run: ruby -Ilib -S rspec spec
|
data/.rubocop.yml
ADDED
data/.travis.yml
ADDED
data/README.md
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
# require_callbacks
|
|
1
|
+
# require_callbacks [](https://travis-ci.org/smudge/require_callbacks) [](https://codeclimate.com/github/smudge/require_callbacks) [](https://codeclimate.com/github/smudge/require_callbacks/coverage)
|
|
2
2
|
|
|
3
|
-
This gem gives you convenient hooks around calls to `
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
This gem gives you convenient hooks around calls to `require`, which can be used to define
|
|
4
|
+
configuration or setup code that will eventually be run when the library is actually loaded.
|
|
5
|
+
This helps prevent loading unnecessary gems and configuration code in contexts where they
|
|
6
|
+
are not needed.
|
|
7
7
|
|
|
8
8
|
## Basic Example
|
|
9
9
|
|
|
@@ -21,7 +21,7 @@ define the block that must be run after the gem is loaded:
|
|
|
21
21
|
|
|
22
22
|
```ruby
|
|
23
23
|
after_require('inky') do
|
|
24
|
-
Inky.authorize!(ENV['MY_INKY_KEY'])
|
|
24
|
+
Inky.authorize!(ENV['MY_INKY_KEY'])
|
|
25
25
|
end
|
|
26
26
|
```
|
|
27
27
|
|
|
@@ -55,37 +55,30 @@ Lastly, don't forget to execute:
|
|
|
55
55
|
|
|
56
56
|
## Callbacks
|
|
57
57
|
|
|
58
|
-
`require_callbacks` adds the following methods, which are wrapped around `
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
be run *every* time.
|
|
58
|
+
`require_callbacks` adds the following methods, which are wrapped around `require`.
|
|
59
|
+
Note that the `after_require` callbacks will only be run the *first* time (when `require`
|
|
60
|
+
returns true), while `before_require` will be run *every* time.
|
|
62
61
|
|
|
63
62
|
```ruby
|
|
64
|
-
after_load('myfile.rb') do
|
|
65
|
-
# always run
|
|
66
|
-
end
|
|
67
63
|
after_require('mygem') do
|
|
68
64
|
# run once
|
|
69
65
|
end
|
|
70
|
-
after_require_relative('./myfile') do
|
|
71
|
-
# run once
|
|
72
|
-
end
|
|
73
66
|
|
|
74
|
-
before_load('myfile.rb') do
|
|
75
|
-
# always run
|
|
76
|
-
end
|
|
77
67
|
before_require('mygem') do
|
|
78
68
|
# always run
|
|
79
69
|
end
|
|
80
|
-
before_require_relative('./myfile') do
|
|
81
|
-
# always run
|
|
82
|
-
end
|
|
83
70
|
```
|
|
84
71
|
|
|
85
72
|
## Warnings
|
|
86
73
|
|
|
87
|
-
This gem messes with standard `
|
|
88
|
-
issues with anything else that modifies `
|
|
74
|
+
This gem messes with standard `Kernel` methods, so use at your own risk! There may be compatibility
|
|
75
|
+
issues with anything else that modifies `require`.
|
|
76
|
+
|
|
77
|
+
## Todo
|
|
78
|
+
|
|
79
|
+
* Add support for `require_relative`
|
|
80
|
+
* Add support for `load`
|
|
81
|
+
* Recognize when different arguments resolve to the same path (e.g. 'my_lib' vs 'my_lib.rb')
|
|
89
82
|
|
|
90
83
|
## Contributing
|
|
91
84
|
|
data/Rakefile
CHANGED
data/lib/require_callbacks.rb
CHANGED
|
@@ -1,25 +1,22 @@
|
|
|
1
1
|
require 'require_callbacks/version'
|
|
2
2
|
|
|
3
3
|
module RequireCallbacks
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
@after_load ||= {}
|
|
7
|
-
@after_load[name] ||= []
|
|
8
|
-
@after_load[name] << block
|
|
9
|
-
end
|
|
4
|
+
BEFORE_CALLBACKS = {}
|
|
5
|
+
AFTER_CALLBACKS = {}
|
|
10
6
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
7
|
+
def after_require(name, &block)
|
|
8
|
+
(AFTER_CALLBACKS[name] ||= []) << block
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def before_require(name, &block)
|
|
12
|
+
(BEFORE_CALLBACKS[name] ||= []) << block
|
|
13
|
+
end
|
|
16
14
|
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
end
|
|
15
|
+
def require(*args)
|
|
16
|
+
(BEFORE_CALLBACKS[args.first] || []).each(&:call)
|
|
17
|
+
loaded = super
|
|
18
|
+
(AFTER_CALLBACKS[args.first] || []).each(&:call) if loaded
|
|
19
|
+
loaded
|
|
23
20
|
end
|
|
24
21
|
end
|
|
25
22
|
|
data/require_callbacks.gemspec
CHANGED
|
@@ -13,11 +13,10 @@ Gem::Specification.new do |spec|
|
|
|
13
13
|
configuration code.
|
|
14
14
|
END
|
|
15
15
|
spec.description = <<-END.gsub("\n", ' ').gsub!(/[[:space:]]+/, ' ').strip
|
|
16
|
-
This gem gives you convenient hooks around calls to `
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
they are not needed.
|
|
16
|
+
This gem gives you convenient hooks around calls to `require` which can be
|
|
17
|
+
used to define configuration or setup code that will eventually be run when
|
|
18
|
+
the library is actually loaded. This helps prevent loading unnecessary gems
|
|
19
|
+
and configuration code in contexts where they are not needed.
|
|
21
20
|
END
|
|
22
21
|
spec.homepage = ''
|
|
23
22
|
spec.license = 'MIT'
|
|
@@ -27,6 +26,7 @@ Gem::Specification.new do |spec|
|
|
|
27
26
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
|
28
27
|
spec.require_paths = ['lib']
|
|
29
28
|
|
|
30
|
-
spec.add_development_dependency '
|
|
31
|
-
spec.add_development_dependency '
|
|
29
|
+
spec.add_development_dependency 'rake'
|
|
30
|
+
spec.add_development_dependency 'rspec'
|
|
31
|
+
spec.add_development_dependency 'simplecov'
|
|
32
32
|
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
require 'spec_helper'
|
|
2
|
+
require 'securerandom'
|
|
3
|
+
|
|
4
|
+
describe RequireCallbacks do
|
|
5
|
+
let(:file_name) { ('a'..'z').to_a.sample(8).join }
|
|
6
|
+
|
|
7
|
+
around(:each) do |example|
|
|
8
|
+
File.open("spec/#{file_name}.rb", 'w') do |f|
|
|
9
|
+
f.write("class #{file_name.upcase}; end")
|
|
10
|
+
end
|
|
11
|
+
example.run
|
|
12
|
+
File.delete("spec/#{file_name}.rb")
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
it 'has a version number' do
|
|
16
|
+
expect(RequireCallbacks::VERSION).not_to be nil
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
describe '#require' do
|
|
20
|
+
subject { require(file_name) }
|
|
21
|
+
|
|
22
|
+
it { is_expected.to be(true) }
|
|
23
|
+
|
|
24
|
+
context 'on second try' do
|
|
25
|
+
before { require(file_name) }
|
|
26
|
+
it { is_expected.to be(false) }
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
describe '#after_require' do
|
|
31
|
+
before do
|
|
32
|
+
@count = 0
|
|
33
|
+
after_require(file_name) { @count += 1 }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
it 'should run callback' do
|
|
37
|
+
expect(@count).to eq(0)
|
|
38
|
+
require(file_name)
|
|
39
|
+
expect(@count).to eq(1)
|
|
40
|
+
require(file_name)
|
|
41
|
+
expect(@count).to eq(1)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
|
|
45
|
+
describe '#before_require' do
|
|
46
|
+
before do
|
|
47
|
+
@count = 0
|
|
48
|
+
before_require(file_name) { @count += 1 }
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
it 'should run callback' do
|
|
52
|
+
expect(@count).to eq(0)
|
|
53
|
+
require(file_name)
|
|
54
|
+
expect(@count).to eq(1)
|
|
55
|
+
require(file_name)
|
|
56
|
+
expect(@count).to eq(2)
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
describe 'callbacks across receivers' do
|
|
60
|
+
let(:loader) { Object.new }
|
|
61
|
+
|
|
62
|
+
before do
|
|
63
|
+
@before_count = 0
|
|
64
|
+
@after_count = 0
|
|
65
|
+
before_require(file_name) { @before_count += 1 }
|
|
66
|
+
after_require(file_name) { @after_count += 1 }
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
it 'runs registered callbacks when a different object requires the file' do
|
|
70
|
+
expect(loader.send(:require, file_name)).to eq(true)
|
|
71
|
+
expect(@before_count).to eq(1)
|
|
72
|
+
expect(@after_count).to eq(1)
|
|
73
|
+
|
|
74
|
+
expect(loader.send(:require, file_name)).to eq(false)
|
|
75
|
+
expect(@before_count).to eq(2)
|
|
76
|
+
expect(@after_count).to eq(1)
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
CHANGED
|
@@ -1,54 +1,71 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: require_callbacks
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.0
|
|
4
|
+
version: 0.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- smudge
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: bin
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
|
-
name:
|
|
13
|
+
name: rake
|
|
15
14
|
requirement: !ruby/object:Gem::Requirement
|
|
16
15
|
requirements:
|
|
17
|
-
- - "
|
|
16
|
+
- - ">="
|
|
18
17
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: '
|
|
18
|
+
version: '0'
|
|
20
19
|
type: :development
|
|
21
20
|
prerelease: false
|
|
22
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
22
|
requirements:
|
|
24
|
-
- - "
|
|
23
|
+
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: '
|
|
25
|
+
version: '0'
|
|
27
26
|
- !ruby/object:Gem::Dependency
|
|
28
|
-
name:
|
|
27
|
+
name: rspec
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - ">="
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '0'
|
|
33
|
+
type: :development
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - ">="
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '0'
|
|
40
|
+
- !ruby/object:Gem::Dependency
|
|
41
|
+
name: simplecov
|
|
29
42
|
requirement: !ruby/object:Gem::Requirement
|
|
30
43
|
requirements:
|
|
31
|
-
- - "
|
|
44
|
+
- - ">="
|
|
32
45
|
- !ruby/object:Gem::Version
|
|
33
|
-
version: '
|
|
46
|
+
version: '0'
|
|
34
47
|
type: :development
|
|
35
48
|
prerelease: false
|
|
36
49
|
version_requirements: !ruby/object:Gem::Requirement
|
|
37
50
|
requirements:
|
|
38
|
-
- - "
|
|
51
|
+
- - ">="
|
|
39
52
|
- !ruby/object:Gem::Version
|
|
40
|
-
version: '
|
|
41
|
-
description: This gem gives you convenient hooks around calls to `
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
53
|
+
version: '0'
|
|
54
|
+
description: This gem gives you convenient hooks around calls to `require` which can
|
|
55
|
+
be used to define configuration or setup code that will eventually be run when the
|
|
56
|
+
library is actually loaded. This helps prevent loading unnecessary gems and configuration
|
|
57
|
+
code in contexts where they are not needed.
|
|
45
58
|
email:
|
|
46
59
|
- nathan@ngriffith.com
|
|
47
60
|
executables: []
|
|
48
61
|
extensions: []
|
|
49
62
|
extra_rdoc_files: []
|
|
50
63
|
files:
|
|
64
|
+
- ".codeclimate.yml"
|
|
65
|
+
- ".github/workflows/ruby.yml"
|
|
51
66
|
- ".gitignore"
|
|
67
|
+
- ".rubocop.yml"
|
|
68
|
+
- ".travis.yml"
|
|
52
69
|
- Gemfile
|
|
53
70
|
- LICENSE.txt
|
|
54
71
|
- README.md
|
|
@@ -56,11 +73,12 @@ files:
|
|
|
56
73
|
- lib/require_callbacks.rb
|
|
57
74
|
- lib/require_callbacks/version.rb
|
|
58
75
|
- require_callbacks.gemspec
|
|
76
|
+
- spec/callbacks_spec.rb
|
|
77
|
+
- spec/spec_helper.rb
|
|
59
78
|
homepage: ''
|
|
60
79
|
licenses:
|
|
61
80
|
- MIT
|
|
62
81
|
metadata: {}
|
|
63
|
-
post_install_message:
|
|
64
82
|
rdoc_options: []
|
|
65
83
|
require_paths:
|
|
66
84
|
- lib
|
|
@@ -75,11 +93,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
75
93
|
- !ruby/object:Gem::Version
|
|
76
94
|
version: '0'
|
|
77
95
|
requirements: []
|
|
78
|
-
|
|
79
|
-
rubygems_version: 2.2.2
|
|
80
|
-
signing_key:
|
|
96
|
+
rubygems_version: 4.0.24
|
|
81
97
|
specification_version: 4
|
|
82
98
|
summary: Convenient hooks to help prevent loading unnecessary gems and configuration
|
|
83
99
|
code.
|
|
84
|
-
test_files:
|
|
85
|
-
|
|
100
|
+
test_files:
|
|
101
|
+
- spec/callbacks_spec.rb
|
|
102
|
+
- spec/spec_helper.rb
|