require_callbacks 0.0.2 → 0.1.1

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: dcbc6813e451b2df6f157c5ead86f08a3ca4112f
4
- data.tar.gz: 1d437b44724e7838900c85a6082972ab674b3d68
2
+ SHA256:
3
+ metadata.gz: 544d6aa7e063395cf8bcb87139921c0736dc59a8c49d2ef6c971bae023983bdc
4
+ data.tar.gz: 4758a20d7ab472610e658e05c5d135d570999033c22dcaa10d96cf6b8453a5c7
5
5
  SHA512:
6
- metadata.gz: 7e3a0c0a823325a79ed7129e5c08b01e8fbd97f84f025ca184d760957d71d46555999ccb3dd2a8e3d1324cb40d6f5c35cd67d41c9ed27e23df553b9fb61ade21
7
- data.tar.gz: a2a41e8522e97317a6d239c7af84e37b322d3b3be046d5973fb63a1de132326f3f459e710d1baaa91b9314c20b7f8f2d656ae042d8db9fde148c546df5a17479
6
+ metadata.gz: 714a53b31be6f808822379db580cda5f2bcd2e470477810208fff8c57a4bc18d45c6f9a94961541281867c8c98bdbeb6b5a21c50931970b1802d3d41154d55f2
7
+ data.tar.gz: c79b1d312cc0348031780cbc72fc200a4e7787473bc49de20bc9dfb5d7ba538ff22a6d7a0403ca76800312005936b2dae7813862c53e69b3c3ca77e865dc23c9
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.2'
2
+ VERSION = '0.1.1'
3
3
  end
@@ -1,23 +1,24 @@
1
1
  require 'require_callbacks/version'
2
2
 
3
3
  module RequireCallbacks
4
+ BEFORE_CALLBACKS = {}
5
+ AFTER_CALLBACKS = {}
6
+
4
7
  def after_require(name, &block)
5
- @after_require ||= {}
6
- @after_require[name] ||= []
7
- @after_require[name] << block
8
+ raise ArgumentError, 'callback block required' unless block
9
+ (AFTER_CALLBACKS[name] ||= []) << block
8
10
  end
9
11
 
10
12
  def before_require(name, &block)
11
- @before_require ||= {}
12
- @before_require[name] ||= []
13
- @before_require[name] << block
13
+ raise ArgumentError, 'callback block required' unless block
14
+ (BEFORE_CALLBACKS[name] ||= []) << block
14
15
  end
15
16
 
16
17
  def require(*args)
17
- @after_require ||= {}
18
- @before_require ||= {}
19
- (@before_require[args.first] || []).each(&:call)
20
- super && !!(@after_require[args.first] || []).each(&:call)
18
+ (BEFORE_CALLBACKS[args.first] || []).each(&:call)
19
+ loaded = super
20
+ (AFTER_CALLBACKS[args.first] || []).each(&:call) if loaded
21
+ loaded
21
22
  end
22
23
  end
23
24
 
@@ -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,7 +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'
32
- spec.add_development_dependency 'rspec', '~> 3.3.0'
29
+ spec.add_development_dependency 'rake'
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'simplecov'
33
32
  end
@@ -16,6 +16,18 @@ describe RequireCallbacks do
16
16
  expect(RequireCallbacks::VERSION).not_to be nil
17
17
  end
18
18
 
19
+ describe 'callback registration without a block' do
20
+ it 'rejects after_require without changing callbacks' do
21
+ expect { after_require(file_name) }.to raise_error(ArgumentError, 'callback block required')
22
+ expect(RequireCallbacks::AFTER_CALLBACKS).not_to have_key(file_name)
23
+ end
24
+
25
+ it 'rejects before_require without changing callbacks' do
26
+ expect { before_require(file_name) }.to raise_error(ArgumentError, 'callback block required')
27
+ expect(RequireCallbacks::BEFORE_CALLBACKS).not_to have_key(file_name)
28
+ end
29
+ end
30
+
19
31
  describe '#require' do
