hypo 0.5.3 → 0.6.0

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: 8614f91194399e30161b6760806d78022b77cf08
4
- data.tar.gz: 84ecd96dac8d1d9be247a2ea75474e9495fcec76
3
+ metadata.gz: 2bef8f9459b0ac07871f21fc9c7b9caca58c72ae
4
+ data.tar.gz: 8ed44d8c920f3969e99d8f994e5373b6527d29c0
5
5
  SHA512:
6
- metadata.gz: e591d6ee14c72335d913d968f9dd8a380cedb902a6dd2b791b9631a2cd23c8ccece718b6a0799fb779d6500749d701e0faa7dca080047455db36c67f997b9ae2
7
- data.tar.gz: 12e0efe7253eb3eb2a4d979a99f79136a3564dde9d3814f43b347e877c1e1d82bbb743656e48c8e2785c0ae51b30a1acbcd3985a15279a6ecd80de4a0b64ecd5
6
+ metadata.gz: 5b8a05f3da971b1ea98b36d61f43d79b32059582a65c52727e080d373e4be1c8f45d6c071c017316a3ec77ebb1e0efdcb23f7b66141d741a31ea810a611946dd
7
+ data.tar.gz: 35c3424b2d58b34450ea9b28d9b2253db26e03ddd517d9737a9a61d2198b40642117c70a5b41a824c7a0f52b5047930da47099902629e8b6bd9120252fa80b8a
data/README.md CHANGED
@@ -78,13 +78,13 @@ container.register(connection, :connection)
78
78
  You must specify component name as it's done in example above.
79
79
 
80
80
  ## Component Life Cycle
81
- By default all registered components have life cycle Hypo::Transient.
81
+ By default all registered components have life cycle Hypo::Transient (:transient).
82
82
  It means, every time when you resolve a component Hypo returns new instance of its type.
83
83
  If you wanna change this behavior then you can replace lifetime strategy.
84
- Out of the box Hypo provides Hypo::Singleton strategy, you can use it when register a component:
84
+ Out of the box Hypo provides Hypo::Singleton (:singleton) strategy, you can use it when register a component:
85
85
 
86
86
  ```ruby
87
- container.register(User).using_life_cycle(Hypo::Singleton)
87
+ container.register(User).using_life_cycle(:singleton)
88
88
  ```
89
89
  Actually you can implement your own life cycle,
90
90
  i.e. makes sense to think about HttpRequest strategy for your web applications.
@@ -1,5 +1,6 @@
1
1
  require 'hypo/life_cycle/transient'
2
2
  require 'hypo/chainable'
3
+ require 'hypo/extensions/string'
3
4
 
4
5
  module Hypo
5
6
  class Component
@@ -11,12 +12,12 @@ module Hypo
11
12
  @container = container
12
13
  @type = type
13
14
  @name = name || type.name.gsub(/(.)([A-Z](?=[a-z]))/, '\1_\2').delete('::').downcase.to_sym
14
- @life_cycle = Transient.new(self)
15
+ @life_cycle = container.life_cycles[:transient]
15
16
  @dependency_names = @type.instance_method(:initialize).parameters.map {|p| p[1]}
16
17
  end
17
18
 
18
19
  def instance
19
- instance = @life_cycle.instance
20
+ instance = @life_cycle.instance(self)
20
21
 
21
22
  @dependency_names.each do |dependency|
22
23
  instance.instance_variable_set "@#{dependency}".to_sym, @container.resolve(dependency)
@@ -30,7 +31,7 @@ module Hypo
30
31
  end
31
32
 
32
33
  def use_life_cycle(life_cycle)
33
- @life_cycle = life_cycle.new(self)
34
+ @life_cycle = container.life_cycles[life_cycle]
34
35
 
35
36
  self
36
37
  end
@@ -1,11 +1,17 @@
1
1
  require 'hypo/container_error'
2
2
  require 'hypo/component'
3
3
  require 'hypo/instance'
4
+ require 'hypo/extensions/string'
4
5
 
5
6
  module Hypo
6
7
  class Container
8
+ attr_reader :life_cycles
9
+
7
10
  def initialize
8
11
  @components = {}
12
+ @life_cycles = {}
13
+ add_life_cycle(LifeCycle::Transient, :transient)
14
+ add_life_cycle(LifeCycle::Singleton, :singleton)
9
15
  register self, :container
10
16
  end
11
17
 
@@ -27,5 +33,11 @@ module Hypo
27
33
 
28
34
  @components[name].instance
29
35
  end
36
+
37
+ def add_life_cycle(life_cycle, name)
38
+ @life_cycles[name] = life_cycle.new
39
+
40
+ self
41
+ end
30
42
  end
31
43
  end
@@ -1,12 +1,17 @@
1
1
  module Hypo
2
- class Singleton
3
- def initialize(component)
4
- @component = component
5
- end
2
+ module LifeCycle
3
+ class Singleton
4
+ def initialize
5
+ @instances = {}
6
+ end
7
+
8
+ def instance(component)
9
+ unless @instances.key? component.name
10
+ @instances[component.name] = component.type.new(*component.dependencies)
11
+ end
6
12
 
7
- def instance
8
- @instance = @component.type.new(*@component.dependencies) if @instance.nil?
9
- @instance
13
+ @instances[component.name]
14
+ end
10
15
  end
11
16
  end
12
17
  end
@@ -1,11 +1,9 @@
1
1
  module Hypo
2
- class Transient
3
- def initialize(component)
4
- @component = component
5
- end
6
-
7
- def instance
8
- @component.type.new(*@component.dependencies)
2
+ module LifeCycle
3
+ class Transient
4
+ def instance(component)
5
+ component.type.new(*component.dependencies)
6
+ end
9
7
  end
10
8
  end
11
9
  end
data/lib/hypo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hypo
2
- VERSION = '0.5.3'
2
+ VERSION = '0.6.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.5.3
4
+ version: 0.6.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-16 00:00:00.000000000 Z
11
+ date: 2017-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler