carb-container 0.2.0 → 0.3.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
  SHA1:
3
- metadata.gz: 4b2266086979fc001d37b802f0737e46a0101fa7
4
- data.tar.gz: 85fe087f9b35d32ee015b897f3eeba8f6a670a3e
3
+ metadata.gz: 14c4ecb86c8b6ad0344f83c69a88197abc40fd28
4
+ data.tar.gz: 98914be8b62f72dbed25e62fe0df868994694b02
5
5
  SHA512:
6
- metadata.gz: 3fe8e15f66cae9431cec660c5cbd082e13c5394d5a6c619dc99ccff405ff702ef206acc6f7e94770e0b10ab3e34e4c866f786b45c53f2a7acdf4421cd729f489
7
- data.tar.gz: 0353b4d1bb150e98b3a8815f62e1585b7cd0d41523ace337c6778f0764255d95c3f20dbbe6a66d02e5ccb22605af1adeec1278c66efd000b80a4df8b2135eff7
6
+ metadata.gz: 9407ea0ee8bc049976ba4b782e59799b4a9d93885a1c308c7778091100aa8a06683ce7d2b6238d03deebff40f6ea88d963636d741768caa576e8fe80350a5737
7
+ data.tar.gz: 849aab3233cc33197af4a87fdc2c547d186384c9537297c366efd914a6820368a294e3d438eac7c35406bd0fd78b677fedb3934fef55e38911e63d5e22f3b034
data/README.md CHANGED
@@ -3,6 +3,50 @@
3
3
  A set of simple container objects to store dependencies. Can be used in
