hypo 0.8.1 → 0.8.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
  SHA1:
3
- metadata.gz: 2e0d6a53f9f9c68dce9af65d0eaef9b209fd54c6
4
- data.tar.gz: 70d8862335541f7f9973c93f49934492fef939ca
3
+ metadata.gz: eeb29a3bdf425982108ca90f88d03e7de685e3ee
4
+ data.tar.gz: 758529eb5a151b6f42d8cdc99d4886437aebaa46
5
5
  SHA512:
6
- metadata.gz: 8a2412d9b4226b8816db156cf2cc45ca763dc227005071b1f704263584af6c0b4ae45d14a994d1dd65b3a8c66ab73744311abe619a556b3c611dc8fb2e8b1519
7
- data.tar.gz: 2e03e94c0303e02b175d54bdfa94d880d027b5b68d0152c2d9d56b7f9ba952cf7fb5a64b7de2e14f7182b0ca34d1fcbabe573da954fcf6e712b16132cce8e5d3
6
+ metadata.gz: 51bfdac12ce95e82b6791d59786b98cd59811235edad08f3045a212b06246adebbb3f97b7be32799a49c29b30ca44d3ec0c3d2254ea428df3b845356559ac602
7
+ data.tar.gz: 5cf5df30c587ed234fccc25e32ad773c6db5e6449d9b94f2d73d797a8137e8707f4530a39c6d3fe488efd9061ce6ae7bc14e4ddc0b5b2c6626b23182116d499c
data/README.md CHANGED
@@ -191,23 +191,6 @@ end
191
191
  ```
192
192
  Method "finalize" calls on scope releasing (Hypo::Scope#release).
193
193
 
194
- ### Remove components
195
- Sometimes you need to manage a component lifetime manually. Especially it can be useful for "instances".
196
- For example, you're going to inject new instance of request parameters every http request in your web application:
197
-
198
- ```ruby
199
- # somewhere in Rack application: request handling
200
- # ...
201
- query_string = env['QUERY_STRING']
202
- container.register_instance(query_string, :query_string)
203
-
204
- # handle the request
205
- # ...
206
-
207
- container.remove(:query_string)
208
- # ...
209
- ```
210
-
211
194
  ## Development
212
195
 
213
196
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -12,6 +12,8 @@ module Hypo
12
12
  def initialize
13
13
  @components = {}
14
14
  @lifetimes = {}
15
+ @mutex = Mutex.new
16
+
15
17
  add_lifetime(Lifetime::Transient.new, :transient)
16
18
  add_lifetime(Lifetime::Singleton.new, :singleton)
17
19
  add_lifetime(Lifetime::Scope.new, :scope)
@@ -26,7 +28,7 @@ module Hypo
26
28
 
27
29
  component = Component.new(item, self, name)
28
30
 
29
- unless @components.key?(component.name)
31
+ @mutex.synchronize do
30
32
  @components[component.name] = component
31
33
  end
32
34
 
@@ -39,7 +41,7 @@ module Hypo
39
41
  'If you wanna register a type please use method "register".'
40
42
  end
41
43
 
42
- unless @components.key?(name)
44
+ @mutex.synchronize do
43
45
  instance = Instance.new(item, self, name)
44
46
  @components[name] = instance
45
47
  end
data/lib/hypo/instance.rb CHANGED
@@ -6,7 +6,7 @@ module Hypo
6
6
  include ScopeFriendly
7
7
  include LifetimeFriendly
8
8
 
9
- attr_reader :name, :container, :scope, :object
9
+ attr_reader :name, :container, :object
10
10
 
11
11
  def initialize(object, container, name)
12
12
  raise ContainerError, 'Registered object should have a name' if name.nil?
@@ -1,31 +1,22 @@
1
1
  module Hypo
2
2
  module Lifetime
3
3
  class Scope
4
- def initialize
5
- @instances = {}
6
- end
7
-
8
4
  def instance(component)
9
- scope = component.scope.object_id
10
- @instances[scope] = {} unless @instances.key? scope
5
+ instances = component.scope.instances
11
6
 
12
- unless @instances[scope].key? component.name
7
+ unless instances.key? component.name
13
8
  if component.respond_to? :type
14
- @instances[scope][component.name] = component.type.new(*component.dependencies)
9
+ instances[component.name] = component.type.new(*component.dependencies)
15
10
  else
16
- @instances[scope][component.name] = component.object
11
+ instances[component.name] = component.object
17
12
  end
18
13
  end
19
14
 
20
- @instances[scope][component.name]
15
+ instances[component.name]
21
16
  end
22
17
 
23
18
  def purge(scope)
24
- @instances[scope.object_id].each_value do |instance|
25
- instance.finalize if instance.respond_to? :finalize
26
- end
27
-
28
- @instances.delete scope.object_id
19
+ scope.purge
29
20
  end
30
21
  end
31
22
  end
@@ -2,19 +2,21 @@ module Hypo
2
2
  module Lifetime
3
3
  class Singleton
4
4
  def initialize
5
- @instances = {}
5
+ @instances = Hash.new
6
+ @mutex = Mutex.new
6
7
  end
7
8
 
8
9
  def instance(component)
9
- unless @instances.key? component.name
10
- if component.respond_to? :type
11
- @instances[component.name] = component.type.new(*component.dependencies)
12
- else
13
- @instances[component.name] = component.object
10
+ @instances[component.name]
11
+ end
12
+
13
+ def preload(component)
14
+ @mutex.synchronize do
15
+ unless @instances.key? component.name
16
+ instance = component.respond_to?(:type) ? component.type.new(*component.dependencies) : component.object
17
+ @instances[component.name] = instance
14
18
  end
15
19
  end
16
-
17
- @instances[component.name]
18
20
  end
19
21
  end
20
22
  end
@@ -6,6 +6,7 @@ module Hypo
6
6
  end
7
7
 
8
8
  @lifetime = @container.lifetimes[name]
9
+ @lifetime.preload(self) if @lifetime.respond_to? :preload
9
10
 
10
11
  self
11
12
  end
data/lib/hypo/scope.rb CHANGED
@@ -1,7 +1,17 @@
1
1
  module Hypo
2
2
  module Scope
3
+ attr_reader :instances
4
+
3
5
  def release
4
6
  @container.lifetimes[:scope].purge(self)
5
7
  end
8
+
9
+ def purge
10
+ @instances.each_value do |instance|
11
+ instance.finalize if instance.respond_to? :finalize
12
+ end
13
+
14
+ @instances = Hash.new
15
+ end
6
16
  end
7
17
  end
@@ -5,6 +5,7 @@ module Hypo
5
5
  @scope_name = scope
6
6
  else
7
7
  @scope = scope
8
+ @scope.instance_variable_set('@instances'.freeze, Hash.new)
8
9
  end
9
10
 
10
11
  self
data/lib/hypo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hypo
2
- VERSION = '0.8.1'
2
+ VERSION = '0.8.2'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hypo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vladimir Kalinkin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-09-24 00:00:00.000000000 Z
11
+ date: 2017-11-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler