dry-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 +50 -0
- data/.rspec +1 -0
- data/.travis.yml +8 -0
- data/Gemfile +10 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/dry-dependency-injection.gemspec +30 -0
- data/example/plugins/first.rb +9 -0
- data/example/plugins/ng/third.rb +11 -0
- data/example/plugins/registry.rb +3 -0
- data/example/plugins/second.rb +9 -0
- data/example/setup.rb +19 -0
- data/lib/dry-dependency-injection.rb +1 -0
- data/lib/dry/dependency_injection.rb +2 -0
- data/lib/dry/dependency_injection/eager.rb +6 -0
- data/lib/dry/dependency_injection/importer.rb +34 -0
- data/lib/dry/dependency_injection/item.rb +38 -0
- data/lib/dry/dependency_injection/registry.rb +24 -0
- data/lib/dry/dependency_injection/singletons.rb +103 -0
- data/lib/dry/dependency_injection/version.rb +5 -0
- data/spec/importer_spec.rb +13 -0
- data/spec/singletons_spec.rb +78 -0
- data/spec/spec_helper.rb +104 -0
- metadata +182 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 03f2cafa34e909ca7b5632c44914297b0e00bb28
|
4
|
+
data.tar.gz: 0102c60c8bb0b49ee385b48d51ef17d244afa2e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 344d31869f359d01640681d673f2b5920356d90fa3a545a61a48f6b38a86772815b93513d0c1f092b9b294bc5ae1b30519b9ae67360715670280d003b65d55aa
|
7
|
+
data.tar.gz: c5b80edf5134ff62b4ca2491663a5eff34b41c8dbbe8582b86f0b1fa23763c15f99a2f04319634a665d86321111366a3bb2c89025e2e835e42d9c7866f8b892e
|
data/.gitignore
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
.ruby-version
|
47
|
+
# .ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--require spec_helper
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Christian Meier
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
[gem]: https://rubygems.org/gems/dry-dependency-injection
|
2
|
+
[travis]: https://travis-ci.org/mkristian/dry-dependency-injection
|
3
|
+
[gemnasium]: https://gemnasium.com/mkristian/dry-dependency-injection
|
4
|
+
[codeclimate]: https://codeclimate.com/github/mkristian/dry-dependency-injection
|
5
|
+
[coveralls]: https://coveralls.io/github/mkristian/dry-dependency-injection?branch=master
|
6
|
+
[codeissues]: https://codeclimate.com/github/mkristian/dry-dependency-injection
|
7
|
+
|
8
|
+
# dry-dependency-injection
|
9
|
+
more containers derived from dry-container
|
10
|
+
|
11
|
+
[![Gem Version](https://badge.fury.io/rb/dry-dependency-injection.svg)][gem]
|
12
|
+
[![Build Status](https://travis-ci.org/mkristian/dry-dependency-injection.svg?branch=master)][travis]
|
13
|
+
[![Dependency Status](https://gemnasium.com/badges/github.com/mkristian/dry-dependency-injection.svg)][gemnasium]
|
14
|
+
[![Code Climate](https://codeclimate.com/github/mkristian/dry-dependency-injection/badges/gpa.svg)][codeclimate]
|
15
|
+
[![Coverage Status](https://coveralls.io/repos/github/mkristian/dry-dependency-injection/badge.svg?branch=master)][coveralls]
|
16
|
+
[![Issue Count](https://codeclimate.com/github/mkristian/dry-dependency-injection/badges/issue_count.svg)][codeissues]
|
17
|
+
|
18
|
+
## Rubygems/Bundler
|
19
|
+
|
20
|
+
```
|
21
|
+
gem install dry-dependency-injection
|
22
|
+
```
|
23
|
+
|
24
|
+
or Gemfile:
|
25
|
+
```
|
26
|
+
gem 'dry-dependency-injection'
|
27
|
+
```
|
28
|
+
|
29
|
+
## Singleton Container with Dependency Injection
|
30
|
+
|
31
|
+
The idea is to have components or services which all are using dry-auto_inject to inject their dependencies. The `Dry::More::Container::Singleton` is dry-container with a special resovler. You need register the service/component class itself (not the an instance of it):
|
32
|
+
|
33
|
+
``` Ruby
|
34
|
+
class Singletons; extend Dry::DependencyInjection::Singletons; end
|
35
|
+
Import = Dry::AutoInject(Singletons)
|
36
|
+
|
37
|
+
class A; include Import['b']; end
|
38
|
+
class B; include Import['c']; end
|
39
|
+
class C; include Import['d']; end
|
40
|
+
class D; end
|
41
|
+
|
42
|
+
singletons.register('a', A)
|
43
|
+
singletons.register('b', B)
|
44
|
+
singletons.register('c', C)
|
45
|
+
singletons.register('d', D)
|
46
|
+
```
|
47
|
+
|
48
|
+
On retrieve the singleton container will create a single instance of the class under the given key and also resolves the dependencies as well in the same manner:
|
49
|
+
|
50
|
+
``` Ruby
|
51
|
+
Singletons['a']
|
52
|
+
|
53
|
+
```
|
54
|
+
|
55
|
+
Now the singleton container has an instance of all services/components.
|
56
|
+
|
57
|
+
On circular dependencies there will be an `Dry::Container::Error`.
|
58
|
+
|
59
|
+
See also [singletons_spec](spec/singletons_spec.rb).
|
60
|
+
|
61
|
+
|
62
|
+
## Finalize, Lazy vs. Eager
|
63
|
+
|
64
|
+
The container is instantiating the components per default in a lazy way. The `finalize` method on the container does ensure all eager components get initialized. If you configure the container to be not-lazy then the `finalize` will instantiate **all** components. If the container is lazy then `finalize` only instantiate the components which are marks as **eager** by extending the component with `Dry::DependencyInjection::Eager`
|
65
|
+
|
66
|
+
```Ruby
|
67
|
+
class MyEagerComponent
|
68
|
+
extend Dry::DependencyInjection::Eager
|
69
|
+
```
|
70
|
+
|
71
|
+
In case there are no eager component then `finalize` is noop. The eager loading must be first configured and then the `finalize` method needs to be called:
|
72
|
+
|
73
|
+
``` Ruby
|
74
|
+
container.config.lazy = false
|
75
|
+
container.finalize
|
76
|
+
```
|
77
|
+
|
78
|
+
## Plugin Example
|
79
|
+
|
80
|
+
Please see the simple plugin implementation in [example](example) provided.
|
81
|
+
|
82
|
+
## Contributing
|
83
|
+
|
84
|
+
Bug reports, comments and pull requests are welcome.
|
85
|
+
|
86
|
+
## Meta-Foo
|
87
|
+
|
88
|
+
be happy and enjoy.
|
@@ -0,0 +1,30 @@
|
|
1
|
+
lib = File.expand_path("../lib", __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "dry/dependency_injection/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "dry-dependency-injection"
|
7
|
+
spec.version = Dry::DependencyInjection::VERSION
|
8
|
+
spec.authors = [""]
|
9
|
+
spec.email = [""]
|
10
|
+
spec.summary = "add dependency injection to dry-container"
|
11
|
+
spec.description = spec.summary
|
12
|
+
spec.homepage = "https://github.com/mkristian/dry-dependency-injection"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
17
|
+
spec.require_paths = ["lib"]
|
18
|
+
|
19
|
+
spec.required_ruby_version = '>= 2.2.0'
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "concurrent-ruby", "~> 1.0"
|
22
|
+
spec.add_runtime_dependency "dry-container", "~> 0.6"
|
23
|
+
spec.add_runtime_dependency "dry-core", "~> 0.4"
|
24
|
+
|
25
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
26
|
+
spec.add_development_dependency "dry-auto_inject", "~> 0.4"
|
27
|
+
spec.add_development_dependency "dry-inflector", "~> 0.4"
|
28
|
+
spec.add_development_dependency "rake", "~> 12.0"
|
29
|
+
spec.add_development_dependency "rspec", "~> 3.4"
|
30
|
+
end
|
data/example/setup.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'dry-dependency-injection'
|
2
|
+
require 'dry-auto_inject'
|
3
|
+
|
4
|
+
class Singletons; extend Dry::DependencyInjection::Singletons; end
|
5
|
+
Dependency = Dry::AutoInject(Singletons)
|
6
|
+
path = File.join(File.dirname(File.expand_path(__FILE__)), 'plugins')
|
7
|
+
Dry::DependencyInjection::Importer.new(Singletons).import(path).finalize
|
8
|
+
|
9
|
+
class App
|
10
|
+
include Dependency['registry']
|
11
|
+
|
12
|
+
def run
|
13
|
+
registry.keys.each_with_object({}) do |key, map|
|
14
|
+
map[key] = registry[key].to_s
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
#p App.new.run
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'dry/dependency_injection'
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'dry/core/inflector'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module DependencyInjection
|
5
|
+
class Importer
|
6
|
+
|
7
|
+
def initialize(container)
|
8
|
+
@container = container
|
9
|
+
end
|
10
|
+
|
11
|
+
def import(path, prefix = '')
|
12
|
+
unless File.directory?(path)
|
13
|
+
raise ArgumentError.new("path must be directory: #{path}")
|
14
|
+
end
|
15
|
+
full = File.expand_path(path)
|
16
|
+
Dir[File.join(full, prefix, '**', '*.rb')].each do |file|
|
17
|
+
require file
|
18
|
+
subpath = file.gsub(/#{full}\/|\.rb/, '')
|
19
|
+
class_name = Dry::Core::Inflector.classify(subpath)
|
20
|
+
clazz = Dry::Core::Inflector.constantize(class_name)
|
21
|
+
if clazz.is_a?(Class)
|
22
|
+
key = subpath.gsub('/', '.')
|
23
|
+
@container.register(key, clazz)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def finalize
|
30
|
+
@container.finalize if @container.respond_to?(:finalize)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module Dry
|
2
|
+
module DependencyInjection
|
3
|
+
class Item
|
4
|
+
|
5
|
+
def initialize(lock, clazz)
|
6
|
+
unless clazz.is_a?(Class)
|
7
|
+
raise Dry::Container::Error, "needs to be Class object: #{clazz}"
|
8
|
+
end
|
9
|
+
@_lock = lock
|
10
|
+
@clazz = clazz
|
11
|
+
@circular = false
|
12
|
+
end
|
13
|
+
|
14
|
+
def call
|
15
|
+
@_lock.acquire_read_lock
|
16
|
+
unless @instance
|
17
|
+
@instance = create
|
18
|
+
end
|
19
|
+
@instance
|
20
|
+
ensure
|
21
|
+
@_lock.release_read_lock
|
22
|
+
end
|
23
|
+
|
24
|
+
def create
|
25
|
+
@_lock.acquire_write_lock
|
26
|
+
begin
|
27
|
+
if @circular
|
28
|
+
raise Dry::Container::Error, "circular dependency detected: #{@clazz} depends on itself"
|
29
|
+
end
|
30
|
+
@circular = true
|
31
|
+
@clazz.new
|
32
|
+
ensure
|
33
|
+
@_lock.release_write_lock
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require_relative 'item'
|
2
|
+
|
3
|
+
module Dry
|
4
|
+
module DependencyInjection
|
5
|
+
class Registry
|
6
|
+
|
7
|
+
def initialize
|
8
|
+
@_lock = Concurrent::ReentrantReadWriteLock.new
|
9
|
+
@_mutex = Concurrent::Synchronization::Lock.new
|
10
|
+
end
|
11
|
+
|
12
|
+
def call(container, key, item, _options)
|
13
|
+
key = key.to_s.dup.freeze
|
14
|
+
@_mutex.synchronize do
|
15
|
+
if container.key?(key)
|
16
|
+
raise Dry::Container::Error, "There is already an item registered with the key #{key.inspect}"
|
17
|
+
end
|
18
|
+
|
19
|
+
container[key] = Item.new(@_lock, item)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'concurrent'
|
2
|
+
require 'dry-container'
|
3
|
+
require_relative 'registry'
|
4
|
+
require_relative 'eager'
|
5
|
+
|
6
|
+
module Dry
|
7
|
+
module DependencyInjection
|
8
|
+
module Singletons
|
9
|
+
|
10
|
+
def self.extended(base)
|
11
|
+
hooks_mod = ::Module.new do
|
12
|
+
def inherited(subclass)
|
13
|
+
subclass.instance_variable_set(:@_lock, Mutex.new)
|
14
|
+
subclass.instance_variable_set(:@_lock, @_eager.dup)
|
15
|
+
super
|
16
|
+
end
|
17
|
+
end
|
18
|
+
base.class_eval do
|
19
|
+
extend Dry::Container::Mixin
|
20
|
+
extend hooks_mod
|
21
|
+
|
22
|
+
setting :lazy, true
|
23
|
+
setting :registry, Registry.new
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def register(*args)
|
27
|
+
register_singleton(*args)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
@_eager = []
|
32
|
+
@_lock = Mutex.new
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
module Initializer
|
37
|
+
def initialize(*, &block)
|
38
|
+
@_eager = []
|
39
|
+
@_lock = Mutex.new
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.included(base)
|
45
|
+
base.class_eval do
|
46
|
+
include ::Dry::Container::Mixin
|
47
|
+
prepend Initializer
|
48
|
+
|
49
|
+
setting :lazy, true
|
50
|
+
setting :registry, Registry.new
|
51
|
+
|
52
|
+
def register(*args)
|
53
|
+
register_singleton(*args)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def finalize
|
59
|
+
@_lock.synchronize do
|
60
|
+
if config.lazy
|
61
|
+
finalize_lazy
|
62
|
+
else
|
63
|
+
finalize_eager
|
64
|
+
end
|
65
|
+
@_finalized = ! config.lazy
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
private
|
70
|
+
|
71
|
+
def register_singleton(key, item = nil, options = {}, &block)
|
72
|
+
@_lock.synchronize do
|
73
|
+
check_finalized
|
74
|
+
end
|
75
|
+
if block_given?
|
76
|
+
raise Dry::Container::Error, 'can not register block'
|
77
|
+
else
|
78
|
+
config.registry.call(_container, key, item, options)
|
79
|
+
@_eager << key if options[:eager] || item.kind_of?(Eager)
|
80
|
+
self
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
def check_finalized
|
85
|
+
if @_finalized
|
86
|
+
raise Dry::Container::Error, 'can not register. container already finalized'
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def finalize_eager
|
91
|
+
keys.each do |key|
|
92
|
+
self[key]
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def finalize_lazy
|
97
|
+
@_eager.each do |key|
|
98
|
+
self[key]
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'dry/dependency_injection/importer'
|
2
|
+
$LOAD_PATH << 'example'
|
3
|
+
|
4
|
+
describe Dry::DependencyInjection::Importer do
|
5
|
+
|
6
|
+
it 'imports the example plugins' do
|
7
|
+
require 'setup'
|
8
|
+
|
9
|
+
result = App.new.run
|
10
|
+
expect(result.keys).to match_array ['first', 'second', 'ng.third']
|
11
|
+
expect(result.values.collect{|v| v.sub(/:0.*/, '')}).to match_array ['#<First', '#<Second', '#<Ng::Third']
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,78 @@
|
|
1
|
+
require 'dry/dependency_injection/singletons'
|
2
|
+
require 'dry-auto_inject'
|
3
|
+
describe Dry::DependencyInjection::Singletons do
|
4
|
+
|
5
|
+
class Lazy; include Dry::DependencyInjection::Singletons; end
|
6
|
+
Import = Dry::AutoInject(Lazy.new)
|
7
|
+
Dependencies = Dry::AutoInject(Lazy.new)
|
8
|
+
|
9
|
+
class Eager; extend Dry::DependencyInjection::Singletons; end
|
10
|
+
Eager.config.lazy = false
|
11
|
+
Include = Dry::AutoInject(Eager)
|
12
|
+
|
13
|
+
class A; include Import['b']; end
|
14
|
+
|
15
|
+
class B; include Import['c']; end
|
16
|
+
|
17
|
+
class C; include Import['d']; end
|
18
|
+
|
19
|
+
class D; end
|
20
|
+
|
21
|
+
class E; include Import['d']; include Import['f']; end
|
22
|
+
|
23
|
+
class F; include Import['e']; end
|
24
|
+
|
25
|
+
class G; include Include['h']; end
|
26
|
+
|
27
|
+
class H; include Include['g']; end
|
28
|
+
|
29
|
+
class I; attr_accessor :plugin; end
|
30
|
+
|
31
|
+
class J
|
32
|
+
include Dependencies['i']
|
33
|
+
extend Dry::DependencyInjection::Eager
|
34
|
+
|
35
|
+
def initialize(i:)
|
36
|
+
super
|
37
|
+
i.plugin = self
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class K; include Dependencies['l']; end
|
42
|
+
|
43
|
+
class L; include Dependencies['k']; end
|
44
|
+
|
45
|
+
['a', 'b', 'c', 'd', 'e', 'f'].each do |name|
|
46
|
+
Import.container.register(name, Object.const_get(name.upcase))
|
47
|
+
end
|
48
|
+
|
49
|
+
['g', 'h'].each do |name|
|
50
|
+
Include.container.register(name, Object.const_get(name.upcase))
|
51
|
+
end
|
52
|
+
|
53
|
+
['i', 'j', 'k', 'l'].each do |name|
|
54
|
+
Dependencies.container.register(name, Object.const_get(name.upcase))
|
55
|
+
end
|
56
|
+
|
57
|
+
it 'does initialize' do
|
58
|
+
['a', 'b', 'c', 'd'].each do |name|
|
59
|
+
expect(Import.container[name].class).to eq Object.const_get(name.upcase)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'does initialize eager component' do
|
64
|
+
expect(Dependencies.container['i'].plugin).to eq nil
|
65
|
+
Dependencies.container.finalize
|
66
|
+
expect(Dependencies.container['i'].plugin).to eq Dependencies.container['j']
|
67
|
+
# all other components are still lazy and contain a circular dependency
|
68
|
+
expect { K.new }.to raise_error Dry::Container::Error
|
69
|
+
end
|
70
|
+
|
71
|
+
it 'bails out on circular dependencies' do
|
72
|
+
expect { E.new }.to raise_error Dry::Container::Error
|
73
|
+
end
|
74
|
+
|
75
|
+
it 'bails out on finalize' do
|
76
|
+
expect { Include.container.finalize }.to raise_error Dry::Container::Error
|
77
|
+
end
|
78
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
require 'pry'
|
2
|
+
require 'coveralls'
|
3
|
+
Coveralls.wear!
|
4
|
+
|
5
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
6
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
7
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
8
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
9
|
+
# files.
|
10
|
+
#
|
11
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
12
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
13
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
14
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
15
|
+
# a separate helper file that requires the additional dependencies and performs
|
16
|
+
# the additional setup, and require it from the spec files that actually need
|
17
|
+
# it.
|
18
|
+
#
|
19
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
20
|
+
RSpec.configure do |config|
|
21
|
+
# rspec-expectations config goes here. You can use an alternate
|
22
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
23
|
+
# assertions if you prefer.
|
24
|
+
config.expect_with :rspec do |expectations|
|
25
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
26
|
+
# and `failure_message` of custom matchers include text for helper methods
|
27
|
+
# defined using `chain`, e.g.:
|
28
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
29
|
+
# # => "be bigger than 2 and smaller than 4"
|
30
|
+
# ...rather than:
|
31
|
+
# # => "be bigger than 2"
|
32
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
33
|
+
end
|
34
|
+
|
35
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
36
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
37
|
+
config.mock_with :rspec do |mocks|
|
38
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
39
|
+
# a real object. This is generally recommended, and will default to
|
40
|
+
# `true` in RSpec 4.
|
41
|
+
mocks.verify_partial_doubles = true
|
42
|
+
end
|
43
|
+
|
44
|
+
# This option will default to `:apply_to_host_groups` in RSpec 4 (and will
|
45
|
+
# have no way to turn it off -- the option exists only for backwards
|
46
|
+
# compatibility in RSpec 3). It causes shared context metadata to be
|
47
|
+
# inherited by the metadata hash of host groups and examples, rather than
|
48
|
+
# triggering implicit auto-inclusion in groups with matching metadata.
|
49
|
+
config.shared_context_metadata_behavior = :apply_to_host_groups
|
50
|
+
|
51
|
+
# The settings below are suggested to provide a good initial experience
|
52
|
+
# with RSpec, but feel free to customize to your heart's content.
|
53
|
+
=begin
|
54
|
+
# This allows you to limit a spec run to individual examples or groups
|
55
|
+
# you care about by tagging them with `:focus` metadata. When nothing
|
56
|
+
# is tagged with `:focus`, all examples get run. RSpec also provides
|
57
|
+
# aliases for `it`, `describe`, and `context` that include `:focus`
|
58
|
+
# metadata: `fit`, `fdescribe` and `fcontext`, respectively.
|
59
|
+
config.filter_run_when_matching :focus
|
60
|
+
|
61
|
+
# Allows RSpec to persist some state between runs in order to support
|
62
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
63
|
+
# you configure your source control system to ignore this file.
|
64
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
65
|
+
|
66
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
67
|
+
# recommended. For more details, see:
|
68
|
+
# - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/
|
69
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
70
|
+
# - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode
|
71
|
+
config.disable_monkey_patching!
|
72
|
+
|
73
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
74
|
+
# be too noisy due to issues in dependencies.
|
75
|
+
config.warnings = true
|
76
|
+
|
77
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
78
|
+
# file, and it's useful to allow more verbose output when running an
|
79
|
+
# individual spec file.
|
80
|
+
if config.files_to_run.one?
|
81
|
+
# Use the documentation formatter for detailed output,
|
82
|
+
# unless a formatter has already been configured
|
83
|
+
# (e.g. via a command-line flag).
|
84
|
+
config.default_formatter = "doc"
|
85
|
+
end
|
86
|
+
|
87
|
+
# Print the 10 slowest examples and example groups at the
|
88
|
+
# end of the spec run, to help surface which specs are running
|
89
|
+
# particularly slow.
|
90
|
+
config.profile_examples = 10
|
91
|
+
|
92
|
+
# Run specs in random order to surface order dependencies. If you find an
|
93
|
+
# order dependency and want to debug it, you can fix the order by providing
|
94
|
+
# the seed, which is printed after each run.
|
95
|
+
# --seed 1234
|
96
|
+
config.order = :random
|
97
|
+
|
98
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
99
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
100
|
+
# test failures related to randomization by passing the same `--seed` value
|
101
|
+
# as the one that triggered the failure.
|
102
|
+
Kernel.srand config.seed
|
103
|
+
=end
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,182 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dry-dependency-injection
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ''
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-26 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: concurrent-ruby
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: dry-container
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.6'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: dry-core
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.4'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.7'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.7'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: dry-auto_inject
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0.4'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0.4'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: dry-inflector
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.4'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.4'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rake
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '12.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '12.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rspec
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '3.4'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '3.4'
|
125
|
+
description: add dependency injection to dry-container
|
126
|
+
email:
|
127
|
+
- ''
|
128
|
+
executables: []
|
129
|
+
extensions: []
|
130
|
+
extra_rdoc_files: []
|
131
|
+
files:
|
132
|
+
- ".gitignore"
|
133
|
+
- ".rspec"
|
134
|
+
- ".travis.yml"
|
135
|
+
- Gemfile
|
136
|
+
- LICENSE
|
137
|
+
- README.md
|
138
|
+
- dry-dependency-injection.gemspec
|
139
|
+
- example/plugins/first.rb
|
140
|
+
- example/plugins/ng/third.rb
|
141
|
+
- example/plugins/registry.rb
|
142
|
+
- example/plugins/second.rb
|
143
|
+
- example/setup.rb
|
144
|
+
- lib/dry-dependency-injection.rb
|
145
|
+
- lib/dry/dependency_injection.rb
|
146
|
+
- lib/dry/dependency_injection/eager.rb
|
147
|
+
- lib/dry/dependency_injection/importer.rb
|
148
|
+
- lib/dry/dependency_injection/item.rb
|
149
|
+
- lib/dry/dependency_injection/registry.rb
|
150
|
+
- lib/dry/dependency_injection/singletons.rb
|
151
|
+
- lib/dry/dependency_injection/version.rb
|
152
|
+
- spec/importer_spec.rb
|
153
|
+
- spec/singletons_spec.rb
|
154
|
+
- spec/spec_helper.rb
|
155
|
+
homepage: https://github.com/mkristian/dry-dependency-injection
|
156
|
+
licenses:
|
157
|
+
- MIT
|
158
|
+
metadata: {}
|
159
|
+
post_install_message:
|
160
|
+
rdoc_options: []
|
161
|
+
require_paths:
|
162
|
+
- lib
|
163
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
164
|
+
requirements:
|
165
|
+
- - ">="
|
166
|
+
- !ruby/object:Gem::Version
|
167
|
+
version: 2.2.0
|
168
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
169
|
+
requirements:
|
170
|
+
- - ">="
|
171
|
+
- !ruby/object:Gem::Version
|
172
|
+
version: '0'
|
173
|
+
requirements: []
|
174
|
+
rubyforge_project:
|
175
|
+
rubygems_version: 2.5.1
|
176
|
+
signing_key:
|
177
|
+
specification_version: 4
|
178
|
+
summary: add dependency injection to dry-container
|
179
|
+
test_files:
|
180
|
+
- spec/importer_spec.rb
|
181
|
+
- spec/singletons_spec.rb
|
182
|
+
- spec/spec_helper.rb
|