jerry 1.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ # A door
2
+ class Door; end
3
+
4
+ # A window
5
+ class Window; end
6
+
7
+ # A house which has a door and a window
8
+ class House
9
+ attr_reader :door, :window
10
+
11
+ def initialize(door, window)
12
+ @door = door
13
+ @window = window
14
+ end
15
+ end
@@ -0,0 +1,31 @@
1
+ require 'jerry'
2
+
3
+ module MultiDbApp
4
+ class Database
5
+ attr_reader :uri
6
+
7
+ def initialize(uri)
8
+ @uri = uri
9
+ end
10
+ end
11
+
12
+ class Application
13
+ attr_reader :foo_db, :bar_db
14
+
15
+ def initialize(foo_db, bar_db)
16
+ @foo_db = foo_db
17
+ @bar_db = bar_db
18
+ end
19
+ end
20
+
21
+ class Config < Jerry::Config
22
+ def initialize(foo_uri, bar_uri)
23
+ @foo_uri = foo_uri
24
+ @bar_uri = bar_uri
25
+ end
26
+
27
+ named_bind :foo_db, Database, [proc { @foo_uri }]
28
+ named_bind :bar_db, Database, [proc { @bar_uri }]
29
+ bind Application, [:foo_db, :bar_db]
30
+ end
31
+ end
@@ -0,0 +1,70 @@
1
+ module ShoppingCart
2
+ class Database
3
+ attr_reader :uri
4
+
5
+ def initialize(uri)
6
+ @uri = uri
7
+ end
8
+ end
9
+
10
+ class UserService
11
+ attr_reader :db
12
+
13
+ def initialize(db)
14
+ @db = db
15
+ end
16
+ end
17
+
18
+ class ProductService
19
+ attr_reader :db
20
+
21
+ def initialize(db)
22
+ @db = db
23
+ end
24
+ end
25
+
26
+ class ShoppingCartService
27
+ attr_reader :db, :product_service, :user_service
28
+
29
+ def initialize(db, product_service, user_service)
30
+ @db = db
31
+ @product_service = product_service
32
+ @user_service = user_service
33
+ end
34
+ end
35
+
36
+ class UserController
37
+ attr_reader :user_service
38
+
39
+ def initialize(user_service)
40
+ @user_service = user_service
41
+ end
42
+ end
43
+
44
+ class ProductController
45
+ attr_reader :product_service
46
+
47
+ def initialize(product_service)
48
+ @product_service = product_service
49
+ end
50
+ end
51
+
52
+ class ShoppingCartController
53
+ attr_reader :shopping_cart_service
54
+
55
+ def initialize(shopping_cart_service)
56
+ @shopping_cart_service = shopping_cart_service
57
+ end
58
+ end
59
+
60
+ class Application
61
+ attr_reader :user_controller, :product_controller, :shopping_cart_controller
62
+
63
+ def initialize(user_controller, product_controller,
64
+ shopping_cart_controller)
65
+ @user_controller = user_controller
66
+ @product_controller = product_controller
67
+ @shopping_cart_controller = shopping_cart_controller
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,27 @@
1
+ require 'jerry'
2
+
3
+ module ShoppingCart
4
+ class DatabaseConfig < Jerry::Config
5
+ bind Database, [proc { 'foo://localhost:9001' }]
6
+ end
7
+
8
+ class UserConfig < Jerry::Config
9
+ bind UserService, [Database]
10
+ bind UserController, [UserService]
11
+ end
12
+
13
+ class ProductConfig < Jerry::Config
14
+ bind ProductService, [Database]
15
+ bind ProductController, [ProductService]
16
+ end
17
+
18
+ class ShoppingCartConfig < Jerry::Config
19
+ bind ShoppingCartService, [Database, ProductService, UserService]
20
+ bind ShoppingCartController, [ShoppingCartService]
21
+ end
22
+
23
+ class ApplicationConfig < Jerry::Config
24
+ bind Application,
25
+ [UserController, ProductController, ShoppingCartController]
26
+ end
27
+ end
data/spec/jerry_spec.rb CHANGED
@@ -1,132 +1,67 @@
1
- require 'rspec'
2
1
  require 'jerry'
