ioc 0.0.1 → 0.1.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: 80e73c2b549c93b5b232498fa2496b2c993d6b07
4
- data.tar.gz: 10143c0341005c1070f9b78602a4e355dde0f406
3
+ metadata.gz: 868245cc4cc63541709598bf36176f20d4b72715
4
+ data.tar.gz: 89ead53a3ffffbf2084d2de52a2c1c0536088b42
5
5
  SHA512:
6
- metadata.gz: ac88db41acc966a6b83b592576fe37b546b4cd5dede75fcb6c5208f1a6951a86f90a26297ef4d489c170779e5564694ed6e28ea3382d1e84c24044406b25c187
7
- data.tar.gz: 168fa1d831595d5548d0aa38e01af9088951805adb23d30775de6423b80a991b7ea469fb7c08030ea009d2847d1e4c85b8d50e1fd01be6ce185e31a6c2899c92
6
+ metadata.gz: 40d1e650d54707bab19aef06c71a0e184b461386b73d2e34bf40e588753e515803bb761d0962154b5fa88963f97e081ad154f16dd132dc6454c405f214482f2c
7
+ data.tar.gz: 228594aa3e7d38d895e333a896eb3c2ec2941f540aeaac1346d72bd8132c918601064f30195af0171766244164d3a2ce600f712aa79ba259bcde5d664ae7ba47
data/lib/ioc.rb CHANGED
@@ -1,5 +1,11 @@
1
- require 'ioc/version'
2
- require 'ioc/container'
3
-
4
1
  module IOC
5
2
  end
3
+
4
+ require 'ioc/version'
5
+ require 'ioc/exceptions'
6
+ require 'ioc/resolvers/abstract_resolver'
7
+ require 'ioc/resolvers/instance_resolver'
8
+ require 'ioc/resolvers/class_resolver'
9
+ require 'ioc/resolvers/factory_resolver'
10
+ require 'ioc/resolver_factory'
11
+ require 'ioc/container'
data/lib/ioc/container.rb CHANGED
@@ -1,6 +1,3 @@
1
- require 'ioc/resolver_factory'
2
- require 'ioc/exceptions'
3
-
4
1
  module IOC
5
2
  class Container
6
3
  def initialize(resolver_factory=ResolverFactory)
@@ -16,7 +13,7 @@ module IOC
16
13
 
17
14
  def resolve(name)
18
15
  raise(UnknownService, "Service '#{name}' has not been registered") unless @resolvers.key?(name)
19
- @cache[name] ||= @resolvers.fetch(name).resolve
16
+ @resolvers.fetch(name).resolve(@cache)
20
17
  end
21
18
  end
22
19
  end
@@ -1,6 +1,3 @@
1
- require 'ioc/resolvers/class_resolver'
2
- require 'ioc/resolvers/instance_resolver'
3
-
4
1
  module IOC
5
2
  class ResolverFactory
6
3
  def self.new_instance(name, object, container, options={})
@@ -6,7 +6,7 @@ module IOC
6
6
  @container = container
7
7
  end
8
8
 
9
- def resolve
9
+ def resolve(cache)
10
10
  raise(NotImplementedError)
11
11
  end
12
12
  end
@@ -1,14 +1,15 @@
1
- require 'ioc/resolvers/abstract_resolver'
2
- require 'ioc/exceptions'
3
-
4
1
  module IOC
5
2
  class ClassResolver < AbstractResolver
6
- def resolve
7
- klass.new(*resolved_dependencies)
3
+ def resolve(cache)
4
+ cache[@name] ||= instance
8
5
  end
9
6
 
10
7
  private
11
8
 
9
+ def instance
10
+ klass.new(*resolved_dependencies)
11
+ end
12
+
12
13
  def klass
13
14
  @object
14
15
  end
@@ -0,0 +1,7 @@
1
+ module IOC
2
+ class FactoryResolver < ClassResolver
3
+ def resolve(cache)
4
+ instance
5
+ end
6
+ end
7
+ end
@@ -1,8 +1,12 @@
1
- require 'ioc/resolvers/abstract_resolver'
2
-
3
1
  module IOC
