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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 868245cc4cc63541709598bf36176f20d4b72715
4
- data.tar.gz: 89ead53a3ffffbf2084d2de52a2c1c0536088b42
3
+ metadata.gz: ac7c1b4bc6cd15d943d365d4ede0d87fb600d879
4
+ data.tar.gz: 0ca2873cc691d49b5897308d0c2253661b001584
5
5
  SHA512:
6
- metadata.gz: 40d1e650d54707bab19aef06c71a0e184b461386b73d2e34bf40e588753e515803bb761d0962154b5fa88963f97e081ad154f16dd132dc6454c405f214482f2c
7
- data.tar.gz: 228594aa3e7d38d895e333a896eb3c2ec2941f540aeaac1346d72bd8132c918601064f30195af0171766244164d3a2ce600f712aa79ba259bcde5d664ae7ba47
6
+ metadata.gz: aa6c8d3754601bb4c634e014fae5f4d546f4e6c7de22f360b326a3048519a8e3bd140abc5b46d78bba15f77bc7bfed841939fa36550485067de295cd6ba7ce7a
7
+ data.tar.gz: 4c053957ca8bb28fea04d68f4c166e42a0dcbf234ebfa1d9d3b7da662cda68b3c259e526786d422cb8ebb109bca237bc2411f06cfe38369cb4620ff9364e084f
@@ -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
- raise(UnknownService, "Service '#{name}' has not been registered") unless @resolvers.key?(name)
16
- @resolvers.fetch(name).resolve(@cache)
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
@@ -1,3 +1,3 @@
1
1
  module IOC
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -165,8 +165,38 @@ describe IOC::Container do
165
165
  end
166
166
  end
167
167
 
168
- def new_container
169
- IOC::Container.new
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.1.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-27 00:00:00.000000000 Z
11
+ date: 2014-01-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler