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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: b5c48fd53e8868361a56e4b4c9404ac335281856
4
- data.tar.gz: 403899eae9b6559612eb67693a07be97f554a20b
2
+ SHA256:
3
+ metadata.gz: abadf1c04770cd5702f443da4d475fcd7e35929ea781c5611ec4765049fcce5d
4
+ data.tar.gz: 788717fdce91316dc9b18d61ced01fe616d19f598bd4e6288ce1e42227e88f5b
5
5
  SHA512:
6
- metadata.gz: c92ed68e70cb66dcaf8eb75c9ee9c775e2293497f6d723bc40fa25ebff1e82cff631c6e70ec2888600e36b7107827b998d29fc01ed9e399bacb29b8eb4736897
7
- data.tar.gz: ea7f92dfb528e801a145c9c25b3c2fd22ae5148c0b780d7959315f83acb664ba9796bb3b590bdb197d2c4ff5ccd4d6ba8c561c91f465f6849b5172385d6dd9c5
6
+ metadata.gz: 4d8b5cfef188beb1bc58765fb7fc1550d2a4cd1cbf43f1ad763903e16f9569e61b47e22f22c000df01b9ca0dc60d115a97edde1420c7cfcf73126b46823cc37c
7
+ data.tar.gz: 63a87d2156fd5b4c318638dc90d923ce0d9d818a1e3d73cac357a16f4c3efe90358b9ef5a2b31a5dccfeaf5002cd6ad282ef8ed516108c16e7243a394c931c3f
data/.codeclimate.yml ADDED
@@ -0,0 +1,11 @@
1
+ ---
2
+ engines:
3
+ fixme:
4
+ enabled: true
5
+ rubocop:
6
+ enabled: true
7
+ ratings:
8
+ paths:
9
+ - "**.rb"
10
+ exclude_paths:
11
+ - spec/**/*
@@ -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
@@ -0,0 +1,6 @@
1
+ Metrics/LineLength:
2
+ Max: 99
3
+ Style/AccessModifierIndentation:
4
+ EnforcedStyle: outdent
5
+ Style/Documentation:
6
+ Enabled: false
data/.travis.yml ADDED
@@ -0,0 +1,8 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.4
4
+ - 2.1.8
5
+ - 2.0.0-p648
6
+ script:
7
+ - bundle exec rake
8
+ - bundle exec codeclimate-test-reporter
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
- # require_callbacks
1
+ # require_callbacks [![Build Status](https://img.shields.io/travis/smudge/require_callbacks.svg)](https://travis-ci.org/smudge/require_callbacks) [![Code Climate](https://img.shields.io/codeclimate/github/smudge/require_callbacks.svg)](https://codeclimate.com/github/smudge/require_callbacks) [![Test Coverage](https://img.shields.io/codeclimate/coverage/github/smudge/require_callbacks.svg)](https://codeclimate.com/github/smudge/require_callbacks/coverage)
2
2
 
3
- This gem gives you convenient hooks around calls to `load`, `require`, and `require_relative`,
4
- which can be used to define configuration or setup code that will eventually be run when the
5
- library is actually loaded. This helps prevent loading unnecessary gems and configuration code in
6
- contexts where they are not needed.
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 `load`, `require`, and
59
- `require_relative` (respectively). Note that the `after_require` callbacks will only be run the
60
- *first* time (when `require` returns true), while `after_load` and all `before_` callbacks will
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 `Kernal` methods, so use at your own risk! There may be compatibility
88
- issues with anything else that modifies `load`, `require`, and `require_relative`.
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
@@ -1 +1,6 @@
1
1
  require 'bundler/gem_tasks'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task default: :spec
@@ -1,3 +1,3 @@
1
1
  module RequireCallbacks
2
- VERSION = '0.0.1'
2
+ VERSION = '0.1.0'
3
3
  end
@@ -1,25 +1,22 @@
1
1
  require 'require_callbacks/version'
2
2
 
3
3
  module RequireCallbacks
4
- %w(load require require_relative).each do |m|
5
- define_method("after_#{m}") do |name, &block|
6
- @after_load ||= {}
7
- @after_load[name] ||= []
8
- @after_load[name] << block
9
- end
4
+ BEFORE_CALLBACKS = {}
5
+ AFTER_CALLBACKS = {}
10
6
 
11
- define_method("before_#{m}") do |name, &block|
12
- @before_load ||= {}
13
- @before_load[name] ||= []
14
- @before_load[name] << block
15
- end
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
- define_method(m) do |*args|
18
- @after_load ||= {}
19
- @before_load ||= {}
20
- (@before_load[args.first] || []).each(&:call)
21
- super(*args) && !!(@after_load[args.first] || []).each(&:call)
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
 
@@ -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 `load`, `require`, and
17
- `require_relative`, which can be used to define configuration or setup code
18
- that will eventually be run when the library is actually loaded. This helps
19
- prevent loading unnecessary gems and configuration code in contexts where
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 'bundler', '~> 1.7'
31
- spec.add_development_dependency 'rake', '~> 10.0'
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
@@ -0,0 +1,5 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
4
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
5
+ require 'require_callbacks'
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.1
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: 2015-09-01 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
- name: bundler
13
+ name: rake
15
14
  requirement: !ruby/object:Gem::Requirement
16
15
  requirements:
17
- - - "~>"
16
+ - - ">="
18
17
  - !ruby/object:Gem::Version
19
- version: '1.7'
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: '1.7'
25
+ version: '0'
27
26
  - !ruby/object:Gem::Dependency
28
- name: rake
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: '10.0'
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: '10.0'
41
- description: This gem gives you convenient hooks around calls to `load`, `require`,
42
- and `require_relative`, which can be used to define configuration or setup code
43
- that will eventually be run when the library is actually loaded. This helps prevent
44
- loading unnecessary gems and configuration code in contexts where they are not needed.
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
- rubyforge_project:
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
- has_rdoc:
100
+ test_files:
101
+ - spec/callbacks_spec.rb
102
+ - spec/spec_helper.rb