4
2
  class InstanceResolver < AbstractResolver
5
- def resolve
3
+ def resolve(cache)
4
+ cache[@name] ||= instance
5
+ end
6
+
7
+ private
8
+
9
+ def instance
6
10
  @object
7
11
  end
8
12
  end
data/lib/ioc/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module IOC
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
data/spec/ioc_spec.rb CHANGED
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
  require 'ioc'
3
3
 
4
4
  describe IOC::Container do
5
- context 'when service is registered as instance' do
5
+ context 'when service is registered as an instance' do
6
6
  it 'can resolve instance' do
7
7
  container = new_container
8
8
  my_object = stub
@@ -11,7 +11,7 @@ describe IOC::Container do
11
11
  end
12
12
  end
13
13
 
14
- context 'when service is registered as class' do
14
+ context 'when service is registered as a class' do
15
15
  context 'when class has no dependencies' do
16
16
  it 'resolves to instance' do
17
17
  container = new_container
@@ -76,6 +76,71 @@ describe IOC::Container do
76
76
  end
77
77
  end
78
78
 
79
+ context 'when service is registered as a factory' do
80
+ context 'when class has no dependencies' do
81
+ it 'resolves to instance' do
82
+ container = new_container
83
+ myclass = new_class
84
+ container.register(:myclass, myclass, :as => :factory)
85
+ container.resolve(:myclass).must_be_instance_of(myclass)
86
+ end
87
+
88
+ it 'does not cache resolved instance' do
89
+ container = new_container
90
+ myclass = new_class
91
+ container.register(:myclass, myclass, :as => :factory)
92
+ inst = container.resolve(:myclass)
93
+ container.resolve(:myclass).wont_equal(inst)
94
+ end
95
+ end
96
+
97
+ context 'when class has dependencies' do
98
+ it 'resolves to instance' do
99
+ container = new_container
100
+ engine_class = new_class
101
+ car_class = new_class_with_dependency
102
+ container.register(:engine, engine_class)
103
+ container.register(:car, car_class, :as => :factory)
104
+
105
+ container.resolve(:car).must_be_instance_of(car_class)
106
+ end
107
+
108
+ it 'does not cache resolved instance' do
109
+ container = new_container
110
+ engine_class = new_class
111
+ car_class = new_class_with_dependency
112
+ container.register(:engine, engine_class)
113
+ container.register(:car, car_class, :as => :factory)
114
+
115
+ car = container.resolve(:car)
116
+ container.resolve(:car).wont_equal(car)
117
+ end
118
+
119
+ it 'injects dependencies into instance' do
120
+ container = new_container
121
+ engine_class = new_class
122
+ car_class = new_class_with_dependency
123
+ container.register(:engine, engine_class)
124
+ container.register(:car, car_class, :as => :factory)
125
+
126
+ car = container.resolve(:car)
127
+ car.engine.must_be_instance_of(engine_class)
128
+ end
129
+
130
+ it 'caches dependencies' do
131
+ container = new_container
132
+ engine_class = new_class
133
+ car_class = new_class_with_dependency
134
+ container.register(:engine, engine_class)
135
+ container.register(:car, car_class, :as => :factory)
136
+
137
+ car = container.resolve(:car)
138
+ engine = car.engine
139
+ container.resolve(:engine).must_equal(engine)
140
+ end
141
+ end
142
+ end
143
+
79
144
  context 'when service not found' do
80
145
  it 'raises error' do
81
146
  container = new_container
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.0.1
4
+ version: 0.1.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-14 00:00:00.000000000 Z
11
+ date: 2014-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -72,6 +72,7 @@ files:
72
72
  - lib/ioc/resolver_factory.rb
73
73
  - lib/ioc/resolvers/abstract_resolver.rb
74
74
  - lib/ioc/resolvers/class_resolver.rb
75
+ - lib/ioc/resolvers/factory_resolver.rb
75
76
  - lib/ioc/resolvers/instance_resolver.rb
76
77
  - lib/ioc/version.rb
77
78
  - spec/ioc_spec.rb