hypo 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f95c8eb6eb6a8fdcef40c219b52f93ef7f019e57
4
- data.tar.gz: 3026722dd03cdb35709b13c0325b72fd248c3841
3
+ metadata.gz: 2e0d6a53f9f9c68dce9af65d0eaef9b209fd54c6
4
+ data.tar.gz: 70d8862335541f7f9973c93f49934492fef939ca
5
5
  SHA512:
6
- metadata.gz: ea62c01f490442eb5456e23294dda1559b2c96837e85c3f04fe9a2aa4b2b263f24fb16c5c086d3171bd67176f0c94041db39883039d14325a236b064f9bf5c23
7
- data.tar.gz: 1f26a3b5bedefd8a8ab10a5527e20228769e910402686519dd291c55b461bcd88d5a389eba52595dd170fcb3d4abd34279310e6c26d96e4765fa1d450ce22dce
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) # an object
173
+ .bound_to(request) # the scope!
163
174
 
164
- # handle the request
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:
@@ -7,7 +7,7 @@ module Hypo
7
7
  include ScopeFriendly
8
8
  include LifetimeFriendly
9
9
 
10
- attr_reader :name, :type, :container, :lifetime, :scope
10
+ attr_reader :name, :type, :container, :lifetime
11
11
 
12
12
  def initialize(type, container, name = nil)
13
13
  @container = container
@@ -3,6 +3,7 @@ require 'hypo/component'
3
3
  require 'hypo/instance'
4
4
  require 'hypo/lifetime/transient'
5
5
  require 'hypo/lifetime/singleton'
6
+ require 'hypo/lifetime/scope'
6
7
 
7
8
  module Hypo
8
9
  class Container
@@ -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(lifetime)
4
- @lifetime = @container.lifetimes[lifetime]
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
@@ -0,0 +1,7 @@
1
+ module Hypo
2
+ module Scope
3
+ def release
4
+ @container.lifetimes[:scope].purge(self)
5
+ end
6
+ end
7
+ end
@@ -2,7 +2,7 @@ module Hypo
2
2
  module ScopeFriendly
3
3
  def bind_to(scope)
4
4
  if scope.is_a? Symbol
5
- @scope = @container.resolve(scope)
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
@@ -1,3 +1,3 @@
1
1
  module Hypo
2
- VERSION = '0.8.0'
2
+ VERSION = '0.8.1'
3
3
  end
data/lib/hypo.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'hypo/version'
2
2
  require 'hypo/container'
3
+ require 'hypo/scope'
3
4
 
4
5
  module Hypo
5
6
  end
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.0
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