3
2
 
4
3
  describe Jerry do
5
- it 'should work with all the pieces' do
6
- house = Class.new do
7
- attr_reader :window, :door
8
-
9
- def initialize(window, door)
10
- @window = window
11
- @door = door
12
- end
13
- end
14
- window = Class.new
15
- door = Class.new
16
-
17
- config = Class.new(Jerry::Config) do
18
- component(:window) { window.new }
19
- component(:door) { door.new }
20
- component(:house) {
21
- house.new rig(:window), rig(:door)
22
- }
23
- end
24
- jerry = Jerry.new config.new
25
-
26
- instance = jerry.rig :house
27
-
28
- expect(instance).not_to be_nil
29
- expect(instance).to be_a house
30
- expect(instance.window).not_to be_nil
31
- expect(instance.window).to be_a window
32
- expect(instance.door).not_to be_nil
33
- expect(instance.door).to be_a door
4
+ def double_config(name, fields = {})
5
+ config = double name, fields
6
+ allow(config).to receive(:jerry=)
7
+ config
34
8
  end
35
9
 
36
- describe '#rig' do
37
- it 'should raise error if the component does not exist' do
38
- jerry = Jerry.new
39
-
40
- expect{jerry.rig :not_actually_a_thing}.to raise_error(Jerry::RigError)
41
- end
42
-
43
- it 'should call the component method on the config' do
44
- config = double('config', components: [:doomsday_device]).as_null_object
45
- jerry = Jerry.new config
46
-
47
- expect(config).to receive(:doomsday_device).with(no_args)
48
-
49
- jerry.rig :doomsday_device
50
- end
51
-
52
- it 'should call the component method registered last' do
53
- target = double('target')
54
- jerry = Jerry.new double('old config', components: [:target]).as_null_object,
55
- double('new config', components: [:target], target: target).as_null_object
10
+ it 'should set the jerry attribute on the configs' do
11
+ alfa = spy 'alfa config'
12
+ bravo = spy 'bravo config'
13
+ charlie = spy 'charlie config'
56
14
 
57
- component = jerry.rig :target
15
+ jerry = Jerry.new alfa, bravo, charlie
58
16
 
59
- expect(component).to eq(target)
60
- end
17
+ expect(alfa).to have_received(:jerry=).with(jerry)
18
+ expect(bravo).to have_received(:jerry=).with(jerry)
19
+ expect(charlie).to have_received(:jerry=).with(jerry)
61
20
  end
62
21
 
63
- describe '#<<' do
64
- let(:jerry) { Jerry.new }
65
-
66
- it 'should fetch the config components' do
67
- config = double('config').as_null_object
68
-
69
- expect(config).to receive(:components).and_return([])
70
-
71
- jerry << config
72
- end
73
-
74
- it 'should register components' do
75
- config = double('config', components: [:target]).as_null_object
76
-
77
- jerry << config
78
-
79
- expect(jerry.knows? :target).to be_truthy
80
- end
81
-
82
- it 'should overwrite previous components' do
83
- target = double('target')
84
- config = double('config', components: [:target], target: target).as_null_object
85
-
86
- jerry << config
87
-
88
- expect(jerry.rig :target).to eq(target)
89
- end
90
-
91
- it 'should pass an instance of itself to the config' do
92
- config = double('config', components: []).as_null_object
22
+ describe '#[]' do
23
+ it 'should delegate to configs' do
24
+ config = double_config 'config'
25
+ allow(config).to receive(:knows?).and_return(true)
26
+ jerry = Jerry.new config
93
27
 
94
- expect(config).to receive(:jerry=).with(jerry)
28
+ expect(config).to receive(:[]).with(House)
95
29
 
