hypo 0.6.2 → 0.7.0

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: 1671c3b418f72e704152244916967ebe0c2288d4
4
- data.tar.gz: 55fde3998486b713e045de9878e45fa5ac336242
3
+ metadata.gz: 1a5df4c1be9652a444db79865461197009cf9b49
4
+ data.tar.gz: 71d0370d8cf8f3e9f672651c1ecab2e61e477047
5
5
  SHA512:
6
- metadata.gz: bcd9ba4639e366d6f74f7275561aa9bc4c2a84c16907f7521dfb07c63564d8d4dcf9f011b7ff68738a8108ee3c7747d3eb6c48371d79f69aa7a25af052d76fe2
7
- data.tar.gz: 267d919d6d1c9a2316747c47a3f94f95072a55137972cd8aa93ee4c471577b298fc7d165dc959f0ee9842afba1e3cd0a49666bb015789a3a392acad84104fdf7
6
+ metadata.gz: 19459aa0a3c3a230855b25d3750c2540d958732698791f02dce185ff6b2a7c3de885b10ba84e8b303495a5aa11cb811a6eaf862efce9f98265613c855892e58b
7
+ data.tar.gz: d852deaa4743bcf8783c5a4da1a3f752c6e9897926005acae1873107b22607edd68c1065fc1619eca12e40fea0e5ec621cdc72a71deb3c97ae82d5894a595528
data/README.md CHANGED
@@ -73,10 +73,8 @@ end
73
73
  In that case you can register an instance instead of a type:
74
74
  ```ruby
75
75
  connection = DB.connect
76
- container.register(connection, :connection)
76
+ container.register_instance(connection, :connection)
77
77
  ```
78
- You must specify component name as it's done in example above.
79
-
80
78
  ## Component Life Cycle
81
79
  By default all registered components have life cycle Hypo::Transient (:transient).
82
80
  It means, every time when you resolve a component Hypo returns new instance of its type.
@@ -89,6 +87,44 @@ container.register(User).using_life_cycle(:singleton)
89
87
  Actually you can implement your own life cycle,
90
88
  i.e. makes sense to think about HttpRequest strategy for your web applications.
91
89
 
90
+ **Instances support only :singleton life cycle.**
91
+
92
+ Sometimes you need to manage a component life cycle manually. Especially it can be useful for "instances".
93
+ For example, you're going to inject new instance of request parameters every http request in your web application:
94
+
95
+ ```ruby
96
+ # somewhere in Rack application
97
+ # ...
98
+ query_string = env['QUERY_STRING']
99
+ container.register_instance(query_string, :query_string)
100
+
101
+ # handle the request
102
+
103
+ container.remove(:query_string)
104
+ # ...
105
+ ```
106
+
107
+ Hypo resolves components with different life cycle strategies independently.
108
+ In other words you can inject a dependency with less lifespan than acceptor type. I.e.:
109
+
110
+ ```ruby
111
+ class A; end
112
+
113
+ class B
114
+ def initialize(type_a)
115
+ @type_a = type_a
116
+ end
117
+ end
118
+
119
+
120
+ container.register(A, :type_a).using_life_cycle(:transient)
121
+ container.register(B, :type_b).using_life_cycle(:singleton)
122
+
123
+ ```
124
+
125
+ According to :transient strategy every time when you try to resolve a singleton
126
+ you retrieve exactly the same instance of the singleton **but with new instance** of transient dependency.
127
+
92
128
  ## Development
93
129
 
94
130
  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.
@@ -13,12 +13,16 @@ module Hypo
13
13
  @life_cycles = {}
14
14
  add_life_cycle(LifeCycle::Transient.new, :transient)
15
15
  add_life_cycle(LifeCycle::Singleton.new, :singleton)
16
- register self, :container
16
+ register_instance self, :container
17
17
  end
18
18
 
19
19
  def register(item, name = nil)
20
- type = item.is_a?(Class) ? Component : Instance
21
- component = type.new(item, self, name)
20
+ unless item.is_a?(Class)
21
+ raise ContainerError, 'Using method "register" you can register only a type. ' \
22
+ 'If you wanna register an instance please use method "register_instance".'
23
+ end
24
+
25
+ component = Component.new(item, self, name)
22
26
 
23
27
  if @components.key?(component.name)
24
28
  raise ContainerError, "Component \"#{component.name}\" has already been registered"
@@ -27,6 +31,20 @@ module Hypo
27
31
  @components[component.name] = component
28
32
  end
29
33
 
34
+ def register_instance(item, name)
35
+ if item.is_a?(Class)
36
+ raise ContainerError, 'Using method "register_instance" you can register only an instance. ' \
37
+ 'If you wanna register a type please use method "register".'
38
+ end
39
+
40
+ if @components.key?(name)
41
+ raise ContainerError, "Component \"#{name}\" has already been registered"
42
+ end
43
+
44
+ instance = Instance.new(item, self, name)
45
+ @components[name] = instance
46
+ end
47
+
30
48
  def resolve(name)
31
49
  unless @components.key?(name)
32
50
  raise ContainerError, "Component with name \"#{name}\" is not registered"
data/lib/hypo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hypo
2
- VERSION = '0.6.2'
2
+ VERSION = '0.7.0'
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.6.2
4
+ version: 0.7.0
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-17 00:00:00.000000000 Z
11
+ date: 2017-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler