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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ab6fc4542b67b4368a2082e9d9b21928efe7855d0c47d7898c28f35ec4079fa1
4
- data.tar.gz: 3b5cb02a41e93f87f653e86ad027e6f97937b8644a3f1f080f75b51a490c8d2a
3
+ metadata.gz: 140342d6e3b4549bca50f71b5880e0d120ee25ea21f009d0709e0f46ade882ba
4
+ data.tar.gz: ba83dbd95e4ebf2b7aab1818f0ca9d9183173270c8cfde57ef5d137068d8d507
5
5
  SHA512:
6
- metadata.gz: cb7de9a127c3d3f680248c9788445e3a35510c9cdfbe3638a24108fc23f361e25638f85752cb0e8868acf265c1822a4e996776a75a0bcdd32fea7739759f0a23
7
- data.tar.gz: 3f0f8c4e979f0f61a8c3d908805169e20906471fc1b78605f8b5091d4bd7814155fb41c7b62c4e67c0514ccf7b535fec6e9e565045d73d0e570ae215e7455edc
6
+ metadata.gz: a5327130a16a552a4eac9139477434411bafa1918715122f4cf8d281c5ccf0851ab88a50593d32dfee9b6327ea15fc93b591832b10a56816141875cad90d82db
7
+ data.tar.gz: 94103715d80651bc9058531d88d219af3fa3394fea6caeeacc9a9ddf761403b1ff41df6da05afab9b1e9d5da50a17b33f27648a17a24b462b8796f29f37a0cab
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- habu (0.1.1)
4
+ habu (0.2.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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">
@@ -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">
@@ -1,6 +1,7 @@
1
1
  require "habu/version"
2
- require "habu/container"
3
2
  require "habu/annotation_collector"
3
+ require "habu/container"
4
+ require "habu/factory"
4
5
 
5
6
  module Habu
6
7
  class Error < StandardError; end
@@ -1,14 +1,16 @@
1
1
  module Habu
2
2
  class Container
3
+ attr_reader :factory
4
+
3
5
  def initialize
4
- @factories = {}
6
+ @factory = Factory.new(self)
5
7
  end
6
8
 
7
9
  def [](key, &block)
8
10
  if block
9
- @factories[key] = block
11
+ @factory.register(key, &block)
10
12
  else
11
- @factories.fetch(key).call(self)
13
+ @factory.resolve(key)
12
14
  end
13
15
  end
14
16
 
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Habu
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
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.1.1
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-28 00:00:00.000000000 Z
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