habu 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +9 -0
- data/example/app.rb +9 -0
- data/lib/habu.rb +2 -1
- data/lib/habu/container.rb +5 -3
- data/lib/habu/factory.rb +32 -0
- data/lib/habu/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 140342d6e3b4549bca50f71b5880e0d120ee25ea21f009d0709e0f46ade882ba
|
4
|
+
data.tar.gz: ba83dbd95e4ebf2b7aab1818f0ca9d9183173270c8cfde57ef5d137068d8d507
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a5327130a16a552a4eac9139477434411bafa1918715122f4cf8d281c5ccf0851ab88a50593d32dfee9b6327ea15fc93b591832b10a56816141875cad90d82db
|
7
|
+
data.tar.gz: 94103715d80651bc9058531d88d219af3fa3394fea6caeeacc9a9ddf761403b1ff41df6da05afab9b1e9d5da50a17b33f27648a17a24b462b8796f29f37a0cab
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -55,6 +55,15 @@ container = Habu::Container.new
|
|
55
55
|
# Register user_repository service by passing the block as service factory
|
56
56
|
container[:user_repository] { User }
|
57
57
|
|
58
|
+
# You can access to registered service by calling the method of Container#factory
|
59
|
+
container.factory.user_repository.new("hanachin")
|
60
|
+
# => #<struct User name="hanachin">
|
61
|
+
|
62
|
+
# Also you can use Container#factory as refinements for shorthand
|
63
|
+
using container.factory.to_refinements
|
64
|
+
user_repository.new("hanachin")
|
65
|
+
# => #<struct User name="hanachin">
|
66
|
+
|
58
67
|
# Call Habu::Container#new to get instance
|
59
68
|
new_user = container.new(NewUserService).call("hanachin")
|
60
69
|
# => #<struct User name="hanachin">
|
data/example/app.rb
CHANGED
@@ -8,6 +8,15 @@ container = Habu::Container.new
|
|
8
8
|
# Register user_repository service by passing the block as service factory
|
9
9
|
container[:user_repository] { User }
|
10
10
|
|
11
|
+
# You can access to registered service by calling the method of Container#factory
|
12
|
+
container.factory.user_repository.new("hanachin")
|
13
|
+
# => #<struct User name="hanachin">
|
14
|
+
|
15
|
+
# Also you can use Container#factory as refinements for shorthand
|
16
|
+
using container.factory.to_refinements
|
17
|
+
user_repository.new("hanachin")
|
18
|
+
# => #<struct User name="hanachin">
|
19
|
+
|
11
20
|
# Call Habu::Container#new to get instance
|
12
21
|
new_user = container.new(NewUserService).call("hanachin")
|
13
22
|
# => #<struct User name="hanachin">
|
data/lib/habu.rb
CHANGED
data/lib/habu/container.rb
CHANGED
@@ -1,14 +1,16 @@
|
|
1
1
|
module Habu
|
2
2
|
class Container
|
3
|
+
attr_reader :factory
|
4
|
+
|
3
5
|
def initialize
|
4
|
-
@
|
6
|
+
@factory = Factory.new(self)
|
5
7
|
end
|
6
8
|
|
7
9
|
def [](key, &block)
|
8
10
|
if block
|
9
|
-
@
|
11
|
+
@factory.register(key, &block)
|
10
12
|
else
|
11
|
-
@
|
13
|
+
@factory.resolve(key)
|
12
14
|
end
|
13
15
|
end
|
14
16
|
|
data/lib/habu/factory.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Habu
|
2
|
+
class Factory
|
3
|
+
def initialize(container)
|
4
|
+
@container = container
|
5
|
+
@registered_service_hash = {}
|
6
|
+
end
|
7
|
+
|
8
|
+
def register(service, &block)
|
9
|
+
@registered_service_hash[service] = block
|
10
|
+
define_singleton_method(service) do
|
11
|
+
block.call(@container)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def resolve(service)
|
16
|
+
@registered_service_hash.fetch(service).call(@container)
|
17
|
+
end
|
18
|
+
|
19
|
+
def to_refinements
|
20
|
+
factory_methods = singleton_methods.map(&self.:singleton_method)
|
21
|
+
refinements = Module.new
|
22
|
+
refinements.instance_exec do |container|
|
23
|
+
refine(Object) do
|
24
|
+
factory_methods.each do |m|
|
25
|
+
define_method(m.name, &m)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
refinements
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/habu/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: habu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Seiei Miyagi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -76,6 +76,7 @@ files:
|
|
76
76
|
- lib/habu/annotation_collector.rb
|
77
77
|
- lib/habu/annotation_collector_helper.rb
|
78
78
|
- lib/habu/container.rb
|
79
|
+
- lib/habu/factory.rb
|
79
80
|
- lib/habu/setup.rb
|
80
81
|
- lib/habu/version.rb
|
81
82
|
homepage: https://github.com/hanachin/habu
|