96
- jerry << config
30
+ jerry[House]
97
31
  end
98
- end
99
32
 
100
- describe '#knows?' do
101
- it 'should be true when the component is defined by a config' do
102
- jerry = Jerry.new double('config', components: [:target]).as_null_object
33
+ it 'should return the provided value from the config' do
34
+ instance = double 'some value'
35
+ config = double_config 'config'
36
+ allow(config).to receive(:knows?).and_return(true)
37
+ allow(config).to receive(:[]).and_return(instance)
38
+ jerry = Jerry.new config
103
39
 
104
- expect(jerry.knows? :target).to be_truthy
40
+ expect(jerry[House]).to eq instance
105
41
  end
106
42
 
107
- it 'should be false if the component is defined by no config' do
108
- jerry = Jerry.new double('config', components: [:not_target]).as_null_object
109
-
110
- expect(jerry.knows? :target).to be_falsey
43
+ it 'should prioritize configs by their order in the constructor' do
44
+ alfa = double_config 'alfa config'
45
+ allow(alfa).to receive(:knows?).and_return(false)
46
+ bravo = double_config 'bravo config'
47
+ allow(bravo).to receive(:knows?).and_return(true)
48
+ bravo_instance = double 'instance from bravo'
49
+ allow(bravo).to receive(:[]).and_return(bravo_instance)
50
+ charlie = double_config 'charlie config'
51
+ allow(charlie).to receive(:knows?).and_return(true)
52
+ charlie_instance = double 'instance from charlie'
53
+ allow(charlie).to receive(:[]).and_return(charlie_instance)
54
+
55
+ jerry = Jerry.new alfa, bravo, charlie
56
+
57
+ expect(jerry[:something]).to eq bravo_instance
111
58
  end
112
- end
113
59
 
114
- describe '#initialize' do
115
- it 'should register all the config components' do
116
- configs = 'a'.upto('c').map {|i| double("config #{i}", components: [i.to_sym]).as_null_object}
60
+ it 'should fail if no config know the key' do
61
+ configs = 3.times.map { double_config 'some config', knows?: false }
117
62
  jerry = Jerry.new(*configs)
118
63
 
119
- expect(jerry.knows? :a).to be_truthy
120
- expect(jerry.knows? :b).to be_truthy
121
- expect(jerry.knows? :c).to be_truthy
122
- end
123
-
124
- it 'should give priority to the components of later configs' do
125
- target = double('target')
126
- jerry = Jerry.new double('old config', components: [:target]).as_null_object,
127
- double('new config', components: [:target], target: target).as_null_object
128
-
129
- expect(jerry.rig :target).to eq(target)
64
+ expect { jerry[:not_there] }.to raise_error(Jerry::InstantiationError)
130
65
  end
131
66
  end
132
- end
67
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,9 +1,7 @@
1
- Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
1
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
2
2
 
3
- silence_warnings do
4
- require 'coveralls'
5
- Coveralls.wear!
6
- end
3
+ require 'coveralls'
4
+ Coveralls.wear!
7
5
 
8
6
  RSpec.configure do |config|
9
7
  config.order = :random
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jerry
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 2.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Boris Bera
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-07-05 00:00:00.000000000 Z
11
+ date: 2015-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -38,34 +38,118 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: guard
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard-rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
41
69
  - !ruby/object:Gem::Dependency
42
70
  name: rspec
43
71
  requirement: !ruby/object:Gem::Requirement
44
72
  requirements:
45
73
  - - "~>"
46
74
  - !ruby/object:Gem::Version
47
- version: 3.0.0
75
+ version: '3.3'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.3'
83
+ - !ruby/object:Gem::Dependency
84
+ name: coveralls
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '0.8'
48
90
  type: :development
49
91
  prerelease: false
50
92
  version_requirements: !ruby/object:Gem::Requirement
51
93
  requirements:
52
94
  - - "~>"
53
95
  - !ruby/object:Gem::Version
