ioc 2.2.0 → 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ioc.gemspec +13 -18
- data/lib/ioc.rb +0 -9
- data/lib/ioc/container.rb +15 -14
- data/lib/ioc/service_block.rb +11 -0
- data/lib/ioc/service_class.rb +16 -0
- data/spec/ioc_spec.rb +61 -98
- data/spec/spec_helper.rb +0 -1
- metadata +7 -28
- data/.gitignore +0 -17
- data/Gemfile +0 -4
- data/Guardfile +0 -9
- data/lib/ioc/exceptions.rb +0 -6
- data/lib/ioc/resolver_factory.rb +0 -8
- data/lib/ioc/resolvers/abstract_resolver.rb +0 -12
- data/lib/ioc/resolvers/class_resolver.rb +0 -27
- data/lib/ioc/resolvers/instance_resolver.rb +0 -13
- data/lib/ioc/version.rb +0 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: daea0d62d1d33b2d291e60e8ded2d3e22ea64434
|
4
|
+
data.tar.gz: 8e58e246b0b6f1ca08f0aa1475e25423db9307f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 92df3344bbe775ce4ce55ec7a28c043a31151baa1310aa272d69120786b45e3f1c6b27b196a85609323fe6a61e7dcdb5e4a3a0c4135fdea93a95377744957153
|
7
|
+
data.tar.gz: 0dff485f90b20931e6715e2d08ac55509791813aa3c8041386b3b3303f641fe7b56fa9187aedae61b98e1bd5d230ddecba49fb86a47829d2f06ddb2f99658491
|
data/ioc.gemspec
CHANGED
@@ -1,24 +1,19 @@
|
|
1
1
|
# coding: utf-8
|
2
|
-
lib = File.expand_path('../lib', __FILE__)
|
3
|
-
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require 'ioc/version'
|
5
2
|
|
6
3
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
8
|
-
spec.version =
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
-
spec.
|
12
|
-
spec.
|
13
|
-
spec.
|
14
|
-
spec.
|
15
|
-
|
16
|
-
spec.files
|
17
|
-
spec.
|
18
|
-
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
4
|
+
spec.name = 'ioc'
|
5
|
+
spec.version = '3.0.0'
|
6
|
+
spec.authors = ['Erik Lott']
|
7
|
+
spec.email = ['erik.lott@kodio.io']
|
8
|
+
spec.summary = 'Minimal inversion of control container for Ruby'
|
9
|
+
spec.homepage = ''
|
10
|
+
spec.license = 'MIT'
|
11
|
+
spec.files = ['LICENSE.txt', 'README.md', 'Rakefile', 'ioc.gemspec']
|
12
|
+
spec.files += Dir.glob('lib/**/*.rb')
|
13
|
+
spec.files += Dir.glob('spec/**/*')
|
14
|
+
spec.test_files = Dir.glob('spec/**/*')
|
19
15
|
spec.require_paths = ["lib"]
|
20
16
|
|
21
|
-
spec.add_development_dependency "bundler", "~> 1.
|
17
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
22
18
|
spec.add_development_dependency "rake"
|
23
|
-
|
24
|
-
end
|
19
|
+
end
|
data/lib/ioc.rb
CHANGED
data/lib/ioc/container.rb
CHANGED
@@ -1,3 +1,6 @@
|
|
1
|
+
require 'ioc/service_class'
|
2
|
+
require 'ioc/service_block'
|
3
|
+
|
1
4
|
module IOC
|
2
5
|
class Container
|
3
6
|
def self.install(*installers)
|
@@ -12,23 +15,21 @@ module IOC
|
|
12
15
|
@cache = {}
|
13
16
|
end
|
14
17
|
|
15
|
-
def register(
|
16
|
-
raise(
|
17
|
-
@resolvers[
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
def register(service_name, klass=nil, &block)
|
19
|
+
raise(ArgumentError, "Service '#{service_name}' has already been registered") if @resolvers.key?(service_name)
|
20
|
+
@resolvers[service_name] =
|
21
|
+
if klass
|
22
|
+
ServiceClass.new(klass)
|
23
|
+
elsif block_given?
|
24
|
+
ServiceBlock.new(block)
|
25
|
+
else
|
26
|
+
raise(ArgumentError, 'expected service')
|
27
|
+
end
|
23
28
|
end
|
24
29
|
|
25
30
|
def resolve(name)
|
26
|
-
raise(
|
27
|
-
@cache[name] ||= @resolvers[name].resolve
|
28
|
-
end
|
29
|
-
|
30
|
-
def resolve_anonymous(klass)
|
31
|
-
ClassResolver.new(klass, self).resolve
|
31
|
+
raise(ArgumentError, "Service '#{name}' not registered") unless @resolvers.key?(name)
|
32
|
+
@cache[name] ||= @resolvers[name].resolve(self)
|
32
33
|
end
|
33
34
|
|
34
35
|
def [](key)
|
@@ -0,0 +1,16 @@
|
|
1
|
+
module IOC
|
2
|
+
class ServiceClass
|
3
|
+
def initialize(klass)
|
4
|
+
klass.instance_method(:initialize)
|
5
|
+
@klass = klass
|
6
|
+
rescue NoMethodError => e
|
7
|
+
raise(ArgumentError, 'Expected initialize method on registered service')
|
8
|
+
end
|
9
|
+
|
10
|
+
def resolve(container)
|
11
|
+
dependencies = @klass.instance_method(:initialize).parameters.map(&:last)
|
12
|
+
resolved_dependencies = dependencies.map{ |dependency| container.resolve(dependency) }
|
13
|
+
@klass.new(*resolved_dependencies)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/spec/ioc_spec.rb
CHANGED
@@ -2,60 +2,32 @@ require 'spec_helper'
|
|
2
2
|
require 'ioc'
|
3
3
|
|
4
4
|
describe IOC::Container do
|
5
|
-
context 'when
|
6
|
-
it '
|
7
|
-
container =
|
8
|
-
|
9
|
-
container.register(:
|
10
|
-
container.resolve(:
|
5
|
+
context 'when class is registered' do
|
6
|
+
it 'resolves a class instance' do
|
7
|
+
container = define_container
|
8
|
+
myclass = define_class
|
9
|
+
container.register(:myclass, myclass)
|
10
|
+
inst = container.resolve(:myclass)
|
11
|
+
inst.must_be_instance_of(myclass)
|
11
12
|
end
|
12
|
-
end
|
13
|
-
|
14
|
-
context 'when service is registered as a class' do
|
15
|
-
context 'when class has no dependencies' do
|
16
|
-
it 'resolves to instance' do
|
17
|
-
container = new_container
|
18
|
-
myclass = new_class
|
19
|
-
container.register(:myclass, myclass)
|
20
|
-
container.resolve(:myclass).must_be_instance_of(myclass)
|
21
|
-
end
|
22
13
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
14
|
+
it 'caches resolved instance' do
|
15
|
+
container = define_container
|
16
|
+
myclass = define_class
|
17
|
+
container.register(:myclass, myclass)
|
18
|
+
inst1 = container.resolve(:myclass)
|
19
|
+
inst2 = container.resolve(:myclass)
|
20
|
+
inst1.must_equal(inst2)
|
30
21
|
end
|
31
22
|
|
32
23
|
context 'when class has dependencies' do
|
33
|
-
it 'resolves to instance' do
|
34
|
-
container = new_container
|
35
|
-
engine_class = new_class
|
36
|
-
car_class = new_class_with_dependency
|
37
|
-
container.register(:engine, engine_class)
|
38
|
-
container.register(:car, car_class)
|
39
|
-
|
40
|
-
container.resolve(:car).must_be_instance_of(car_class)
|
41
|
-
end
|
42
|
-
|
43
|
-
it 'caches resolved instance' do
|
44
|
-
container = new_container
|
45
|
-
engine_class = new_class
|
46
|
-
car_class = new_class_with_dependency
|
47
|
-
container.register(:engine, engine_class)
|
48
|
-
container.register(:car, car_class)
|
49
|
-
|
50
|
-
car = container.resolve(:car)
|
51
|
-
container.resolve(:car).must_equal(car)
|
52
|
-
end
|
53
|
-
|
54
24
|
it 'injects dependencies into instance' do
|
55
|
-
container =
|
56
|
-
|
57
|
-
|
25
|
+
container = define_container
|
26
|
+
|
27
|
+
engine_class = define_class
|
58
28
|
container.register(:engine, engine_class)
|
29
|
+
|
30
|
+
car_class = define_class_with_engine_dependency
|
59
31
|
container.register(:car, car_class)
|
60
32
|
|
61
33
|
car = container.resolve(:car)
|
@@ -63,54 +35,60 @@ describe IOC::Container do
|
|
63
35
|
end
|
64
36
|
|
65
37
|
it 'caches dependencies' do
|
66
|
-
container =
|
67
|
-
|
68
|
-
|
38
|
+
container = define_container
|
39
|
+
|
40
|
+
engine_class = define_class
|
69
41
|
container.register(:engine, engine_class)
|
42
|
+
|
43
|
+
car_class = define_class_with_engine_dependency
|
70
44
|
container.register(:car, car_class)
|
71
45
|
|
72
46
|
car = container.resolve(:car)
|
73
|
-
engine =
|
74
|
-
|
47
|
+
engine = container.resolve(:engine)
|
48
|
+
|
49
|
+
car.engine.must_equal(engine)
|
75
50
|
end
|
76
51
|
end
|
77
52
|
end
|
78
53
|
|
79
|
-
context 'when
|
80
|
-
it '
|
81
|
-
|
82
|
-
|
54
|
+
context 'when block is registered' do
|
55
|
+
it 'returns result of block' do
|
56
|
+
myobject = 'a string object'
|
57
|
+
container = define_container
|
58
|
+
container.register(:myobject){|c| myobject }
|
59
|
+
container.resolve(:myobject).must_equal(myobject)
|
83
60
|
end
|
84
61
|
end
|
85
62
|
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
container.
|
90
|
-
|
91
|
-
end
|
63
|
+
it 'raises error when service not found' do
|
64
|
+
container = define_container
|
65
|
+
proc {
|
66
|
+
container.resolve(:some_service)
|
67
|
+
}.must_raise(ArgumentError)
|
92
68
|
end
|
93
69
|
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
70
|
+
it 'raises error when service registered more than once' do
|
71
|
+
myclass = define_class
|
72
|
+
container = define_container
|
73
|
+
container.register(:myclass, myclass)
|
74
|
+
proc {
|
75
|
+
container.register(:myclass, myclass)
|
76
|
+
}.must_raise(ArgumentError)
|
101
77
|
end
|
102
78
|
|
103
|
-
it '
|
104
|
-
|
105
|
-
|
106
|
-
container
|
107
|
-
|
79
|
+
it 'raises error when object registered as class' do
|
80
|
+
myclass = define_class
|
81
|
+
myobject = myclass.new
|
82
|
+
container = define_container
|
83
|
+
proc {
|
84
|
+
container.register(:myclass, myobject)
|
85
|
+
}.must_raise(ArgumentError)
|
108
86
|
end
|
109
87
|
|
110
88
|
it 'can register components using an installer' do
|
111
89
|
installer_class = Class.new do
|
112
|
-
def self.install(
|
113
|
-
|
90
|
+
def self.install(cont)
|
91
|
+
cont.register(:myobj){|c| 'I was resolved'}
|
114
92
|
end
|
115
93
|
end
|
116
94
|
|
@@ -119,33 +97,18 @@ describe IOC::Container do
|
|
119
97
|
end
|
120
98
|
|
121
99
|
it 'supports resolving via hash syntax' do
|
122
|
-
container =
|
123
|
-
myclass =
|
100
|
+
container = define_container
|
101
|
+
myclass = Class.new
|
124
102
|
container.register(:myclass, myclass)
|
125
|
-
container[:myclass]
|
126
|
-
|
127
|
-
|
128
|
-
it 'can resolve anonymous class' do
|
129
|
-
container = new_container
|
130
|
-
myobj = stub
|
131
|
-
anon_class = Class.new do
|
132
|
-
attr_reader :myobj
|
133
|
-
def initialize(myobj)
|
134
|
-
@myobj = myobj
|
135
|
-
end
|
136
|
-
end
|
137
|
-
|
138
|
-
container.register(:myobj, myobj, :as => :instance)
|
139
|
-
inst = container.resolve_anonymous(anon_class)
|
140
|
-
inst.must_be_instance_of(anon_class)
|
141
|
-
inst.myobj.must_equal(myobj)
|
103
|
+
inst = container[:myclass]
|
104
|
+
inst.must_be_instance_of(myclass)
|
142
105
|
end
|
143
106
|
|
144
|
-
def
|
107
|
+
def define_container(*args)
|
145
108
|
IOC::Container.new(*args)
|
146
109
|
end
|
147
110
|
|
148
|
-
def
|
111
|
+
def define_class_with_engine_dependency
|
149
112
|
Class.new do
|
150
113
|
attr_reader :engine
|
151
114
|
|
@@ -155,7 +118,7 @@ describe IOC::Container do
|
|
155
118
|
end
|
156
119
|
end
|
157
120
|
|
158
|
-
def
|
121
|
+
def define_class
|
159
122
|
Class.new
|
160
123
|
end
|
161
124
|
end
|
data/spec/spec_helper.rb
CHANGED
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:
|
4
|
+
version: 3.0.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-
|
11
|
+
date: 2014-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '1.
|
19
|
+
version: '1.5'
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '1.
|
26
|
+
version: '1.5'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -38,42 +38,21 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
|
42
|
-
name: mocha
|
43
|
-
requirement: !ruby/object:Gem::Requirement
|
44
|
-
requirements:
|
45
|
-
- - "~>"
|
46
|
-
- !ruby/object:Gem::Version
|
47
|
-
version: 0.14.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.14.0
|
55
|
-
description: Minimal inversion of control container for Ruby
|
41
|
+
description:
|
56
42
|
email:
|
57
43
|
- erik.lott@kodio.io
|
58
44
|
executables: []
|
59
45
|
extensions: []
|
60
46
|
extra_rdoc_files: []
|
61
47
|
files:
|
62
|
-
- ".gitignore"
|
63
|
-
- Gemfile
|
64
|
-
- Guardfile
|
65
48
|
- LICENSE.txt
|
66
49
|
- README.md
|
67
50
|
- Rakefile
|
68
51
|
- ioc.gemspec
|
69
52
|
- lib/ioc.rb
|
70
53
|
- lib/ioc/container.rb
|
71
|
-
- lib/ioc/
|
72
|
-
- lib/ioc/
|
73
|
-
- lib/ioc/resolvers/abstract_resolver.rb
|
74
|
-
- lib/ioc/resolvers/class_resolver.rb
|
75
|
-
- lib/ioc/resolvers/instance_resolver.rb
|
76
|
-
- lib/ioc/version.rb
|
54
|
+
- lib/ioc/service_block.rb
|
55
|
+
- lib/ioc/service_class.rb
|
77
56
|
- spec/ioc_spec.rb
|
78
57
|
- spec/spec_helper.rb
|
79
58
|
homepage: ''
|
data/.gitignore
DELETED
data/Gemfile
DELETED
data/Guardfile
DELETED
@@ -1,9 +0,0 @@
|
|
1
|
-
# A sample Guardfile
|
2
|
-
# More info at https://github.com/guard/guard#readme
|
3
|
-
|
4
|
-
guard :minitest, :all_on_start => false do
|
5
|
-
# with Minitest::Spec
|
6
|
-
watch(%r{^spec/(.*)_spec\.rb})
|
7
|
-
watch(%r{^lib/(.+)\.rb}) { |m| "spec/lib/#{m[1]}_spec.rb" }
|
8
|
-
watch(%r{^spec/spec_helper\.rb}) { 'spec' }
|
9
|
-
end
|
data/lib/ioc/exceptions.rb
DELETED
data/lib/ioc/resolver_factory.rb
DELETED
@@ -1,27 +0,0 @@
|
|
1
|
-
module IOC
|
2
|
-
class ClassResolver < AbstractResolver
|
3
|
-
def resolve
|
4
|
-
instance
|
5
|
-
end
|
6
|
-
|
7
|
-
private
|
8
|
-
|
9
|
-
def instance
|
10
|
-
klass.new(*resolved_dependencies)
|
11
|
-
end
|
12
|
-
|
13
|
-
def klass
|
14
|
-
@object
|
15
|
-
end
|
16
|
-
|
17
|
-
def dependencies
|
18
|
-
klass.instance_method(:initialize).parameters.map(&:last)
|
19
|
-
rescue NoMethodError => e
|
20
|
-
raise(ResolverError, "Service '#{klass}' missing initialize method ")
|
21
|
-
end
|
22
|
-
|
23
|
-
def resolved_dependencies
|
24
|
-
dependencies.map{ |dependency| @container.resolve(dependency) }
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
data/lib/ioc/version.rb
DELETED