hypo 0.4.0 → 0.5.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: 16e2327263264e9d87d5a3bbbcfb69639f9d8a97
4
- data.tar.gz: 2c188346ea7e3ebf43420daff2ce41cdbc10bba7
3
+ metadata.gz: 35c57b5bdcc35e6e5360a56828c3a5eebb6d2dea
4
+ data.tar.gz: d2baae541df45d07295d78ac3eda8425e7f358fd
5
5
  SHA512:
6
- metadata.gz: 32e602357537e0c5936ea8e3df4b2ec7cae3929e9be97566856ee282b6464c9b6958597e23bf29b4713e8276d94f11edcbbe29c729088875bba43cc30cb4bf8c
7
- data.tar.gz: 4f66a7f5368809046dcdd4218798c3ad5fad65612c319511f8926d26ec2dfa0b7eee182ff8d8a584efc5a26b62d3440df325a08da3a9b03913438ca036f88000
6
+ metadata.gz: b663cc5597b98df87b3af69f86a7f44b0f83d72b9d25f66d3d945afdb1350a5241e1db57d1caf5a9982594c4ab267eab45963e9aac4a27713639314a246ea7ef
7
+ data.tar.gz: b8189392738784f65b150ed1062a91b04da6421e8e976612f9b38074007c710d5ec90e307928404ae41a4e06fc1ebde615e40e7482fc43a88bd590eb4f8e4d69
data/README.md CHANGED
@@ -17,7 +17,8 @@ Or install it yourself as:
17
17
 
18
18
  $ gem install hypo
19
19
 
20
- ## Usage
20
+ ## Getting Started
21
+
21
22
  First of all you need to create an instance of Hypo::Container.
22
23
  ```ruby
23
24
  container = Hypo::Container.new
@@ -52,6 +53,18 @@ and if you registered both of them, you can do:
52
53
  # user.company is resolved as well
53
54
  ```
54
55
 
56
+ ## Component Lifetime
57
+ By default all registered components have lifestyle Hypo::Transient.
58
+ It means, every time when you resolve a component Hypo returns new instance of its type.
59
+ If you wanna change this behavior then you can replace lifetime strategy.
60
+ Out of the box Hypo provides Hypo::Singleton strategy, you can use it when register a component:
61
+
62
+ ```ruby
63
+ container.register(User).using_lifestyle(Hypo::Singleton)
64
+ ```
65
+ Actually you can implement you own lifestyle,
66
+ i.e. makes sense to think about HttpRequest strategy for your web applications.
67
+
55
68
  ## Development
56
69
 
57
70
  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.
@@ -0,0 +1,29 @@
1
+ require 'hypo/life_style/transient'
2
+
3
+ module Hypo
4
+ class Component
5
+ attr_reader :name, :type, :container, :lifestyle
6
+
7
+ def initialize(type, container, name = nil)
8
+ @container = container
9
+ @lifestyle = Transient.new(self)
10
+
11
+ @type = type
12
+ @name = name || type.name.gsub(/(.)([A-Z](?=[a-z]))/, '\1_\2').delete('::').downcase.to_sym
13
+ end
14
+
15
+ def instance
16
+ @lifestyle.instance
17
+ end
18
+
19
+ def dependencies
20
+ @type.instance_method(:initialize).parameters.map { |p| @container.resolve(p[1]) }
21
+ end
22
+
23
+ def use_lifestyle(lifestyle)
24
+ @lifestyle = lifestyle.new(self)
25
+ end
26
+
27
+ alias using_lifestyle use_lifestyle
28
+ end
29
+ end
@@ -1,6 +1,6 @@
1
1
  require 'hypo/container_error'
2
- require 'hypo/lazy_component'
3
- require 'hypo/simple_component'
2
+ require 'hypo/component'
3
+ require 'hypo/instance'
4
4
 
5
5
  module Hypo
6
6
  class Container
@@ -9,7 +9,7 @@ module Hypo
9
9
  end
10
10
 
11
11
  def register(item, name = nil)
12
- type = item.is_a?(Class) ? LazyComponent : SimpleComponent
12
+ type = item.is_a?(Class) ? Component : Instance
13
13
  component = type.new(item, self, name)
14
14
 
15
15
  if @components.key?(component.name)
@@ -1,13 +1,13 @@
1
1
  module Hypo
2
- class SimpleComponent
2
+ class Instance
3
3
  attr_reader :name
4
4
 
5
5
  def initialize(object, container, name)
6
6
  raise ContainerError, 'Registered object should have a name' if name.nil?
7
7
 
8
8
  @object = object
9
- @name = name
10
9
  @container = container
10
+ @name = name
11
11
  end
12
12
 
13
13
  def instance
@@ -0,0 +1,12 @@
1
+ module Hypo
2
+ class Singleton
3
+ def initialize(component)
4
+ @component = component
5
+ end
6
+
7
+ def instance
8
+ @instance = @component.type.new(*@component.dependencies) if @instance.nil?
9
+ @instance
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
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)
9
+ end
10
+ end
11
+ end
data/lib/hypo/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Hypo
2
- VERSION = '0.4.0'
2
+ VERSION = '0.5.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.4.0
4
+ version: 0.5.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-02 00:00:00.000000000 Z
11
+ date: 2017-09-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -70,10 +70,12 @@ files:
70
70
  - bin/setup
71
71
  - hypo.gemspec
72
72
  - lib/hypo.rb
73
+ - lib/hypo/component.rb
73
74
  - lib/hypo/container.rb
74
75
  - lib/hypo/container_error.rb
75
- - lib/hypo/lazy_component.rb
76
- - lib/hypo/simple_component.rb
76
+ - lib/hypo/instance.rb
77
+ - lib/hypo/life_style/singleton.rb
78
+ - lib/hypo/life_style/transient.rb
77
79
  - lib/hypo/version.rb
78
80
  homepage: https://github.com/cylon-v/hypo
79
81
  licenses:
@@ -1,18 +0,0 @@
1
- module Hypo
2
- class LazyComponent
3
- attr_reader :name, :type
4
-
5
- def initialize(type, container, name = nil)
6
- @type = type
7
- @name = name || type.name.gsub(/(.)([A-Z](?=[a-z]))/,'\1_\2').delete('::').downcase.to_sym
8
- @container = container
9
- end
10
-
11
- def instance
12
- dependencies = @type.instance_method(:initialize).parameters.map{|p| @container.resolve(p[1])}
13
- @instance = @type.new(*dependencies) if @instance.nil?
14
-
15
- @instance
16
- end
17
- end
18
- end