ioc 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 80e73c2b549c93b5b232498fa2496b2c993d6b07
4
+ data.tar.gz: 10143c0341005c1070f9b78602a4e355dde0f406
5
+ SHA512:
6
+ metadata.gz: ac88db41acc966a6b83b592576fe37b546b4cd5dede75fcb6c5208f1a6951a86f90a26297ef4d489c170779e5564694ed6e28ea3382d1e84c24044406b25c187
7
+ data.tar.gz: 168fa1d831595d5548d0aa38e01af9088951805adb23d30775de6423b80a991b7ea469fb7c08030ea009d2847d1e4c85b8d50e1fd01be6ce185e31a6c2899c92
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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 ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ioc.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,9 @@
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/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Erik Lott
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,29 @@
1
+ # IOC
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ioc'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ioc
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/ioc.gemspec ADDED
@@ -0,0 +1,24 @@
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
+
6
+ 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)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "mocha", "~> 0.14.0"
24
+ end
data/lib/ioc.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'ioc/version'
2
+ require 'ioc/container'
3
+
4
+ module IOC
5
+ end
@@ -0,0 +1,22 @@
1
+ require 'ioc/resolver_factory'
2
+ require 'ioc/exceptions'
3
+
4
+ module IOC
5
+ class Container
6
+ def initialize(resolver_factory=ResolverFactory)
7
+ @resolver_factory = resolver_factory
8
+ @resolvers = {}
9
+ @cache = {}
10
+ end
11
+
12
+ def register(name, object, options={})
13
+ raise(DuplicateRegistration, "Service '#{name}' has already been registered") if @resolvers.key?(name)
14
+ @resolvers[name] = @resolver_factory.new_instance(name, object, self, options)
15
+ end
16
+
17
+ def resolve(name)
18
+ raise(UnknownService, "Service '#{name}' has not been registered") unless @resolvers.key?(name)
19
+ @cache[name] ||= @resolvers.fetch(name).resolve
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,6 @@
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
@@ -0,0 +1,11 @@
1
+ require 'ioc/resolvers/class_resolver'
2
+ require 'ioc/resolvers/instance_resolver'
3
+
4
+ module IOC
5
+ class ResolverFactory
6
+ def self.new_instance(name, object, container, options={})
7
+ type = options.fetch(:as, :class)
8
+ IOC.const_get("#{type.capitalize}Resolver").new(name, object, container)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,13 @@
1
+ module IOC
2
+ class AbstractResolver
3
+ def initialize(name, object, container)
4
+ @name = name
5
+ @object = object
6
+ @container = container
7
+ end
8
+
9
+ def resolve
10
+ raise(NotImplementedError)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ require 'ioc/resolvers/abstract_resolver'
2
+ require 'ioc/exceptions'
3
+
4
+ module IOC
5
+ class ClassResolver < AbstractResolver
6
+ def resolve
7
+ klass.new(*resolved_dependencies)
8
+ end
9
+
10
+ private
11
+
12
+ def klass
13
+ @object
14
+ end
15
+
16
+ def dependencies
17
+ klass.instance_method(:initialize).parameters.map(&:last)
18
+ rescue NoMethodError => e
19
+ raise(ResolverError, "Service '#{@name}' missing initialize method ")
20
+ end
21
+
22
+ def resolved_dependencies
23
+ dependencies.map{ |dependency| @container.resolve(dependency) }
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,9 @@
1
+ require 'ioc/resolvers/abstract_resolver'
2
+
3
+ module IOC
4
+ class InstanceResolver < AbstractResolver
5
+ def resolve
6
+ @object
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,3 @@
1
+ module IOC
2
+ VERSION = "0.0.1"
3
+ end
data/spec/ioc_spec.rb ADDED
@@ -0,0 +1,120 @@
1
+ require 'spec_helper'
2
+ require 'ioc'
3
+
4
+ describe IOC::Container do
5
+ context 'when service is registered as 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)
11
+ end
12
+ end
13
+
14
+ context 'when service is registered as 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
+
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
30
+ end
31
+
32
+ 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
+ it 'injects dependencies into instance' do
55
+ container = new_container
56
+ engine_class = new_class
57
+ car_class = new_class_with_dependency
58
+ container.register(:engine, engine_class)
59
+ container.register(:car, car_class)
60
+
61
+ car = container.resolve(:car)
62
+ car.engine.must_be_instance_of(engine_class)
63
+ end
64
+
65
+ it 'caches dependencies' do
66
+ container = new_container
67
+ engine_class = new_class
68
+ car_class = new_class_with_dependency
69
+ container.register(:engine, engine_class)
70
+ container.register(:car, car_class)
71
+
72
+ car = container.resolve(:car)
73
+ engine = car.engine
74
+ container.resolve(:engine).must_equal(engine)
75
+ end
76
+ end
77
+ end
78
+
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)
83
+ end
84
+ end
85
+
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
92
+ end
93
+
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
101
+ end
102
+
103
+ def new_container
104
+ IOC::Container.new
105
+ end
106
+
107
+ def new_class_with_dependency
108
+ Class.new do
109
+ attr_reader :engine
110
+
111
+ def initialize(engine)
112
+ @engine = engine
113
+ end
114
+ end
115
+ end
116
+
117
+ def new_class
118
+ Class.new
119
+ end
120
+ end
@@ -0,0 +1,6 @@
1
+ require 'minitest/autorun'
2
+ require 'mocha/setup'
3
+
4
+ def context *args, &block
5
+ describe *args, &block
6
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ioc
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Erik Lott
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-01-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
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
56
+ email:
57
+ - erik.lott@kodio.io
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - Guardfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - ioc.gemspec
69
+ - lib/ioc.rb
70
+ - 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
77
+ - spec/ioc_spec.rb
78
+ - spec/spec_helper.rb
79
+ homepage: ''
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.0.7
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: Minimal inversion of control container for Ruby
103
+ test_files:
104
+ - spec/ioc_spec.rb
105
+ - spec/spec_helper.rb