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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c4f7ece1883442db7d03642ac2a0723e0e4208ec
4
- data.tar.gz: abf30ff7dbb9f75f608cbe6fe0b0d4df0a1f0b4a
3
+ metadata.gz: daea0d62d1d33b2d291e60e8ded2d3e22ea64434
4
+ data.tar.gz: 8e58e246b0b6f1ca08f0aa1475e25423db9307f5
5
5
  SHA512:
6
- metadata.gz: 0f6a0d831a5823f85df5eb71577be1cfa38c613cd4f0559c3bb9e8220a07392cbbaf0baf541108014f063ae15ec2cd5b2193621998f7a52e2cc0e2678bbcf422
7
- data.tar.gz: 1e492ebbb5e74d66660ba98d4746c01c5b9c4e5c76bd8768ea73f89b5c9970ac02f78e434343eaab559afe7650abb31056323c9502a0717585c749e78f64ff45
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 = "ioc"
8
- spec.version = IOC::VERSION
9
- spec.authors = ["Erik Lott"]
10
- spec.email = ["erik.lott@kodio.io"]
11
- spec.description = %q{Minimal inversion of control container for Ruby}
12
- spec.summary = %q{Minimal inversion of control container for Ruby}
13
- spec.homepage = ""
14
- spec.license = "MIT"
15
-
16
- spec.files = `git ls-files`.split($/)
17
- spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
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.3"
17
+ spec.add_development_dependency "bundler", "~> 1.5"
22
18
  spec.add_development_dependency "rake"
23
- spec.add_development_dependency "mocha", "~> 0.14.0"
24
- end
19
+ end
data/lib/ioc.rb CHANGED
@@ -1,10 +1 @@
1
- module IOC
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/resolver_factory'
10
1
  require 'ioc/container'
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(name, object, options={})
16
- raise(DuplicateRegistration, "Service '#{name}' has already been registered") if @resolvers.key?(name)
17
- @resolvers[name] = ResolverFactory.new_instance(object, self, options)
18
- end
19
-
20
- def register_instance(name, object, options={})
21
- options[:as] = :instance
22
- register(name, object, options)
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(UnknownService, "Service '#{name}' has not been registered") unless @resolvers.key?(name)
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,11 @@
1
+ module IOC
2
+ class ServiceBlock
3
+ def initialize(block)
4
+ @block = block
5
+ end
6
+
7
+ def resolve(container)
8
+ @block.call(container)
9
+ end
10
+ end
11
+ end
@@ -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 service is registered as an instance' do
6
- it 'can resolve instance' do
7
- container = new_container
8
- my_object = stub
9
- container.register(:myobj, my_object, :as => :instance)
10
- container.resolve(:myobj).must_equal(my_object)
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
- it 'caches resolved instance' do
24
- container = new_container
25
- myclass = new_class
26
- container.register(:myclass, myclass)
27
- inst = container.resolve(:myclass)
28
- container.resolve(:myclass).must_equal(inst)
29
- end
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 = new_container
56
- engine_class = new_class
57
- car_class = new_class_with_dependency
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 = new_container
67
- engine_class = new_class
68
- car_class = new_class_with_dependency
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 = car.engine
74
- container.resolve(:engine).must_equal(engine)
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 service not found' do
80
- it 'raises error' do
81
- container = new_container
82
- proc { container.resolve(:some_service) }.must_raise(IOC::UnknownService)
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
- context 'when service is registered twice' do
87
- it 'raises error' do
88
- container = new_container
89
- container.register(:car, stub)
90
- proc { container.register(:car, stub) }.must_raise(IOC::DuplicateRegistration)
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
- context 'when instance is registered as a class' do
95
- it 'raises error' do
96
- container = new_container
97
- object = Object.new
98
- container.register(:car, object)
99
- proc {container.resolve(:car)}.must_raise(IOC::ResolverError)
100
- end
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 'can register object as an instance' do
104
- object = stub
105
- container = IOC::Container.new
106
- container.register_instance(:myobj, object)
107
- container.resolve(:myobj).must_equal(object)
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(c)
113
- c.register(:myobj, 'I was resolved', :as => :instance)
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 = new_container
123
- myclass = new_class
100
+ container = define_container
101
+ myclass = Class.new
124
102
  container.register(:myclass, myclass)
125
- container[:myclass].must_be_instance_of(myclass)
126
- end
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 new_container(*args)
107
+ def define_container(*args)
145
108
  IOC::Container.new(*args)
146
109
  end
147
110
 
148
- def new_class_with_dependency
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 new_class
121
+ def define_class
159
122
  Class.new
160
123
  end
161
124
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,4 @@
1
1
  require 'minitest/autorun'
2
- require 'mocha/setup'
3
2
 
4
3
  def context *args, &block
5
4
  describe *args, &block
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: 2.2.0
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-04-06 00:00:00.000000000 Z
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.3'
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.3'
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
- - !ruby/object:Gem::Dependency
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/exceptions.rb
72
- - lib/ioc/resolver_factory.rb
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
@@ -1,17 +0,0 @@
1
- *.gem
2
- *.rbc
3
- .bundle
4
- .config
5
- .yardoc
6
- Gemfile.lock
7
- InstalledFiles
8
- _yardoc
9
- coverage
10
- doc/
11
- lib/bundler/man
12
- pkg
13
- rdoc
14
- spec/reports
15
- test/tmp
16
- test/version_tmp
17
- tmp
data/Gemfile DELETED
@@ -1,4 +0,0 @@
1
- source 'https://rubygems.org'
2
-
3
- # Specify your gem's dependencies in ioc.gemspec
4
- gemspec
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
@@ -1,6 +0,0 @@
1
- module IOC
2
- class IOCError < StandardError; end
3
- class UnknownService < IOCError; end
4
- class DuplicateRegistration < IOCError; end
5
- class ResolverError < IOCError; end
6
- end
@@ -1,8 +0,0 @@
1
- module IOC
2
- class ResolverFactory
3
- def self.new_instance(object, container, options={})
4
- type = options.fetch(:as, :class)
5
- IOC.const_get("#{type.capitalize}Resolver").new(object, container)
6
- end
7
- end
8
- end
@@ -1,12 +0,0 @@
1
- module IOC
2
- class AbstractResolver
3
- def initialize(object, container)
4
- @object = object
5
- @container = container
6
- end
7
-
8
- def resolve
9
- raise(NotImplementedError)
10
- end
11
- end
12
- end
@@ -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
@@ -1,13 +0,0 @@
1
- module IOC
2
- class InstanceResolver < AbstractResolver
3
- def resolve
4
- instance
5
- end
6
-
7
- private
8
-
9
- def instance
10
- @object
11
- end
12
- end
13
- end
data/lib/ioc/version.rb DELETED
@@ -1,3 +0,0 @@
1
- module IOC
2
- VERSION = "2.2.0"
3
- end