unity-dependency-container 1.0.0 → 1.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +3 -2
- data/README.md +31 -22
- data/lib/unity/dependency_container/version.rb +1 -1
- data/lib/unity/dependency_container.rb +14 -2
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7a83bf465acfb55c0bee367904a600cfe6faba2dbd915407ba572126a5617035
|
4
|
+
data.tar.gz: 25acaee0d86a24f95705fcdab7de8ef9121ffe289d0fe1cc3706b4e2fcbf2a9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4722d4da1cb80d8366d92650f8bce4c76c460b76390d6d5b719dae79fd9ce5771f51d9ea90bc619a5d92adc7ff72151ac47c4714401d6f24c97fc126c84e7475
|
7
|
+
data.tar.gz: '0391a5b0c302773ea8b61bce20bb0b8d8060800e3a701b2a802d09235cb9cac9131d3dff869c2f2c1686971aea887fa4509dce7f88cce73f1c5243c90b47572d'
|
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
unity-dependency-container (1.
|
4
|
+
unity-dependency-container (1.1.1)
|
5
5
|
|
6
6
|
GEM
|
7
7
|
remote: https://rubygems.org/
|
@@ -9,6 +9,7 @@ GEM
|
|
9
9
|
rake (13.0.6)
|
10
10
|
|
11
11
|
PLATFORMS
|
12
|
+
arm64-darwin-21
|
12
13
|
x86_64-darwin-20
|
13
14
|
|
14
15
|
DEPENDENCIES
|
@@ -16,4 +17,4 @@ DEPENDENCIES
|
|
16
17
|
unity-dependency-container!
|
17
18
|
|
18
19
|
BUNDLED WITH
|
19
|
-
2.
|
20
|
+
2.3.3
|
data/README.md
CHANGED
@@ -1,8 +1,6 @@
|
|
1
1
|
# Unity::DependencyContainer
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
TODO: Delete this and the text above, and describe your gem
|
3
|
+
Simple thread-safe dependency injection.
|
6
4
|
|
7
5
|
## Installation
|
8
6
|
|
@@ -12,24 +10,35 @@ Add this line to your application's Gemfile:
|
|
12
10
|
gem 'unity-dependency-container'
|
13
11
|
```
|
14
12
|
|
15
|
-
And then execute:
|
16
|
-
|
17
|
-
$ bundle install
|
18
|
-
|
19
|
-
Or install it yourself as:
|
20
|
-
|
21
|
-
$ gem install unity-dependency-container
|
22
|
-
|
23
13
|
## Usage
|
24
14
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
15
|
+
```ruby
|
16
|
+
class CalculatorService
|
17
|
+
def sum(a, b)
|
18
|
+
a + b
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
module Foo
|
23
|
+
extend Unity::DependencyContainer
|
24
|
+
|
25
|
+
dependency 'services.calculator' do
|
26
|
+
CalculatorService.new
|
27
|
+
end
|
28
|
+
|
29
|
+
# you can also use Symbol as dependencies' names
|
30
|
+
dependency :calculator do
|
31
|
+
CalculatorService.new
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
a = Foo.di('services.calculator').sum(1, 2)
|
36
|
+
b = Foo.with_dependency('services.calculator') do |dep|
|
37
|
+
dep.sum(1, 2)
|
38
|
+
end
|
39
|
+
c = Foo.with_dependency(:calculator) { |dep| dep.sum(1, 2) }
|
40
|
+
|
41
|
+
puts a # => 3
|
42
|
+
puts b # => 3
|
43
|
+
puts c # => 3
|
44
|
+
```
|
@@ -11,11 +11,23 @@ module Unity
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def di_instances
|
14
|
-
|
14
|
+
Thread.current[:di_instances]
|
15
15
|
end
|
16
16
|
|
17
17
|
def di(name)
|
18
|
-
|
18
|
+
instances = Thread.current[:di_instances]
|
19
|
+
return instances[name] if !instances.nil? && instances.key?(name)
|
20
|
+
|
21
|
+
Thread.current[:di_instances] ||= {}
|
22
|
+
|
23
|
+
unless di_container_repository.key?(name)
|
24
|
+
raise Error, "Dependency '#{name}' does not exists"
|
25
|
+
end
|
26
|
+
Thread.current[:di_instances][name] = di_container_repository[name].call
|
27
|
+
end
|
28
|
+
|
29
|
+
def with_dependency(name, &block)
|
30
|
+
yield(di(name))
|
19
31
|
end
|
20
32
|
|
21
33
|
def dependency(name, &block)
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: unity-dependency-container
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- saluzafa
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-03-
|
11
|
+
date: 2022-03-29 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Extend module/class abilities to provide dependencies container/injection
|
14
14
|
email:
|
@@ -49,7 +49,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
49
49
|
- !ruby/object:Gem::Version
|
50
50
|
version: '0'
|
51
51
|
requirements: []
|
52
|
-
rubygems_version: 3.
|
52
|
+
rubygems_version: 3.3.3
|
53
53
|
signing_key:
|
54
54
|
specification_version: 4
|
55
55
|
summary: Unity - Dependencies container utility
|