require_callbacks 0.0.1 → 0.0.2

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
  SHA1:
3
- metadata.gz: b5c48fd53e8868361a56e4b4c9404ac335281856
4
- data.tar.gz: 403899eae9b6559612eb67693a07be97f554a20b
3
+ metadata.gz: dcbc6813e451b2df6f157c5ead86f08a3ca4112f
4
+ data.tar.gz: 1d437b44724e7838900c85a6082972ab674b3d68
5
5
  SHA512:
6
- metadata.gz: c92ed68e70cb66dcaf8eb75c9ee9c775e2293497f6d723bc40fa25ebff1e82cff631c6e70ec2888600e36b7107827b998d29fc01ed9e399bacb29b8eb4736897
7
- data.tar.gz: ea7f92dfb528e801a145c9c25b3c2fd22ae5148c0b780d7959315f83acb664ba9796bb3b590bdb197d2c4ff5ccd4d6ba8c561c91f465f6849b5172385d6dd9c5
6
+ metadata.gz: 7e3a0c0a823325a79ed7129e5c08b01e8fbd97f84f025ca184d760957d71d46555999ccb3dd2a8e3d1324cb40d6f5c35cd67d41c9ed27e23df553b9fb61ade21
7
+ data.tar.gz: a2a41e8522e97317a6d239c7af84e37b322d3b3be046d5973fb63a1de132326f3f459e710d1baaa91b9314c20b7f8f2d656ae042d8db9fde148c546df5a17479
@@ -1,25 +1,23 @@
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
+ def after_require(name, &block)
5
+ @after_require ||= {}
6
+ @after_require[name] ||= []
7
+ @after_require[name] << block
8
+ end
10
9
 
11
- define_method("before_#{m}") do |name, &block|
12
- @before_load ||= {}
13
- @before_load[name] ||= []
14
- @before_load[name] << block
15
- end
10
+ def before_require(name, &block)
11
+ @before_require ||= {}
12
+ @before_require[name] ||= []
13
+ @before_require[name] << block
14
+ end
16
15
 
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
16
+ def require(*args)
17
+ @after_require ||= {}
18
+ @before_require ||= {}
19
+ (@before_require[args.first] || []).each(&:call)
20
+ super && !!(@after_require[args.first] || []).each(&:call)
23
21
  end
24
22
  end
25
23
 
@@ -1,3 +1,3 @@
1
1
  module RequireCallbacks
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -29,4 +29,5 @@ Gem::Specification.new do |spec|
29
29
 
30
30
  spec.add_development_dependency 'bundler', '~> 1.7'
31
31
  spec.add_development_dependency 'rake', '~> 10.0'
32
+ spec.add_development_dependency 'rspec', '~> 3.3.0'
32
33
  end
@@ -0,0 +1,59 @@
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
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'require_callbacks'
metadata CHANGED
@@ -1,14 +1,14 @@
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.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - smudge
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-01 00:00:00.000000000 Z
11
+ date: 2015-09-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 3.3.0
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 3.3.0
41
55
  description: This gem gives you convenient hooks around calls to `load`, `require`,
42
56
  and `require_relative`, which can be used to define configuration or setup code
43
57
  that will eventually be run when the library is actually loaded. This helps prevent
@@ -56,6 +70,8 @@ files:
56
70
  - lib/require_callbacks.rb
57
71
  - lib/require_callbacks/version.rb
58
72
  - require_callbacks.gemspec
73
+ - spec/callbacks_spec.rb
74
+ - spec/spec_helper.rb
59
75
  homepage: ''
60
76
  licenses:
61
77
  - MIT
@@ -76,10 +92,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
76
92
  version: '0'
77
93
  requirements: []
78
94
  rubyforge_project:
79
- rubygems_version: 2.2.2
95
+ rubygems_version: 2.4.5.1
80
96
  signing_key:
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