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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2ebccc95a98e2bb957d09819d66793e2a84aba0f29f6ba6a6173abbb8a9cf85c
4
- data.tar.gz: b77f544d0f1fc4317761b5ccb913d5a0695b011645301c937b336430c47cccb0
3
+ metadata.gz: 7a83bf465acfb55c0bee367904a600cfe6faba2dbd915407ba572126a5617035
4
+ data.tar.gz: 25acaee0d86a24f95705fcdab7de8ef9121ffe289d0fe1cc3706b4e2fcbf2a9f
5
5
  SHA512:
6
- metadata.gz: 10ae97b11f95e14207a1ebd551e01016dab0f4646b8ff8052239a856f05e933e4a9414ed6c01b39a51e412fda8c73fd0e120c40f0d19ed8a4e21dd6c7eb6e765
7
- data.tar.gz: b1ea601ca9bcace408908202827c0b07d95f9474dade8436d5efa81503aed7c8de3b87267171ff30fb5ad94871a13945f11edac90b122afde4500c6bda2e87ca
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.0.0)
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.2.30
20
+ 2.3.3
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # Unity::DependencyContainer
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/unity/dependency_container`. To experiment with that code, run `bin/console` for an interactive prompt.
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
- TODO: Write usage instructions here
26
-
27
- ## Development
28
-
29
- After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
-
31
- To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
-
33
- ## Contributing
34
-
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/unity-dependency-container.
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
+ ```
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Unity
4
4
  module DependencyContainer
5
- VERSION = '1.0.0'
5
+ VERSION = '1.1.2'
6
6
  end
7
7
  end
@@ -11,11 +11,23 @@ module Unity
11
11
  end
12
12
 
13
13
  def di_instances
14
- @di_instances ||= {}
14
+ Thread.current[:di_instances]
15
15
  end
16
16
 
17
17
  def di(name)
18
- di_instances[name] ||= di_container_repository[name].call
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.0.0
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-15 00:00:00.000000000 Z
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.2.3
52
+ rubygems_version: 3.3.3
53
53
  signing_key:
54
54
  specification_version: 4
55
55
  summary: Unity - Dependencies container utility