hypo 0.4.0 → 0.5.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +14 -1
- data/lib/hypo/component.rb +29 -0
- data/lib/hypo/container.rb +3 -3
- data/lib/hypo/{simple_component.rb → instance.rb} +2 -2
- data/lib/hypo/life_style/singleton.rb +12 -0
- data/lib/hypo/life_style/transient.rb +11 -0
- data/lib/hypo/version.rb +1 -1
- metadata +6 -4
- data/lib/hypo/lazy_component.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 35c57b5bdcc35e6e5360a56828c3a5eebb6d2dea
|
4
|
+
data.tar.gz: d2baae541df45d07295d78ac3eda8425e7f358fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
##
|
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
|
data/lib/hypo/container.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'hypo/container_error'
|
2
|
-
require 'hypo/
|
3
|
-
require 'hypo/
|
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) ?
|
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
|
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
|
data/lib/hypo/version.rb
CHANGED
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
|
+
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-
|
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/
|
76
|
-
- lib/hypo/
|
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:
|
data/lib/hypo/lazy_component.rb
DELETED
@@ -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
|