hypo 0.8.0 → 0.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +30 -6
- data/lib/hypo/component.rb +1 -1
- data/lib/hypo/container.rb +1 -0
- data/lib/hypo/lifetime/scope.rb +4 -5
- data/lib/hypo/lifetime_friendly.rb +6 -2
- data/lib/hypo/scope.rb +7 -0
- data/lib/hypo/scope_friendly.rb +10 -1
- data/lib/hypo/version.rb +1 -1
- data/lib/hypo.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2e0d6a53f9f9c68dce9af65d0eaef9b209fd54c6
|
4
|
+
data.tar.gz: 70d8862335541f7f9973c93f49934492fef939ca
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8a2412d9b4226b8816db156cf2cc45ca763dc227005071b1f704263584af6c0b4ae45d14a994d1dd65b3a8c66ab73744311abe619a556b3c611dc8fb2e8b1519
|
7
|
+
data.tar.gz: 2e03e94c0303e02b175d54bdfa94d880d027b5b68d0152c2d9d56b7f9ba952cf7fb5a64b7de2e14f7182b0ca34d1fcbabe573da954fcf6e712b16132cce8e5d3
|
data/README.md
CHANGED
@@ -142,8 +142,19 @@ lifetime.purge
|
|
142
142
|
|
143
143
|
#### Lifetime :scope
|
144
144
|
For most of cases when you need to bind dependency lifetime to lifetime of another item of your application
|
145
|
-
you can use Hypo::Lifetime::Scope (:scope) strategy.
|
145
|
+
you can use Hypo::Lifetime::Scope (:scope) strategy. In order to to that first of all you should implement a scope:
|
146
146
|
|
147
|
+
```ruby
|
148
|
+
class Request
|
149
|
+
include Hypo::Scope
|
150
|
+
|
151
|
+
def handle
|
152
|
+
# do something
|
153
|
+
release
|
154
|
+
# return a result
|
155
|
+
end
|
156
|
+
end
|
157
|
+
```
|
147
158
|
|
148
159
|
```ruby
|
149
160
|
# somewhere in Rack application: application initialization
|
@@ -159,14 +170,27 @@ container
|
|
159
170
|
container
|
160
171
|
.register_instance(request, :request)
|
161
172
|
.using_lifetime(:scope)
|
162
|
-
.bound_to(request) #
|
173
|
+
.bound_to(request) # the scope!
|
163
174
|
|
164
|
-
|
165
|
-
# ...
|
166
|
-
|
167
|
-
lifetime.purge(request)
|
175
|
+
request.handle
|
168
176
|
```
|
169
177
|
|
178
|
+
|
179
|
+
:scope lifetime supports a finalizing for its objects. Class with such ability should respond to "finalize" method:
|
180
|
+
|
181
|
+
```ruby
|
182
|
+
class SQLTransation
|
183
|
+
def initialize
|
184
|
+
@transaction = DB.begin_transaction
|
185
|
+
end
|
186
|
+
|
187
|
+
def finalize
|
188
|
+
@transaction.commit
|
189
|
+
end
|
190
|
+
end
|
191
|
+
```
|
192
|
+
Method "finalize" calls on scope releasing (Hypo::Scope#release).
|
193
|
+
|
170
194
|
### Remove components
|
171
195
|
Sometimes you need to manage a component lifetime manually. Especially it can be useful for "instances".
|
172
196
|
For example, you're going to inject new instance of request parameters every http request in your web application:
|
data/lib/hypo/component.rb
CHANGED
data/lib/hypo/container.rb
CHANGED
data/lib/hypo/lifetime/scope.rb
CHANGED
@@ -6,11 +6,6 @@ module Hypo
|
|
6
6
|
end
|
7
7
|
|
8
8
|
def instance(component)
|
9
|
-
if component.scope.nil?
|
10
|
-
raise ContainerError, "Component \"#{component.name}\" must be bound to a scope" \
|
11
|
-
" according to Hupo::Lifetime::Scope lifetime strategy"
|
12
|
-
end
|
13
|
-
|
14
9
|
scope = component.scope.object_id
|
15
10
|
@instances[scope] = {} unless @instances.key? scope
|
16
11
|
|
@@ -26,6 +21,10 @@ module Hypo
|
|
26
21
|
end
|
27
22
|
|
28
23
|
def purge(scope)
|
24
|
+
@instances[scope.object_id].each_value do |instance|
|
25
|
+
instance.finalize if instance.respond_to? :finalize
|
26
|
+
end
|
27
|
+
|
29
28
|
@instances.delete scope.object_id
|
30
29
|
end
|
31
30
|
end
|
@@ -1,7 +1,11 @@
|
|
1
1
|
module Hypo
|
2
2
|
module LifetimeFriendly
|
3
|
-
def use_lifetime(
|
4
|
-
|
3
|
+
def use_lifetime(name)
|
4
|
+
unless @container.lifetimes.key? name
|
5
|
+
raise ContainerError, "Lifetime with name \"#{name}\" is not registered"
|
6
|
+
end
|
7
|
+
|
8
|
+
@lifetime = @container.lifetimes[name]
|
5
9
|
|
6
10
|
self
|
7
11
|
end
|
data/lib/hypo/scope.rb
ADDED
data/lib/hypo/scope_friendly.rb
CHANGED
@@ -2,7 +2,7 @@ module Hypo
|
|
2
2
|
module ScopeFriendly
|
3
3
|
def bind_to(scope)
|
4
4
|
if scope.is_a? Symbol
|
5
|
-
@
|
5
|
+
@scope_name = scope
|
6
6
|
else
|
7
7
|
@scope = scope
|
8
8
|
end
|
@@ -10,6 +10,15 @@ module Hypo
|
|
10
10
|
self
|
11
11
|
end
|
12
12
|
|
13
|
+
def scope
|
14
|
+
if @scope.nil? && @scope_name.nil?
|
15
|
+
raise ContainerError, "Component \"#{@name}\" must be bound to a scope" \
|
16
|
+
" according to Hypo::Lifetime::Scope lifetime strategy"
|
17
|
+
end
|
18
|
+
|
19
|
+
@scope || @container.resolve(@scope_name)
|
20
|
+
end
|
21
|
+
|
13
22
|
alias bound_to bind_to
|
14
23
|
end
|
15
24
|
end
|
data/lib/hypo/version.rb
CHANGED
data/lib/hypo.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
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.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Vladimir Kalinkin
|
@@ -78,6 +78,7 @@ files:
|
|
78
78
|
- lib/hypo/lifetime/singleton.rb
|
79
79
|
- lib/hypo/lifetime/transient.rb
|
80
80
|
- lib/hypo/lifetime_friendly.rb
|
81
|
+
- lib/hypo/scope.rb
|
81
82
|
- lib/hypo/scope_friendly.rb
|
82
83
|
- lib/hypo/version.rb
|
83
84
|
homepage: https://github.com/cylon-v/hypo
|