20
32
  subject { require(file_name) }
21
33
 
@@ -56,4 +68,24 @@ describe RequireCallbacks do
56
68
  expect(@count).to eq(2)
57
69
  end
58
70
  end
71
+ describe 'callbacks across receivers' do
72
+ let(:loader) { Object.new }
73
+
74
+ before do
75
+ @before_count = 0
76
+ @after_count = 0
77
+ before_require(file_name) { @before_count += 1 }
78
+ after_require(file_name) { @after_count += 1 }
79
+ end
80
+
81
+ it 'runs registered callbacks when a different object requires the file' do
82
+ expect(loader.send(:require, file_name)).to eq(true)
83
+ expect(@before_count).to eq(1)
84
+ expect(@after_count).to eq(1)
85
+
86
+ expect(loader.send(:require, file_name)).to eq(false)
87
+ expect(@before_count).to eq(2)
88
+ expect(@after_count).to eq(1)
89
+ end
90
+ end
59
91
  end
data/spec/spec_helper.rb CHANGED
@@ -1,2 +1,5 @@
1
+ require 'simplecov'
2
+ SimpleCov.start
3
+
1
4
  $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
5
  require 'require_callbacks'
metadata CHANGED
@@ -1,68 +1,71 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: require_callbacks
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - smudge
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2015-09-03 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
29
28
  requirement: !ruby/object:Gem::Requirement
30
29
  requirements:
31
- - - "~>"
30
+ - - ">="
32
31
  - !ruby/object:Gem::Version
33
- version: '10.0'
32
+ version: '0'
34
33
  type: :development
35
34
  prerelease: false
36
35
  version_requirements: !ruby/object:Gem::Requirement
37
36
  requirements:
38
- - - "~>"
37
+ - - ">="
39
38
  - !ruby/object:Gem::Version
40
- version: '10.0'
39
+ version: '0'
41
40
  - !ruby/object:Gem::Dependency
42
- name: rspec
41
+ name: simplecov
43
42
  requirement: !ruby/object:Gem::Requirement
44
43
  requirements:
45
- - - "~>"
44
+ - - ">="
46
45
  - !ruby/object:Gem::Version
47
- version: 3.3.0
46
+ version: '0'
48
47
  type: :development
49
48
  prerelease: false
50
49
  version_requirements: !ruby/object:Gem::Requirement
51
50
  requirements:
52
- - - "~>"
51
+ - - ">="
53
52
  - !ruby/object:Gem::Version
54
- version: 3.3.0
55
- description: This gem gives you convenient hooks around calls to `load`, `require`,
56
- and `require_relative`, which can be used to define configuration or setup code
57
- that will eventually be run when the library is actually loaded. This helps prevent
58
- 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.
59
58
  email:
60
59
  - nathan@ngriffith.com
61
60
  executables: []
62
61
  extensions: []
63
62
  extra_rdoc_files: []
64
63
  files:
64
+ - ".codeclimate.yml"
65
+ - ".github/workflows/ruby.yml"
65
66
  - ".gitignore"
67
+ - ".rubocop.yml"
68
+ - ".travis.yml"
66
69
  - Gemfile
67
70
  - LICENSE.txt
68
71
  - README.md
@@ -76,7 +79,6 @@ homepage: ''
76
79
  licenses:
77
80
  - MIT
78
81
  metadata: {}
79
- post_install_message:
80
82
  rdoc_options: []
81
83
  require_paths:
82
84
  - lib
@@ -91,9 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
91
93
  - !ruby/object:Gem::Version
92
94
  version: '0'
93
95
  requirements: []
94
- rubyforge_project:
95
- rubygems_version: 2.4.5.1
96
- signing_key:
96
+ rubygems_version: 4.0.24
97
97
  specification_version: 4
98
98
  summary: Convenient hooks to help prevent loading unnecessary gems and configuration
99
99
  code.