ioc 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/ioc/container.rb +11 -3
- data/lib/ioc/version.rb +1 -1
- data/spec/ioc_spec.rb +32 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac7c1b4bc6cd15d943d365d4ede0d87fb600d879
|
4
|
+
data.tar.gz: 0ca2873cc691d49b5897308d0c2253661b001584
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: aa6c8d3754601bb4c634e014fae5f4d546f4e6c7de22f360b326a3048519a8e3bd140abc5b46d78bba15f77bc7bfed841939fa36550485067de295cd6ba7ce7a
|
7
|
+
data.tar.gz: 4c053957ca8bb28fea04d68f4c166e42a0dcbf234ebfa1d9d3b7da662cda68b3c259e526786d422cb8ebb109bca237bc2411f06cfe38369cb4620ff9364e084f
|
data/lib/ioc/container.rb
CHANGED
@@ -1,9 +1,10 @@
|
|
1
1
|
module IOC
|
2
2
|
class Container
|
3
|
-
def initialize(resolver_factory=ResolverFactory)
|
3
|
+
def initialize(parent=Container, resolver_factory=ResolverFactory)
|
4
4
|
@resolver_factory = resolver_factory
|
5
5
|
@resolvers = {}
|
6
6
|
@cache = {}
|
7
|
+
@parent = parent
|
7
8
|
end
|
8
9
|
|
9
10
|
def register(name, object, options={})
|
@@ -12,8 +13,15 @@ module IOC
|
|
12
13
|
end
|
13
14
|
|
14
15
|
def resolve(name)
|
15
|
-
|
16
|
-
|
16
|
+
resolver(name).resolve(@cache)
|
17
|
+
end
|
18
|
+
|
19
|
+
def resolver(name)
|
20
|
+
@resolvers[name] || @parent.resolver(name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def self.resolver(name)
|
24
|
+
raise(UnknownService, "Service '#{name}' has not been registered")
|
17
25
|
end
|
18
26
|
end
|
19
27
|
end
|
data/lib/ioc/version.rb
CHANGED
data/spec/ioc_spec.rb
CHANGED
@@ -165,8 +165,38 @@ describe IOC::Container do
|
|
165
165
|
end
|
166
166
|
end
|
167
167
|
|
168
|
-
|
169
|
-
|
168
|
+
context 'when parent ioc container is assigned' do
|
169
|
+
it 'can traverse parent container to resolve object' do
|
170
|
+
parent = new_container
|
171
|
+
child = new_container(parent)
|
172
|
+
|
173
|
+
my_object = stub
|
174
|
+
parent.register(:myobj, my_object, :as => :instance)
|
175
|
+
child.resolve(:myobj).must_equal(my_object)
|
176
|
+
end
|
177
|
+
|
178
|
+
it 'cannot travers sibling containers to resolve object' do
|
179
|
+
parent = new_container
|
180
|
+
son = new_container(parent)
|
181
|
+
daughter = new_container(parent)
|
182
|
+
|
183
|
+
my_object = stub
|
184
|
+
daughter.register(:myobj, my_object, :as => :instance)
|
185
|
+
proc { son.resolve(:myobj) }.must_raise(IOC::UnknownService)
|
186
|
+
end
|
187
|
+
|
188
|
+
it 'cannot be traversed by parent container' do
|
189
|
+
parent = new_container
|
190
|
+
child = new_container(parent)
|
191
|
+
|
192
|
+
my_object = stub
|
193
|
+
child.register(:myobj, my_object, :as => :instance)
|
194
|
+
proc { parent.resolve(:myobj) }.must_raise(IOC::UnknownService)
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
def new_container(*args)
|
199
|
+
IOC::Container.new(*args)
|
170
200
|
end
|
171
201
|
|
172
202
|
def new_class_with_dependency
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Erik Lott
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-01-
|
11
|
+
date: 2014-01-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|