4
4
  conjuction with [carb-inject](https://github.com/Carburetor/carb-inject) as
5
5
  an IoC container.
6
+ Check the following example usage:
7
+
8
+ ```ruby
9
+ # Optional, used to auto convert class names to symbol
10
+ require "active_support/inflector/methods"
11
+
12
+ Container = Carb::Container::RegistryContainer.new
13
+ Carb::Container::RegistrationGlue.call(Container)
14
+
15
+ class Foo
16
+ carb_container as: :myfoo
17
+
18
+ def hello
19
+ "foo"
20
+ end
21
+ end
22
+
23
+ class Bar
24
+ carb_container
25
+
26
+ def hello
27
+ "bar"
28
+ end
29
+ end
30
+
31
+ class Baz
32
+ def hello
33
+ "baz"
34
+ end
35
+ end
36
+
37
+ Container.register(:special_baz, -> { Baz.new })
38
+
39
+ # Now let's use the container to fetch various dependencies
40
+ foo_class = Container[:myfoo]
41
+ bar_class = Container[:bar]
42
+ foo = foo_class.new
43
+ bar = bar_class.new
44
+ baz = Container[:special_baz]
45
+
46
+ foo.hello # => "foo"
47
+ bar.hello # => "bar"
48
+ baz.hello # => "baz"
49
+ ```
6
50
 
7
51
  ## Installation
8
52
 
@@ -61,9 +105,10 @@ container[:my_class].new.hello
61
105
 
62
106
  ### Carb::Container::RegistrationGlue
63
107
 
64
- A utility class that allows you to create a class method inside `Module` so
65
- that you can easily register any newly created class. This is entirely
66
- optional, for people who don't like monkey patching, can be skipped.
108
+ A utility class that makes the method `carb_container` available as class
109
+ class method in all classes so, that you can easily register any newly
110
+ created class. This is entirely optional, for people who don't like monkey
111
+ patching, can be skipped.
67
112
 
68
113
  ```ruby
69
114
  MyContainer = Carb::Container::RegistryContainer.new
@@ -94,6 +139,27 @@ person.greet
94
139
  person.bark
95
140
  ```
96
141
 
142
+ #### carb_container
143
+
144
+ This method accepts two options:
145
+
146
+ - `as:` which accepts a `Symbol` and is the name which will be used to
147
+ register the class. If not supplied and `activesupport` is present, it will
148
+ be inferred by class name in snake_case
149
+ - `init: false` by default the value is the same as the `init`
150
+ supplied to `RegistrationGlue` when invoked (which defaults to `false`).
151
+ When false, it will register the class itself as is, equivalent to calling
152
+ `mycontainer.register(:foo, -> { Foo })`, however if set to `true` it
153
+ store the dependency with a call to `new`, like
154
+ `mycontainer.register(:foo, -> { Foo.new })`, the class must have a
155
+ constructor without arguments
156
+
157
+ `RegistrationGlue` also accepts an option when called:
158
+
159
+ - `init: false` which can be set to `true` so that `carb_container` will
160
+ automatically supply `init: true` instead (check `carb_container` for
161
+ more details)
162
+
97
163
  ### Carb::Container::DelegateContainer
98
164
 
99
165
  Special container which allows chaining containers together so that a
@@ -7,26 +7,35 @@ module Carb::Container
7
7
  private
8
8
 
9
9
  attr_reader :container
10
+ attr_reader :init
10
11
  attr_reader :converter
11
12
 
12
13
  # @param container [#register] object which will be used to register
13
14
  # dependencies as {::Proc}
15
+ # @param init [Boolean] defaults to `false`. If `true`, carb_container will
16
+ # register classes as `-> { MyClass.new }` instead of `-> { MyClass }`
14
17
  # @param converter [#call] object which accepts a string and converts it
15
18
  # into a valid method name. It's used to convert class name to a method
16
19
  # name. By default it uses an instance of {ClassNameToMethodName}
17
- def initialize(container, converter: ClassNameToMethodName.new)
20
+ def initialize(
21
+ container,
22
+ init: false,
23
+ converter: ClassNameToMethodName.new
24
+ )
18
25
  @container = container
26
+ @init = init
19
27
  @converter = converter
20
- def_carb_container(container, converter)
28
+ def_carb_container(container, init, converter)
21
29
  end
22
30
 
23
31
  private
24
32
 
25
- def def_carb_container(kontainer, convert)
26
- define_method(:carb_container) do |as: nil|
33
+ def def_carb_container(kontainer, auto_init, convert)
34
+ define_method(:carb_container) do |as: nil, init: auto_init|
27
35
  as ||= convert.call(self.name.to_s)
28
36
  as = as.to_sym
29
- kontainer.register(as, -> { self })
37
+ kontainer.register(as, -> { self.new }) if init
38
+ kontainer.register(as, -> { self }) unless init
30
39
  end
31
40
  send(:protected, :carb_container)
32
41
  end
@@ -10,6 +10,9 @@ module Carb::Container
10
10
  # @param container [#register] a generic object, usually a
11
11
  # {RegistryContainer} which responds to `#register(name, dependency)`,
12
12
  # it's the only required argument
13
+ # @param init [Boolean] auto initialize classes by calling `#new` (without
14
+ # arguments) if true when `carb_container` is called. By default is
15
+ # `false`
13
16
  # @param target [Class, Module] class where you want to add the class
14
17
  # method `carb_container`. Usually it's the {::Module} class so it's
15
18
  # available on all classes
@@ -21,11 +24,16 @@ module Carb::Container
21
24
  # name. By default it uses an instance of {ClassNameToMethodName}
22
25
  def call(
23
26
  container,
27
+ init: false,
24
28
  target: Module,
25
29
  registerer: Registerer,
26
30
  converter: ClassNameToMethodName.new
27
31
  )
28
- registerer_instance = registerer.new(container, converter: converter)
32
+ registerer_instance = registerer.new(
33
+ container,
34
+ init: init,
35
+ converter: converter
36
+ )
29
37
 
30
38
  include_registerer(target, registerer_instance)
31
39
  end
@@ -1,5 +1,5 @@
1
1
  module Carb
2
2
  module Container
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: carb-container
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fire-Dragon-DoL
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-05-27 00:00:00.000000000 Z
11
+ date: 2017-06-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: carb-core