54
- version: 3.0.0
96
+ version: '0.8'
55
97
  - !ruby/object:Gem::Dependency
56
98
  name: yard
57
99
  requirement: !ruby/object:Gem::Requirement
58
100
  requirements:
59
101
  - - "~>"
60
102
  - !ruby/object:Gem::Version
61
- version: 0.8.7.4
103
+ version: 0.8.7
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 0.8.7
111
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.10'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.10'
125
+ - !ruby/object:Gem::Dependency
126
+ name: rubocop
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '0.32'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '0.32'
139
+ - !ruby/object:Gem::Dependency
140
+ name: reek
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '2.2'
62
146
  type: :development
63
147
  prerelease: false
64
148
  version_requirements: !ruby/object:Gem::Requirement
65
149
  requirements:
66
150
  - - "~>"
67
151
  - !ruby/object:Gem::Version
68
- version: 0.8.7.4
152
+ version: '2.2'
69
153
  description: Jerry is an Inversion of Control container. It allows you to decouple
70
154
  the code that bootstraps your application from the rest of your application
71
155
  email:
@@ -75,21 +159,38 @@ extensions: []
75
159
  extra_rdoc_files: []
76
160
  files:
77
161
  - ".gitignore"
162
+ - ".reek"
78
163
  - ".rspec"
164
+ - ".rubocop.yml"
79
165
  - ".travis.yml"
80
166
  - ".yardopts"
81
167
  - Gemfile
168
+ - Guardfile
82
169
  - LICENSE.txt
83
170
  - README.md
84
171
  - Rakefile
172
+ - bin/console
173
+ - bin/guard
174
+ - bin/rake
175
+ - bin/setup
176
+ - doc/multiple-configurations.md
85
177
  - jerry.gemspec
86
178
  - lib/jerry.rb
179
+ - lib/jerry/class_provider.rb
87
180
  - lib/jerry/config.rb
181
+ - lib/jerry/errors.rb
88
182
  - lib/jerry/version.rb
183
+ - spec/class_provider_spec.rb
89
184
  - spec/config_spec.rb
185
+ - spec/error_spec.rb
186
+ - spec/examples_spec.rb
187
+ - spec/fixtures/db_app.rb
188
+ - spec/fixtures/house.rb
189
+ - spec/fixtures/multi_db_app.rb
190
+ - spec/fixtures/shopping_cart.rb
191
+ - spec/fixtures/shopping_cart_config.rb
90
192
  - spec/jerry_spec.rb
91
193
  - spec/spec_helper.rb
92
- - spec/support/silence_warnings.rb
93
194
  homepage: http://github.com/beraboris/jerry
94
195
  licenses:
95
196
  - MIT
@@ -110,13 +211,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
110
211
  version: '0'
111
212
  requirements: []
112
213
  rubyforge_project:
113
- rubygems_version: 2.2.2
214
+ rubygems_version: 2.4.5.1
114
215
  signing_key:
115
216
  specification_version: 4
116
217
  summary: Jerry rigs your application together. It's an Inversion of Control container.
117
218
  test_files:
219
+ - spec/class_provider_spec.rb
118
220
  - spec/config_spec.rb
221
+ - spec/error_spec.rb
222
+ - spec/examples_spec.rb
223
+ - spec/fixtures/db_app.rb
224
+ - spec/fixtures/house.rb
225
+ - spec/fixtures/multi_db_app.rb
226
+ - spec/fixtures/shopping_cart.rb
227
+ - spec/fixtures/shopping_cart_config.rb
119
228
  - spec/jerry_spec.rb
120
229
  - spec/spec_helper.rb
121
- - spec/support/silence_warnings.rb
122
230
  has_rdoc:
@@ -1,7 +0,0 @@
1
- def silence_warnings
2
- original = $VERBOSE
3
- $VERBOSE = nil
4
- result = yield
5
- $VERBOSE = original
6
- result
7
- end