dependency_injection 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/.ruby-gemset +1 -0
- data/.ruby-version +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +44 -0
- data/README.md +6 -0
- data/Rakefile +6 -0
- data/dependency_injection.gemspec +18 -0
- data/examples/basic_usage.rb +63 -0
- data/examples/global_container_parameter.rb +35 -0
- data/examples/inject_a_reference_to_another_dependency.rb +48 -0
- data/examples/services.yml +11 -0
- data/examples/yaml_configuration_file.rb +29 -0
- data/lib/dependency_injection/container.rb +26 -0
- data/lib/dependency_injection/definition.rb +69 -0
- data/lib/dependency_injection/loaders/yaml.rb +35 -0
- data/lib/dependency_injection/version.rb +3 -0
- data/test/dependency_injection/loaders/test_yaml.rb +88 -0
- data/test/dependency_injection/test_container.rb +54 -0
- data/test/dependency_injection/test_definition.rb +134 -0
- data/test/test_helper.rb +9 -0
- metadata +83 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: ab031c6a6cfce9dd524b8f57851aa6b0ac19ed47
|
4
|
+
data.tar.gz: e3e3f3bf5b38432373e2352bca3e3790c342ce0c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8ab8a953e957e6578c159a09a648a2914447e35ed10153d67b8a7774bfd7479817b60bab6a2db733557e9dfec41daa3677f1090c9151ed48f09e6f59febb4ffb
|
7
|
+
data.tar.gz: 46c38816ad28d38c93ad1de72cc770e5113df3cc2900f71754dfeacf1b244093cf077d8ff6f4385c0fc78190a2eeaf6c4601ed98b2da2e22f3d33918460d4517
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
coverage
|
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
dependency_injection
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.0.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
dependency_injection (0.1.0)
|
5
|
+
activesupport
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
activesupport (3.2.14)
|
11
|
+
i18n (~> 0.6, >= 0.6.4)
|
12
|
+
multi_json (~> 1.0)
|
13
|
+
colorize (0.5.8)
|
14
|
+
coveralls (0.6.7)
|
15
|
+
colorize
|
16
|
+
multi_json (~> 1.3)
|
17
|
+
rest-client
|
18
|
+
simplecov (>= 0.7)
|
19
|
+
thor
|
20
|
+
i18n (0.6.4)
|
21
|
+
metaclass (0.0.1)
|
22
|
+
mime-types (1.23)
|
23
|
+
minitest (5.0.6)
|
24
|
+
mocha (0.14.0)
|
25
|
+
metaclass (~> 0.0.1)
|
26
|
+
multi_json (1.7.8)
|
27
|
+
rake (10.1.0)
|
28
|
+
rest-client (1.6.7)
|
29
|
+
mime-types (>= 1.16)
|
30
|
+
simplecov (0.7.1)
|
31
|
+
multi_json (~> 1.0)
|
32
|
+
simplecov-html (~> 0.7.1)
|
33
|
+
simplecov-html (0.7.1)
|
34
|
+
thor (0.18.1)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
coveralls
|
41
|
+
dependency_injection!
|
42
|
+
minitest
|
43
|
+
mocha
|
44
|
+
rake
|
data/README.md
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
# Dependency Injection for Ruby
|
2
|
+
[![Build Status](https://travis-ci.org/kdisneur/dependency_injection-ruby.png?branch=master)](https://travis-ci.org/kdisneur/dependency_injection-ruby) [![Coverage Status](https://coveralls.io/repos/kdisneur/dependency_injection-ruby/badge.png?branch=master)](https://coveralls.io/r/kdisneur/dependency_injection-ruby?branch=master) [![Code Climate](https://codeclimate.com/github/kdisneur/dependency_injection-ruby.png)](https://codeclimate.com/github/kdisneur/dependency_injection-ruby)
|
3
|
+
|
4
|
+
It's a WIP for now.
|
5
|
+
|
6
|
+
You can find some examples in the [examples folder](https://github.com/kdisneur/dependency_injection-ruby/tree/master/examples)
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.expand_path('../lib', __FILE__))
|
2
|
+
require 'dependency_injection/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |spec|
|
5
|
+
spec.name = 'dependency_injection'
|
6
|
+
spec.version = DependencyInjection::VERSION
|
7
|
+
spec.summary = 'Dependency Injection system for Ruby'
|
8
|
+
spec.description = 'A fully customizable Dependency injection system for Ruby'
|
9
|
+
spec.homepage = 'https://github.com/kdisneur/dependency_injection'
|
10
|
+
spec.license = 'MIT'
|
11
|
+
spec.authors = ['Kevin Disneur']
|
12
|
+
spec.email = 'kevin@koboyz.org'
|
13
|
+
spec.files = `git ls-files`.split($/)
|
14
|
+
spec.require_paths = %w(lib)
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
|
17
|
+
spec.add_dependency('activesupport')
|
18
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'dependency_injection/container'
|
2
|
+
|
3
|
+
c = DependencyInjection::Container.new
|
4
|
+
|
5
|
+
# 1 - No specific class initialization
|
6
|
+
class Mailer
|
7
|
+
def send_mail(message)
|
8
|
+
puts "mail sent: #{message}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
c.register('mailer', 'Mailer')
|
13
|
+
c.get('mailer').send_mail('Hello World')
|
14
|
+
# => mail sent: Hello World
|
15
|
+
|
16
|
+
Object.instance_eval { remove_const(:Mailer) }
|
17
|
+
|
18
|
+
# 2 - constructor specific class initialization
|
19
|
+
class Mailer
|
20
|
+
def initialize(transport)
|
21
|
+
@transport = transport
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_mail(message)
|
25
|
+
puts "mail sent via #{@transport}: #{message}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
c.register('mailer', 'Mailer').add_argument('send_mail')
|
30
|
+
c.get('mailer').send_mail('Hello World')
|
31
|
+
# => mail sent via send_mail: Hello World
|
32
|
+
|
33
|
+
Object.instance_eval { remove_const(:Mailer) }
|
34
|
+
|
35
|
+
# 3 - method specific class initialization
|
36
|
+
class Mailer
|
37
|
+
attr_accessor :transport
|
38
|
+
|
39
|
+
def send_mail(message)
|
40
|
+
puts "mail sent via #{self.transport}: #{message}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
c.register('mailer', 'Mailer').add_method_call('transport=', 'send_mail')
|
45
|
+
c.get('mailer').send_mail('Hello World')
|
46
|
+
# => mail sent via send_mail: Hello World
|
47
|
+
|
48
|
+
# 4 - Mix of constructor and method specific class initialization
|
49
|
+
class Mailer
|
50
|
+
attr_accessor :transport
|
51
|
+
|
52
|
+
def initialize(from)
|
53
|
+
@from = from
|
54
|
+
end
|
55
|
+
|
56
|
+
def send_mail(message)
|
57
|
+
puts "mail sent by #{@from} via #{self.transport}: #{message}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
c.register('mailer', 'Mailer').add_argument('jon@doe.com').add_method_call('transport=', 'send_mail')
|
62
|
+
c.get('mailer').send_mail('Hello World')
|
63
|
+
# => mail sent by jon@doe.com via send_mail: Hello World
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'dependency_injection/container'
|
2
|
+
|
3
|
+
c = DependencyInjection::Container.new
|
4
|
+
c.add_parameter('mailer.transport', 'sendmail')
|
5
|
+
|
6
|
+
class Mailer
|
7
|
+
attr_accessor :transport
|
8
|
+
|
9
|
+
def send_mail(message)
|
10
|
+
puts "mail sent via #{transport}: #{message}"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
# 1 - Simple global parameter
|
15
|
+
c.register('mailer', 'Mailer').add_method_call('transport=', '%mailer.transport%')
|
16
|
+
c.get('mailer').send_mail('Hello World')
|
17
|
+
# => mail sent via send_mail: Hello World
|
18
|
+
|
19
|
+
# 2 - Complex global parameter
|
20
|
+
class NewsletterManager
|
21
|
+
def initialize(mailer)
|
22
|
+
@mailer = mailer
|
23
|
+
end
|
24
|
+
|
25
|
+
def send_mail(message)
|
26
|
+
puts 'newsletter'
|
27
|
+
@mailer.send_mail(message)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
c.add_parameter('my.mailer', '@mailer')
|
32
|
+
c.register('newsletter', 'NewsletterManager').add_argument('%my.mailer%')
|
33
|
+
c.get('newsletter').send_mail('Hello World')
|
34
|
+
# => newsletter
|
35
|
+
# mail sent via send_mail: Hello World
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'dependency_injection/container'
|
2
|
+
|
3
|
+
c = DependencyInjection::Container.new
|
4
|
+
|
5
|
+
# 1 - constructor specific class initialization
|
6
|
+
class NewsletterManager
|
7
|
+
def initialize(mailer)
|
8
|
+
@mailer = mailer
|
9
|
+
end
|
10
|
+
|
11
|
+
def send_mail(message)
|
12
|
+
@mailer.send_mail(message)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Mailer
|
17
|
+
def send_mail(message)
|
18
|
+
puts "mail sent: #{message}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
c.register('my.mailer', 'Mailer')
|
23
|
+
c.register('newsletter', 'NewsletterManager').add_argument('@my.mailer')
|
24
|
+
|
25
|
+
c.get('newsletter').send_mail('Hello World')
|
26
|
+
# => mail sent: Hello World
|
27
|
+
|
28
|
+
Object.instance_eval { remove_const(:NewsletterManager) }
|
29
|
+
|
30
|
+
# 2 - method specific class initialization
|
31
|
+
class NewsletterManager
|
32
|
+
attr_accessor :mailer, :from
|
33
|
+
|
34
|
+
def send_mail(message)
|
35
|
+
puts "mail sent by #{@from}"
|
36
|
+
@mailer.send_mail(message)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
c.register('my.mailer', 'Mailer')
|
41
|
+
c.register('newsletter', 'NewsletterManager')
|
42
|
+
.add_method_call('mailer=', '@my.mailer')
|
43
|
+
.add_method_call('from=', 'john@doe.com')
|
44
|
+
|
45
|
+
c.get('newsletter').send_mail('Hello World')
|
46
|
+
# => mail sent: Hello World
|
47
|
+
|
48
|
+
Object.instance_eval { remove_const(:Mailer) }
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'dependency_injection/container'
|
2
|
+
require 'dependency_injection/loaders/yaml'
|
3
|
+
|
4
|
+
c = DependencyInjection::Container.new
|
5
|
+
loader = DependencyInjection::Loaders::Yaml.new(c)
|
6
|
+
loader.load(File.join(File.dirname(File.expand_path(__FILE__)), 'services.yml'))
|
7
|
+
|
8
|
+
class NewsletterManager
|
9
|
+
def initialize(mailer)
|
10
|
+
@mailer = mailer
|
11
|
+
end
|
12
|
+
|
13
|
+
def send_mail(message)
|
14
|
+
puts 'newletter'
|
15
|
+
@mailer.send_mail(message)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class Mailer
|
20
|
+
attr_accessor :transport
|
21
|
+
|
22
|
+
def send_mail(message)
|
23
|
+
puts "mail sent via #{self.transport}: #{message}"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
c.get('newsletter').send_mail('Hello World')
|
28
|
+
# => newletter
|
29
|
+
# mail sent via sendmail: Hello World
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'dependency_injection/definition'
|
2
|
+
|
3
|
+
module DependencyInjection
|
4
|
+
class Container
|
5
|
+
attr_reader :definitions, :parameters
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@definitions = {}
|
9
|
+
@parameters = {}
|
10
|
+
end
|
11
|
+
|
12
|
+
def add_parameter(name, value)
|
13
|
+
@parameters[name] = value
|
14
|
+
end
|
15
|
+
|
16
|
+
def get(name)
|
17
|
+
if (definition = @definitions[name])
|
18
|
+
definition.object
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def register(name, klass_name)
|
23
|
+
@definitions[name] = Definition.new(klass_name, self)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
|
3
|
+
module DependencyInjection
|
4
|
+
class Definition
|
5
|
+
attr_accessor :klass_name, :arguments, :method_calls
|
6
|
+
|
7
|
+
def initialize(klass_name, container)
|
8
|
+
@container = container
|
9
|
+
self.arguments = []
|
10
|
+
self.klass_name = klass_name
|
11
|
+
self.method_calls = {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def add_argument(argument)
|
15
|
+
self.add_arguments(argument)
|
16
|
+
end
|
17
|
+
|
18
|
+
def add_arguments(*arguments)
|
19
|
+
self.arguments += arguments
|
20
|
+
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def add_method_call(method_name, *arguments)
|
25
|
+
self.method_calls[method_name] = arguments
|
26
|
+
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def klass
|
31
|
+
self.klass_name.constantize
|
32
|
+
end
|
33
|
+
|
34
|
+
def object
|
35
|
+
return @object if @object
|
36
|
+
|
37
|
+
@object = self.klass.new(*resolve(self.arguments))
|
38
|
+
self.method_calls.each { |method_name, arguments| @object.send(method_name, *resolve(arguments)) }
|
39
|
+
|
40
|
+
@object
|
41
|
+
end
|
42
|
+
|
43
|
+
private
|
44
|
+
|
45
|
+
def resolve(arguments)
|
46
|
+
resolve_references(resolve_container_parameters(arguments))
|
47
|
+
end
|
48
|
+
|
49
|
+
def resolve_container_parameters(arguments)
|
50
|
+
arguments.map do |argument|
|
51
|
+
if /^%(?<parameter_name>.*)%$/ =~ argument
|
52
|
+
@container.parameters[parameter_name]
|
53
|
+
else
|
54
|
+
argument
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
def resolve_references(arguments)
|
60
|
+
arguments.map do |argument|
|
61
|
+
if /^@(?<reference_name>.*)/ =~ argument
|
62
|
+
@container.get(reference_name)
|
63
|
+
else
|
64
|
+
argument
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
|
3
|
+
module DependencyInjection
|
4
|
+
module Loaders
|
5
|
+
class Yaml
|
6
|
+
def initialize(container)
|
7
|
+
@container = container
|
8
|
+
end
|
9
|
+
|
10
|
+
def load(filename)
|
11
|
+
file = YAML::load_file(filename)
|
12
|
+
add_parameters(file['parameters']) if file['parameters']
|
13
|
+
add_services(file['services']) if file['services']
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def add_parameters(parameters)
|
19
|
+
parameters.each { |name, value| @container.add_parameter(name, value) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def add_services(services)
|
23
|
+
services.each { |name, parameters| add_service(name, parameters) }
|
24
|
+
end
|
25
|
+
|
26
|
+
def add_service(name, parameters)
|
27
|
+
definition = @container.register(name, parameters['class'])
|
28
|
+
definition.add_arguments(*parameters['arguments']) if parameters['arguments']
|
29
|
+
if parameters['calls']
|
30
|
+
parameters['calls'].each { |method_name, arguments| definition.add_method_call(method_name, *arguments) }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'dependency_injection/loaders/yaml'
|
3
|
+
|
4
|
+
class TestYaml < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@container = mock
|
7
|
+
@yaml_loader = DependencyInjection::Loaders::Yaml.new(@container)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_loading_file_without_parameters
|
11
|
+
YAML.stubs(:load_file).with('services.yml').returns({ 'services' => [] })
|
12
|
+
@yaml_loader.expects(:add_parameters).never
|
13
|
+
|
14
|
+
@yaml_loader.load('services.yml')
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_loading_file_with_parameters
|
18
|
+
YAML.stubs(:load_file).with('services.yml').returns({'parameters' => { 'key_1' => 'value_1' }, 'services' => [] })
|
19
|
+
@yaml_loader.expects(:add_parameters).with({ 'key_1' => 'value_1' })
|
20
|
+
|
21
|
+
@yaml_loader.load('services.yml')
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_loading_file_without_services
|
25
|
+
YAML.stubs(:load_file).with('services.yml').returns({ 'parameters' => [] })
|
26
|
+
@yaml_loader.expects(:add_services).never
|
27
|
+
|
28
|
+
@yaml_loader.load('services.yml')
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_loading_file_with_services
|
32
|
+
YAML.stubs(:load_file).with('services.yml').returns({ 'parameters' => {}, 'services' => { 'service_1' => { 'class' => 'MyKlass' }}})
|
33
|
+
@yaml_loader.expects(:add_services).with({ 'service_1' => { 'class' => 'MyKlass' }})
|
34
|
+
|
35
|
+
@yaml_loader.load('services.yml')
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_adding_parameters
|
39
|
+
@container.expects(:add_parameter).with('key_1', 'value_1')
|
40
|
+
@container.expects(:add_parameter).with('key_2', 'value_2')
|
41
|
+
|
42
|
+
@yaml_loader.send(:add_parameters, { 'key_1' => 'value_1', 'key_2' => 'value_2' })
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_adding_services
|
46
|
+
@yaml_loader.expects(:add_service).with('key_1', { 'param_1' => 'value_1', 'param_2' => 'value_2' })
|
47
|
+
@yaml_loader.expects(:add_service).with('key_2', { 'param_1' => 'value_1' })
|
48
|
+
|
49
|
+
@yaml_loader.send(:add_services, { 'key_1' => { 'param_1' => 'value_1', 'param_2' => 'value_2' },
|
50
|
+
'key_2' => { 'param_1' => 'value_1' }})
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_adding_service_without_parameters
|
54
|
+
definition = mock
|
55
|
+
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition)
|
56
|
+
definition.expects(:add_arguments).never
|
57
|
+
|
58
|
+
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass' })
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_adding_service_with_parameters
|
62
|
+
definition = mock
|
63
|
+
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition)
|
64
|
+
definition.expects(:add_arguments).with('arg_1', 'arg_2')
|
65
|
+
|
66
|
+
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass', 'arguments' => ['arg_1', 'arg_2'] })
|
67
|
+
end
|
68
|
+
|
69
|
+
def test_adding_service_without_method_calls
|
70
|
+
definition = mock
|
71
|
+
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition)
|
72
|
+
definition.expects(:add_method_call).never
|
73
|
+
|
74
|
+
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass' })
|
75
|
+
end
|
76
|
+
|
77
|
+
def test_adding_service_with_method_calls
|
78
|
+
definition = mock
|
79
|
+
@container.stubs(:register).with('key_1', 'MyKlass').returns(definition)
|
80
|
+
definition.expects(:add_method_call).with('method_1', 'arg_1')
|
81
|
+
definition.expects(:add_method_call).with('method_2', 'arg_1', 'arg_2')
|
82
|
+
definition.expects(:add_method_call).with('method_3', %w(arg_1 arg_2))
|
83
|
+
|
84
|
+
@yaml_loader.send(:add_service, 'key_1', { 'class' => 'MyKlass', 'calls' => { 'method_1' => ['arg_1'],
|
85
|
+
'method_2' => ['arg_1', 'arg_2'],
|
86
|
+
'method_3' => [['arg_1', 'arg_2']] }})
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'dependency_injection/container'
|
3
|
+
|
4
|
+
class TestContainer < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@container = DependencyInjection::Container.new
|
7
|
+
@final_object = mock
|
8
|
+
@definition = mock
|
9
|
+
@definition.stubs(:object).returns(@final_object)
|
10
|
+
@another_definition = mock
|
11
|
+
DependencyInjection::Definition.stubs(:new).with('MyDefinition', @container).returns(@definition)
|
12
|
+
DependencyInjection::Definition.stubs(:new).with('MyOtherDefinition', @container).returns(@another_definition)
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_adding_new_parameter
|
16
|
+
@container.add_parameter('my.parameter', 'value')
|
17
|
+
assert_equal({ 'my.parameter' => 'value' }, @container.parameters)
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_adding_an_already_existing_parameter
|
21
|
+
@container.add_parameter('my.parameter', 'value')
|
22
|
+
assert_equal({ 'my.parameter' => 'value' }, @container.parameters)
|
23
|
+
|
24
|
+
@container.add_parameter('my.parameter', 'other value')
|
25
|
+
assert_equal({ 'my.parameter' => 'other value' }, @container.parameters)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_getting_a_registered_definition_returns_an_object
|
29
|
+
@container.register('my_definition', 'MyDefinition')
|
30
|
+
assert_equal(@final_object, @container.get('my_definition'))
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_getting_a_not_registered_definition_returns_nil
|
34
|
+
assert_equal(nil, @container.get('my_definition'))
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_registering_a_definition
|
38
|
+
@container.register('my_definition', 'MyDefinition')
|
39
|
+
|
40
|
+
assert_equal({ 'my_definition' => @definition }, @container.definitions)
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_registering_a_class_return_a_definition_object
|
44
|
+
assert_equal(@definition, @container.register('my_definition', 'MyDefinition'))
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_registering_an_already_existing_definition_replace_it
|
48
|
+
@container.register('my_definition', 'MyDefinition')
|
49
|
+
assert_equal({ 'my_definition' => @definition }, @container.definitions)
|
50
|
+
|
51
|
+
@container.register('my_definition', 'MyOtherDefinition')
|
52
|
+
assert_equal({ 'my_definition' => @another_definition }, @container.definitions)
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,134 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
require 'dependency_injection/definition'
|
3
|
+
|
4
|
+
class TestDefinition < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@container = mock
|
7
|
+
@definition = DependencyInjection::Definition.new('MyClass', @container)
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_adding_an_argument
|
11
|
+
@definition.add_argument('new argument')
|
12
|
+
|
13
|
+
assert_equal(['new argument'], @definition.arguments)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_adding_an_argument_returns_definition_object
|
17
|
+
assert_equal(@definition, @definition.add_argument('new argument'))
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_adding_several_arguments
|
21
|
+
@definition.add_arguments('first', 'second')
|
22
|
+
|
23
|
+
assert_equal(%w(first second), @definition.arguments)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_adding_several_arguments_returns_definition_object
|
27
|
+
assert_equal(@definition, @definition.add_arguments('first', 'second'))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_adding_additional_arguments
|
31
|
+
@definition.add_arguments('first')
|
32
|
+
assert_equal(%w(first), @definition.arguments)
|
33
|
+
|
34
|
+
@definition.add_arguments('second', 'third')
|
35
|
+
assert_equal(%w(first second third), @definition.arguments)
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_adding_a_method_call_without_parameters
|
39
|
+
@definition.add_method_call('my_method')
|
40
|
+
|
41
|
+
assert_equal({ 'my_method' => [] }, @definition.method_calls)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_adding_a_method_call_with_parameters
|
45
|
+
@definition.add_method_call('my_method=', 'value')
|
46
|
+
|
47
|
+
assert_equal({ 'my_method=' => %w(value) }, @definition.method_calls)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_adding_a_method_call_returns_definition_object
|
51
|
+
assert_equal(@definition, @definition.add_method_call('my_method'))
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_getting_klass
|
55
|
+
klass = mock
|
56
|
+
klass_name = 'MyClass'
|
57
|
+
@definition.stubs(:klass_name).returns(klass_name)
|
58
|
+
klass_name.stubs(:constantize).returns(klass)
|
59
|
+
|
60
|
+
assert_equal(klass, @definition.klass)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_getting_object_with_arguments
|
64
|
+
final_object = mock
|
65
|
+
final_class = mock
|
66
|
+
@definition.stubs(:klass).returns(final_class)
|
67
|
+
@definition.add_arguments('first', 'second')
|
68
|
+
final_class.stubs(:new).with('first', 'second').returns(final_object)
|
69
|
+
|
70
|
+
assert_equal(final_object, @definition.object)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_getting_object_without_arguments
|
74
|
+
final_object = mock
|
75
|
+
final_class = mock
|
76
|
+
@definition.stubs(:klass).returns(final_class)
|
77
|
+
final_class.stubs(:new).with.returns(final_object)
|
78
|
+
|
79
|
+
assert_equal(final_object, @definition.object)
|
80
|
+
end
|
81
|
+
|
82
|
+
def test_getting_object_with_method_calls
|
83
|
+
final_object = mock
|
84
|
+
final_class = mock
|
85
|
+
@definition.stubs(:klass).returns(final_class)
|
86
|
+
final_class.stubs(:new).with.returns(final_object)
|
87
|
+
final_object.expects(:method_1).with
|
88
|
+
final_object.expects(:method_2).with('value')
|
89
|
+
|
90
|
+
@definition.add_method_call('method_1')
|
91
|
+
@definition.add_method_call('method_2', 'value')
|
92
|
+
|
93
|
+
@definition.object
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_resolving_first_container_parameters
|
97
|
+
changed_arguments = mock
|
98
|
+
arguments = mock
|
99
|
+
@definition.stubs(:resolve_references).returns(changed_arguments)
|
100
|
+
@definition.expects(:resolve_container_parameters).with(arguments)
|
101
|
+
|
102
|
+
@definition.send(:resolve, arguments)
|
103
|
+
end
|
104
|
+
|
105
|
+
def test_resolving_references_after_container_parameters
|
106
|
+
changed_arguments = mock
|
107
|
+
arguments = mock
|
108
|
+
@definition.stubs(:resolve_container_parameters).with(arguments).returns(changed_arguments)
|
109
|
+
@definition.expects(:resolve_references).with(changed_arguments)
|
110
|
+
|
111
|
+
@definition.send(:resolve, arguments)
|
112
|
+
end
|
113
|
+
|
114
|
+
def test_resolving_container_parameters_without_parameters
|
115
|
+
assert_equal(%w(first, second), @definition.send(:resolve_container_parameters, %w(first, second)))
|
116
|
+
end
|
117
|
+
|
118
|
+
def test_resolving_container_parameters_with_parameters
|
119
|
+
@container.stubs(:parameters).returns({ 'parameter' => 'value' })
|
120
|
+
|
121
|
+
assert_equal(['first', 'value'], @definition.send(:resolve_container_parameters, %w(first %parameter%)))
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_resolving_references_without_references
|
125
|
+
assert_equal(%w(first second), @definition.send(:resolve_references, %w(first second)))
|
126
|
+
end
|
127
|
+
|
128
|
+
def test_resolving_references_with_references
|
129
|
+
referenced_object = mock
|
130
|
+
@container.stubs(:get).with('reference.name').returns(referenced_object)
|
131
|
+
|
132
|
+
assert_equal(['first', referenced_object], @definition.send(:resolve_references, %w(first @reference.name)))
|
133
|
+
end
|
134
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dependency_injection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kevin Disneur
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-08-05 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description: A fully customizable Dependency injection system for Ruby
|
28
|
+
email: kevin@koboyz.org
|
29
|
+
executables: []
|
30
|
+
extensions: []
|
31
|
+
extra_rdoc_files: []
|
32
|
+
files:
|
33
|
+
- .gitignore
|
34
|
+
- .ruby-gemset
|
35
|
+
- .ruby-version
|
36
|
+
- .travis.yml
|
37
|
+
- Gemfile
|
38
|
+
- Gemfile.lock
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- dependency_injection.gemspec
|
42
|
+
- examples/basic_usage.rb
|
43
|
+
- examples/global_container_parameter.rb
|
44
|
+
- examples/inject_a_reference_to_another_dependency.rb
|
45
|
+
- examples/services.yml
|
46
|
+
- examples/yaml_configuration_file.rb
|
47
|
+
- lib/dependency_injection/container.rb
|
48
|
+
- lib/dependency_injection/definition.rb
|
49
|
+
- lib/dependency_injection/loaders/yaml.rb
|
50
|
+
- lib/dependency_injection/version.rb
|
51
|
+
- test/dependency_injection/loaders/test_yaml.rb
|
52
|
+
- test/dependency_injection/test_container.rb
|
53
|
+
- test/dependency_injection/test_definition.rb
|
54
|
+
- test/test_helper.rb
|
55
|
+
homepage: https://github.com/kdisneur/dependency_injection
|
56
|
+
licenses:
|
57
|
+
- MIT
|
58
|
+
metadata: {}
|
59
|
+
post_install_message:
|
60
|
+
rdoc_options: []
|
61
|
+
require_paths:
|
62
|
+
- lib
|
63
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - '>='
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0'
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - '>='
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: '0'
|
73
|
+
requirements: []
|
74
|
+
rubyforge_project:
|
75
|
+
rubygems_version: 2.0.6
|
76
|
+
signing_key:
|
77
|
+
specification_version: 4
|
78
|
+
summary: Dependency Injection system for Ruby
|
79
|
+
test_files:
|
80
|
+
- test/dependency_injection/loaders/test_yaml.rb
|
81
|
+
- test/dependency_injection/test_container.rb
|
82
|
+
- test/dependency_injection/test_definition.rb
|
83
|
+
- test/test_helper.rb
|