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 +4 -4
- data/README.md +0 -17
- data/lib/hypo/container.rb +4 -2
- data/lib/hypo/instance.rb +1 -1
- data/lib/hypo/lifetime/scope.rb +6 -15
- data/lib/hypo/lifetime/singleton.rb +10 -8
- data/lib/hypo/lifetime_friendly.rb +1 -0
- data/lib/hypo/scope.rb +10 -0
- data/lib/hypo/scope_friendly.rb +1 -0
- data/lib/hypo/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eeb29a3bdf425982108ca90f88d03e7de685e3ee
|
4
|
+
data.tar.gz: 758529eb5a151b6f42d8cdc99d4886437aebaa46
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
data/lib/hypo/container.rb
CHANGED
@@ -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
|
-
|
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
|
-
|
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, :
|
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?
|
data/lib/hypo/lifetime/scope.rb
CHANGED
@@ -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
|
-
|
10
|
-
@instances[scope] = {} unless @instances.key? scope
|
5
|
+
instances = component.scope.instances
|
11
6
|
|
12
|
-
unless
|
7
|
+
unless instances.key? component.name
|
13
8
|
if component.respond_to? :type
|
14
|
-
|
9
|
+
instances[component.name] = component.type.new(*component.dependencies)
|
15
10
|
else
|
16
|
-
|
11
|
+
instances[component.name] = component.object
|
17
12
|
end
|
18
13
|
end
|
19
14
|
|
20
|
-
|
15
|
+
instances[component.name]
|
21
16
|
end
|
22
17
|
|
23
18
|
def purge(scope)
|
24
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
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
|
data/lib/hypo/scope_friendly.rb
CHANGED
data/lib/hypo/version.rb
CHANGED
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.
|
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-
|
11
|
+
date: 2017-11-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|