dependency_injection 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/Gemfile.lock +2 -2
- data/README.md +2 -0
- data/lib/dependency_injection/container.rb +5 -1
- data/lib/dependency_injection/definition.rb +22 -3
- data/lib/dependency_injection/loaders/yaml.rb +8 -2
- data/lib/dependency_injection/version.rb +1 -1
- data/test/dependency_injection/loaders/test_yaml.rb +12 -4
- data/test/dependency_injection/test_container.rb +9 -0
- data/test/dependency_injection/test_definition.rb +37 -12
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bee91995df1f5fc2220d223c7387a9e4c384e9fe
|
4
|
+
data.tar.gz: 15be55e7503b817ee7d3d79d1e88eb143c752485
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a519c2ff82347e5ea4718adde01b6b87fc80b29d8409b4e2f4e1567ffbe551923a8ad79351271ffb6d83036360cf868c2342d7cfd8f5817da242df71928d7215
|
7
|
+
data.tar.gz: 1d12edeafb9daa99ffc1919f9f9f834fff87043681cb3572086413345f4b2076a85cce1b5886468e8e63530ddc2bf306cab356ba8460f48dd446c5263078aba9
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
dependency_injection (0.
|
4
|
+
dependency_injection (0.4.0)
|
5
5
|
activesupport
|
6
6
|
|
7
7
|
GEM
|
@@ -17,7 +17,7 @@ GEM
|
|
17
17
|
rest-client
|
18
18
|
simplecov (>= 0.7)
|
19
19
|
thor
|
20
|
-
i18n (0.6.
|
20
|
+
i18n (0.6.9)
|
21
21
|
metaclass (0.0.1)
|
22
22
|
mime-types (1.23)
|
23
23
|
minitest (5.0.6)
|
data/README.md
CHANGED
@@ -172,6 +172,8 @@ And here's some more details about each keyword:
|
|
172
172
|
|
173
173
|
* `calls`: An array containing an array of each instance method and its parameters. Note that you only need to define the methods during your class instantiation.
|
174
174
|
|
175
|
+
* `file_path`: A string containing the file path to require to load the class.
|
176
|
+
|
175
177
|
* `lazy`: Returns a Proxy Object if true. The _real_ object will only be instantiated at the first method call.
|
176
178
|
|
177
179
|
* `alias`: A string containing the target service name.
|
@@ -3,11 +3,12 @@ require 'dependency_injection/scope_widening_injection_error'
|
|
3
3
|
|
4
4
|
module DependencyInjection
|
5
5
|
class Definition
|
6
|
-
attr_accessor :arguments, :configurator, :klass_name, :method_calls, :scope
|
6
|
+
attr_accessor :arguments, :configurator, :file_path, :klass_name, :method_calls, :scope
|
7
7
|
|
8
8
|
def initialize(klass_name, container)
|
9
9
|
@container = container
|
10
10
|
self.arguments = []
|
11
|
+
self.file_path = nil
|
11
12
|
self.klass_name = klass_name
|
12
13
|
self.method_calls = {}
|
13
14
|
self.scope = :container
|
@@ -50,6 +51,7 @@ module DependencyInjection
|
|
50
51
|
end
|
51
52
|
|
52
53
|
def initialize_object
|
54
|
+
require_object
|
53
55
|
object = self.klass.new(*resolve(self.arguments))
|
54
56
|
self.method_calls.each { |method_name, arguments| object.send(method_name, *resolve(arguments)) }
|
55
57
|
if self.configurator
|
@@ -61,10 +63,26 @@ module DependencyInjection
|
|
61
63
|
object
|
62
64
|
end
|
63
65
|
|
66
|
+
def object_already_required?
|
67
|
+
true if Kernel.const_get(self.klass_name)
|
68
|
+
rescue
|
69
|
+
false
|
70
|
+
end
|
71
|
+
|
64
72
|
def prototype_scoped_object
|
65
73
|
initialize_object
|
66
74
|
end
|
67
75
|
|
76
|
+
def require_object
|
77
|
+
return if object_already_required?
|
78
|
+
|
79
|
+
if self.file_path
|
80
|
+
require self.file_path
|
81
|
+
else
|
82
|
+
require self.klass_name.underscore
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
68
86
|
def resolve(arguments)
|
69
87
|
resolve_references(resolve_container_parameters(arguments))
|
70
88
|
end
|
@@ -82,8 +100,9 @@ module DependencyInjection
|
|
82
100
|
def resolve_references(arguments)
|
83
101
|
arguments.map do |argument|
|
84
102
|
if /^@(?<reference_name>.*)/ =~ argument
|
85
|
-
|
86
|
-
|
103
|
+
reference_definition = @container.find(reference_name)
|
104
|
+
reference = reference_definition.object
|
105
|
+
raise ScopeWideningInjectionError if reference_definition.scope == :prototype && scope == :container
|
87
106
|
|
88
107
|
reference
|
89
108
|
else
|
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'erb'
|
1
2
|
require 'yaml'
|
2
3
|
|
3
4
|
module DependencyInjection
|
@@ -8,7 +9,7 @@ module DependencyInjection
|
|
8
9
|
end
|
9
10
|
|
10
11
|
def load(filename)
|
11
|
-
file =
|
12
|
+
file = load_file(filename)
|
12
13
|
add_parameters(file['parameters']) if file['parameters']
|
13
14
|
add_services(file['services']) if file['services']
|
14
15
|
end
|
@@ -38,7 +39,8 @@ module DependencyInjection
|
|
38
39
|
def add_standard_service(name, parameters)
|
39
40
|
lazy_load = parameters['lazy'] || false
|
40
41
|
definition = @container.register(name, parameters['class'], lazy_load)
|
41
|
-
definition.scope
|
42
|
+
definition.scope = parameters['scope'] if parameters['scope']
|
43
|
+
definition.file_path = parameters['file_path'] if parameters['file_path']
|
42
44
|
definition.add_arguments(*parameters['arguments']) if parameters['arguments']
|
43
45
|
if (configurator = parameters['configurator'])
|
44
46
|
definition.add_configurator(configurator[0], configurator[1])
|
@@ -47,6 +49,10 @@ module DependencyInjection
|
|
47
49
|
parameters['calls'].each { |method_name, arguments| definition.add_method_call(method_name, *arguments) }
|
48
50
|
end
|
49
51
|
end
|
52
|
+
|
53
|
+
def load_file(filename)
|
54
|
+
YAML::load(ERB.new(IO.read(filename)).result)
|
55
|
+
end
|
50
56
|
end
|
51
57
|
end
|
52
58
|
end
|
@@ -8,28 +8,28 @@ class TestYaml < Minitest::Test
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def test_loading_file_without_parameters
|
11
|
-
|
11
|
+
@yaml_loader.stubs(:load_file).with('services.yml').returns({ 'services' => [] })
|
12
12
|
@yaml_loader.expects(:add_parameters).never
|
13
13
|
|
14
14
|
@yaml_loader.load('services.yml')
|
15
15
|
end
|
16
16
|
|
17
17
|
def test_loading_file_with_parameters
|
18
|
-
|
18
|
+
@yaml_loader.stubs(:load_file).with('services.yml').returns({'parameters' => { 'key_1' => 'value_1' }, 'services' => [] })
|
19
19
|
@yaml_loader.expects(:add_parameters).with({ 'key_1' => 'value_1' })
|
20
20
|
|
21
21
|
@yaml_loader.load('services.yml')
|
22
22
|
end
|
23
23
|
|
24
24
|
def test_loading_file_without_services
|
25
|
-
|
25
|
+
@yaml_loader.stubs(:load_file).with('services.yml').returns({ 'parameters' => [] })
|
26
26
|
@yaml_loader.expects(:add_services).never
|
27
27
|
|
28
28
|
@yaml_loader.load('services.yml')
|
29
29
|
end
|
30
30
|
|
31
31
|
def test_loading_file_with_services
|
32
|
-
|
32
|
+
@yaml_loader.stubs(:load_file).with('services.yml').returns({ 'parameters' => {}, 'services' => { 'service_1' => { 'class' => 'MyKlass' }}})
|
33
33
|
@yaml_loader.expects(:add_services).with({ 'service_1' => { 'class' => 'MyKlass' }})
|
34
34
|
|
35
35
|
@yaml_loader.load('services.yml')
|
@@ -152,4 +152,12 @@ class TestYaml < Minitest::Test
|
|
152
152
|
|
153
153
|
@yaml_loader.send(:add_standard_service, 'key_1', { 'class' => 'MyKlass', 'configurator' => ['ConfiguratorKlass', 'method_name'] })
|
154
154
|
end
|
155
|
+
|
156
|
+
def test_adding_standard_service_with_file_path
|
157
|
+
definition = mock
|
158
|
+
@container.stubs(:register).with('key_1', 'MyKlass', false).returns(definition)
|
159
|
+
definition.expects(:file_path=).with('path/to/file')
|
160
|
+
|
161
|
+
@yaml_loader.send(:add_standard_service, 'key_1', { 'class' => 'MyKlass', 'file_path' => 'path/to/file' })
|
162
|
+
end
|
155
163
|
end
|
@@ -29,6 +29,15 @@ class TestContainer < Minitest::Test
|
|
29
29
|
assert_equal({ 'my.parameter' => 'other value' }, @container.parameters)
|
30
30
|
end
|
31
31
|
|
32
|
+
def test_find_a_registered_definition_returns_a_definition
|
33
|
+
@container.register('my_definition', 'MyDefinition')
|
34
|
+
assert_equal(@definition, @container.find('my_definition'))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_find_a_not_registered_definition_returns_nil
|
38
|
+
assert_equal(nil, @container.find('my_definition'))
|
39
|
+
end
|
40
|
+
|
32
41
|
def test_getting_a_registered_definition_returns_an_object
|
33
42
|
@container.register('my_definition', 'MyDefinition')
|
34
43
|
assert_equal(@final_object, @container.get('my_definition'))
|
@@ -71,6 +71,7 @@ class TestDefinition < Minitest::Test
|
|
71
71
|
end
|
72
72
|
|
73
73
|
def test_getting_object_with_arguments
|
74
|
+
@definition.stubs(:require_object).returns(mock)
|
74
75
|
final_object = mock
|
75
76
|
final_class = mock
|
76
77
|
@definition.stubs(:klass).returns(final_class)
|
@@ -81,6 +82,7 @@ class TestDefinition < Minitest::Test
|
|
81
82
|
end
|
82
83
|
|
83
84
|
def test_getting_object_without_arguments
|
85
|
+
@definition.stubs(:require_object).returns(mock)
|
84
86
|
final_object = mock
|
85
87
|
final_class = mock
|
86
88
|
@definition.stubs(:klass).returns(final_class)
|
@@ -90,6 +92,7 @@ class TestDefinition < Minitest::Test
|
|
90
92
|
end
|
91
93
|
|
92
94
|
def test_getting_object_with_method_calls
|
95
|
+
@definition.stubs(:require_object).returns(mock)
|
93
96
|
final_object = mock
|
94
97
|
final_class = mock
|
95
98
|
@definition.stubs(:klass).returns(final_class)
|
@@ -104,6 +107,7 @@ class TestDefinition < Minitest::Test
|
|
104
107
|
end
|
105
108
|
|
106
109
|
def test_getting_object_without_configurator
|
110
|
+
@definition.stubs(:require_object).returns(mock)
|
107
111
|
configurator_object = mock
|
108
112
|
final_object = mock
|
109
113
|
final_class = mock
|
@@ -117,6 +121,7 @@ class TestDefinition < Minitest::Test
|
|
117
121
|
end
|
118
122
|
|
119
123
|
def test_getting_object_with_configurator
|
124
|
+
@definition.stubs(:require_object).returns(mock)
|
120
125
|
configurator_object = mock
|
121
126
|
final_object = mock
|
122
127
|
final_class = mock
|
@@ -189,38 +194,58 @@ class TestDefinition < Minitest::Test
|
|
189
194
|
end
|
190
195
|
|
191
196
|
def test_resolving_references_with_defintion_and_referenced_object_in_container_scope
|
192
|
-
|
193
|
-
referenced_object
|
197
|
+
referenced_definition = mock
|
198
|
+
referenced_object = mock
|
199
|
+
referenced_definition.stubs(:object).returns(referenced_object)
|
200
|
+
referenced_definition.stubs(:scope).returns(:container)
|
194
201
|
@definition.scope= :container
|
195
|
-
@container.stubs(:
|
202
|
+
@container.stubs(:find).with('reference.name').returns(referenced_definition)
|
196
203
|
|
197
204
|
assert_equal(['first', referenced_object], @definition.send(:resolve_references, %w(first @reference.name)))
|
198
205
|
end
|
199
206
|
|
200
207
|
def test_resolving_references_with_defintion_and_referenced_object_in_prototype_scope
|
201
|
-
|
202
|
-
referenced_object
|
208
|
+
referenced_definition = mock
|
209
|
+
referenced_object = mock
|
210
|
+
referenced_definition.stubs(:object).returns(referenced_object)
|
211
|
+
referenced_definition.stubs(:scope).returns(:prototype)
|
203
212
|
@definition.scope= :prototype
|
204
|
-
@container.stubs(:
|
213
|
+
@container.stubs(:find).with('reference.name').returns(referenced_definition)
|
205
214
|
|
206
215
|
assert_equal(['first', referenced_object], @definition.send(:resolve_references, %w(first @reference.name)))
|
207
216
|
end
|
208
217
|
|
209
218
|
def test_resolving_references_with_defintion_in_prototype_scope_and_referenced_object_in_container_scope
|
210
|
-
|
211
|
-
referenced_object
|
219
|
+
referenced_definition = mock
|
220
|
+
referenced_object = mock
|
221
|
+
referenced_definition.stubs(:object).returns(referenced_object)
|
222
|
+
referenced_definition.stubs(:scope).returns(:container)
|
212
223
|
@definition.scope= :prototype
|
213
|
-
@container.stubs(:
|
224
|
+
@container.stubs(:find).with('reference.name').returns(referenced_definition)
|
214
225
|
|
215
226
|
assert_equal(['first', referenced_object], @definition.send(:resolve_references, %w(first @reference.name)))
|
216
227
|
end
|
217
228
|
|
218
229
|
def test_resolving_references_with_defintion_in_container_scope_and_referenced_object_in_prototype_scope
|
219
|
-
|
220
|
-
referenced_object
|
230
|
+
referenced_definition = mock
|
231
|
+
referenced_object = mock
|
232
|
+
referenced_definition.stubs(:object).returns(referenced_object)
|
233
|
+
referenced_definition.stubs(:scope).returns(:prototype)
|
221
234
|
@definition.scope= :container
|
222
|
-
@container.stubs(:
|
235
|
+
@container.stubs(:find).with('reference.name').returns(referenced_definition)
|
223
236
|
|
224
237
|
assert_raises(ScopeWideningInjectionError) { @definition.send(:resolve_references, %w(first @reference.name)) }
|
225
238
|
end
|
239
|
+
|
240
|
+
def test_require_object_with_file_path
|
241
|
+
@definition.expects(:require).with('/file/to/path').once
|
242
|
+
@definition.file_path = '/file/to/path'
|
243
|
+
@definition.send(:require_object)
|
244
|
+
end
|
245
|
+
|
246
|
+
def test_require_object_automatically
|
247
|
+
@definition.expects(:require).with('my_module/my_class').once
|
248
|
+
@definition.klass_name = 'MyModule::MyClass'
|
249
|
+
@definition.send(:require_object)
|
250
|
+
end
|
226
251
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dependency_injection
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kevin Disneur
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2014-02-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -88,7 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
88
88
|
version: '0'
|
89
89
|
requirements: []
|
90
90
|
rubyforge_project:
|
91
|
-
rubygems_version: 2.0.
|
91
|
+
rubygems_version: 2.0.14
|
92
92
|
signing_key:
|
93
93
|
specification_version: 4
|
94
94
|
summary: Dependency Injection system for Ruby
|