degect 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +55 -0
- data/Rakefile +2 -0
- data/degect.gemspec +17 -0
- data/lib/degect.rb +6 -0
- data/lib/degect/degect.rb +11 -0
- data/lib/degect/version.rb +3 -0
- data/spec/degect/degect_spec.rb +129 -0
- metadata +56 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Steven Degutis
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Degect
|
2
|
+
|
3
|
+
Dependency injection made easy
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'degect'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install degect
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Your Account class:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Account
|
25
|
+
extend Degect
|
26
|
+
dependency(:authenticator) { RealAuthenticator.new }
|
27
|
+
|
28
|
+
def authenticate
|
29
|
+
authenticator.authenticate_with street_creds
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
Your Account tests:
|
35
|
+
|
36
|
+
```ruby
|
37
|
+
describe Account do
|
38
|
+
|
39
|
+
it 'authenticates well' do
|
40
|
+
fake_authenticator = FakeAuthenticator.new
|
41
|
+
subject.authenticator = fake_authenticator
|
42
|
+
|
43
|
+
subject.authenticate
|
44
|
+
|
45
|
+
fake_authenticator.should be_authenticated
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
## Contributing
|
52
|
+
|
53
|
+
1. Fork it
|
54
|
+
2. Write features with tests
|
55
|
+
3. Create new Pull Request
|
data/Rakefile
ADDED
data/degect.gemspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/degect/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Steven Degutis"]
|
6
|
+
gem.email = ["sdegutis@8thlight.com"]
|
7
|
+
gem.description = %q{Dependency injection helper method}
|
8
|
+
gem.summary = %q{Dependency injection}
|
9
|
+
gem.homepage = ""
|
10
|
+
|
11
|
+
gem.files = `git ls-files`.split($\)
|
12
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
13
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
14
|
+
gem.name = "degect"
|
15
|
+
gem.require_paths = ["lib"]
|
16
|
+
gem.version = Degect::VERSION
|
17
|
+
end
|
data/lib/degect.rb
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'degect'
|
2
|
+
|
3
|
+
describe Degect, 'with only a default dependency' do
|
4
|
+
|
5
|
+
it 'lets me specify the default dependency' do
|
6
|
+
my_class = Class.new do
|
7
|
+
extend Degect
|
8
|
+
dependency(:fetcher) { fake_method }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'lets me use the default dependency' do
|
13
|
+
my_class = Class.new do
|
14
|
+
extend Degect
|
15
|
+
dependency(:fetcher) { 3 }
|
16
|
+
end
|
17
|
+
|
18
|
+
my_class.new.fetcher.should == 3
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'lets me access self in the default dependency' do
|
22
|
+
my_class = Class.new do
|
23
|
+
extend Degect
|
24
|
+
dependency(:fetcher) { self }
|
25
|
+
end
|
26
|
+
|
27
|
+
foo = my_class.new
|
28
|
+
|
29
|
+
foo.fetcher.should == foo
|
30
|
+
end
|
31
|
+
|
32
|
+
it 'executes the dependency when i ask it to' do
|
33
|
+
my_class = Class.new do
|
34
|
+
extend Degect
|
35
|
+
dependency(:fetcher) { panic }
|
36
|
+
end
|
37
|
+
|
38
|
+
foo = my_class.new
|
39
|
+
foo.should_receive(:panic)
|
40
|
+
foo.fetcher
|
41
|
+
end
|
42
|
+
|
43
|
+
it 'does not execute the dependency until i ask it to' do
|
44
|
+
my_class = Class.new do
|
45
|
+
extend Degect
|
46
|
+
dependency(:fetcher) { panic }
|
47
|
+
end
|
48
|
+
|
49
|
+
foo = my_class.new
|
50
|
+
foo.should_not_receive(:panic)
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'caches the default dependency in memory on first use' do
|
54
|
+
my_class = Class.new do
|
55
|
+
extend Degect
|
56
|
+
dependency(:fetcher) { panic }
|
57
|
+
|
58
|
+
attr_reader :i
|
59
|
+
def panic
|
60
|
+
@i ||= 0
|
61
|
+
@i += 1
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
foo = my_class.new
|
66
|
+
foo.fetcher
|
67
|
+
foo.fetcher
|
68
|
+
foo.i.should == 1
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'allows multiple, independent dependencies' do
|
72
|
+
my_class = Class.new do
|
73
|
+
extend Degect
|
74
|
+
dependency(:runner) { run }
|
75
|
+
dependency(:walker) { walk }
|
76
|
+
|
77
|
+
attr_accessor :x
|
78
|
+
def run
|
79
|
+
self.x += 4
|
80
|
+
end
|
81
|
+
|
82
|
+
attr_accessor :y
|
83
|
+
def walk
|
84
|
+
self.y += 12
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
foo = my_class.new
|
89
|
+
foo.x = 0
|
90
|
+
foo.y = 0
|
91
|
+
|
92
|
+
foo.runner
|
93
|
+
foo.x.should == 4
|
94
|
+
foo.y.should == 0
|
95
|
+
|
96
|
+
foo.walker
|
97
|
+
foo.x.should == 4
|
98
|
+
foo.y.should == 12
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
describe Degect, 'with an overridden dependency' do
|
104
|
+
|
105
|
+
it 'doesnt call the original dependency' do
|
106
|
+
my_class = Class.new do
|
107
|
+
extend Degect
|
108
|
+
dependency(:fetcher) { panic }
|
109
|
+
end
|
110
|
+
|
111
|
+
foo = my_class.new
|
112
|
+
foo.fetcher = 7
|
113
|
+
foo.should_not_receive(:panic)
|
114
|
+
foo.fetcher
|
115
|
+
end
|
116
|
+
|
117
|
+
it 'calls the new dependency' do
|
118
|
+
my_class = Class.new do
|
119
|
+
extend Degect
|
120
|
+
dependency(:fetcher) { panic }
|
121
|
+
end
|
122
|
+
|
123
|
+
foo = my_class.new
|
124
|
+
foo.fetcher = -> { relax }
|
125
|
+
self.should_receive(:relax)
|
126
|
+
foo.fetcher.call
|
127
|
+
end
|
128
|
+
|
129
|
+
end
|
metadata
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: degect
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Steven Degutis
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-11 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Dependency injection helper method
|
15
|
+
email:
|
16
|
+
- sdegutis@8thlight.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- degect.gemspec
|
27
|
+
- lib/degect.rb
|
28
|
+
- lib/degect/degect.rb
|
29
|
+
- lib/degect/version.rb
|
30
|
+
- spec/degect/degect_spec.rb
|
31
|
+
homepage: ''
|
32
|
+
licenses: []
|
33
|
+
post_install_message:
|
34
|
+
rdoc_options: []
|
35
|
+
require_paths:
|
36
|
+
- lib
|
37
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
38
|
+
none: false
|
39
|
+
requirements:
|
40
|
+
- - ! '>='
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
44
|
+
none: false
|
45
|
+
requirements:
|
46
|
+
- - ! '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 1.8.21
|
52
|
+
signing_key:
|
53
|
+
specification_version: 3
|
54
|
+
summary: Dependency injection
|
55
|
+
test_files:
|
56
|
+
- spec/degect/degect_spec.rb
|