diftw 1.0.0 → 2.0.0.pre.rc1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +10 -8
- data/lib/diftw.rb +1 -0
- data/lib/diftw/factory.rb +1 -1
- data/lib/diftw/injector.rb +8 -13
- data/lib/diftw/provider.rb +23 -0
- data/lib/diftw/singleton.rb +1 -1
- data/lib/diftw/version.rb +1 -1
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 857aa566d69b0bd4d1ef8dc6ecefbc4f3a0b3cd0
|
4
|
+
data.tar.gz: d52f12033f704c5084848d754dc44910eaa6c083
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7c6da96970981bee7c5b3c92ccec642fb28196dbdea5d56dc8b4b587df728582658cc6200c9cc6af60ba13fdafe6bf50c05eeaaca8f2dde39f74b3ef38694904
|
7
|
+
data.tar.gz: 32f4b052c953d33429cdbc68a3e93ad72b9891ee9f04694f0631db5dbbdb7f330d7ff05f204743da15aa5d3d6e653fda1a47976199c1c66e8d81b7ab0d09f78f
|
data/README.md
CHANGED
@@ -26,6 +26,11 @@ If your only concern is testing, mocks/stubs and `webmock` might be all you need
|
|
26
26
|
factory :bar do
|
27
27
|
OpenStruct.new(message: "Everyone will get a NEW version of this object")
|
28
28
|
end
|
29
|
+
|
30
|
+
# inject dependencies into your dependency
|
31
|
+
factory :foobar, [:foo, :bar] do
|
32
|
+
OpenStruct.new(message: foo.message + bar.message)
|
33
|
+
end
|
29
34
|
end
|
30
35
|
|
31
36
|
# Or register things out here
|
@@ -33,9 +38,6 @@ If your only concern is testing, mocks/stubs and `webmock` might be all you need
|
|
33
38
|
OpenStruct.new(message: "Bar")
|
34
39
|
end
|
35
40
|
|
36
|
-
# Or use a Proc
|
37
|
-
DI.factory :xyz, -> { OpenStruct.new(message: "XYZ") }
|
38
|
-
|
39
41
|
## Lazy injection (by default)
|
40
42
|
|
41
43
|
class Widget
|
@@ -97,7 +99,7 @@ This means you can re-register a dependency on a child injector, and *it* will b
|
|
97
99
|
|
98
100
|
# Create your root injector and register :foo
|
99
101
|
DI = DiFtw::Injector.new
|
100
|
-
DI.singleton
|
102
|
+
DI.singleton(:foo) { 'Foo' }
|
101
103
|
|
102
104
|
class Widget
|
103
105
|
include DI.inject :foo
|
@@ -133,7 +135,7 @@ This means you can re-register a dependency on a child injector, and *it* will b
|
|
133
135
|
|
134
136
|
# But we could re-register/override :foo in Spline.injector, and all new
|
135
137
|
# Spline instances would resolve :foo differently.
|
136
|
-
Spline.injector.singleton
|
138
|
+
Spline.injector.singleton(:foo) { 'Bar' }
|
137
139
|
Spline.new.foo
|
138
140
|
=> 'Bar'
|
139
141
|
# But DI and Widget.injector would be unchanged
|
@@ -143,7 +145,7 @@ This means you can re-register a dependency on a child injector, and *it* will b
|
|
143
145
|
# We can go even further and override :foo in just one specific instance of Spline
|
144
146
|
# NOTE This only works if you're using lazy injection (the default) AND if you haven't called #foo yet
|
145
147
|
s = Spline.new
|
146
|
-
s.injector.singleton
|
148
|
+
s.injector.singleton(:foo) { 'Baz' }
|
147
149
|
s.foo
|
148
150
|
=> 'Baz'
|
149
151
|
# Other Spline instances will still get their override from Spline.injector
|
@@ -157,13 +159,13 @@ This means you can re-register a dependency on a child injector, and *it* will b
|
|
157
159
|
|
158
160
|
To inject different dependencies in these environments, you have several options. You can simply re-register dependencies in your root injector:
|
159
161
|
|
160
|
-
DI.singleton
|
162
|
+
DI.singleton(:foo) { OpenStruct.new(message: 'Test Foo') }
|
161
163
|
|
162
164
|
And/Or you can use the parent-child injector features described above to great effect:
|
163
165
|
|
164
166
|
before :each do
|
165
167
|
# Give all MyService instances 'Test foo' as #foo
|
166
|
-
MyService.injector.singleton
|
168
|
+
MyService.injector.singleton(:foo) {
|
167
169
|
'Test foo'
|
168
170
|
}
|
169
171
|
end
|
data/lib/diftw.rb
CHANGED
data/lib/diftw/factory.rb
CHANGED
data/lib/diftw/injector.rb
CHANGED
@@ -12,17 +12,12 @@ module DiFtw
|
|
12
12
|
# Bar.new
|
13
13
|
# end
|
14
14
|
#
|
15
|
-
# # Or you can pass a Proc
|
16
|
-
# DI.singleton :baz, -> { Baz.new }
|
17
|
-
#
|
18
15
|
# Alternatively, you can pass a block to the initializer and register your depencies right inside it:
|
19
16
|
#
|
20
17
|
# DI = DiFtw::Injector.new do
|
21
18
|
# singleton :foo do
|
22
19
|
# Foo.new
|
23
20
|
# end
|
24
|
-
#
|
25
|
-
# singleton :bar, -> { Bar }
|
26
21
|
# end
|
27
22
|
#
|
28
23
|
class Injector
|
@@ -53,14 +48,14 @@ module DiFtw
|
|
53
48
|
# Foo
|
54
49
|
# end
|
55
50
|
#
|
56
|
-
# DI.singleton :bar, -> { Bar }
|
57
|
-
#
|
58
51
|
# @param name [Symbol] name of the dependency
|
52
|
+
# @param deps [Array<Symbol>] Array of dependencies to inject into the provider block
|
59
53
|
# @param y [Proc] the dependency wrapped in a Proc or block
|
60
54
|
# @return [DiFtw::Injector] returns the Injector object
|
61
55
|
#
|
62
|
-
def singleton(name,
|
63
|
-
|
56
|
+
def singleton(name, deps = [], &y)
|
57
|
+
provider = Provider.new(self, name, y, deps)
|
58
|
+
registry[name] = Singleton.new(provider)
|
64
59
|
self
|
65
60
|
end
|
66
61
|
|
@@ -72,14 +67,14 @@ module DiFtw
|
|
72
67
|
# Foo
|
73
68
|
# end
|
74
69
|
#
|
75
|
-
# DI.factory :bar, -> { Bar }
|
76
|
-
#
|
77
70
|
# @param name [Symbol] name of the dependency
|
71
|
+
# @param deps [Array<Symbol>] Array of dependencies to inject into the provider block
|
78
72
|
# @param y [Proc] the dependency wrapped in a Proc or block
|
79
73
|
# @return [DiFtw::Injector] returns the Injector object
|
80
74
|
#
|
81
|
-
def factory(name,
|
82
|
-
|
75
|
+
def factory(name, deps = [], &y)
|
76
|
+
provider = Provider.new(self, name, y, deps)
|
77
|
+
registry[name] = Factory.new(provider)
|
83
78
|
self
|
84
79
|
end
|
85
80
|
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module DiFtw
|
2
|
+
#
|
3
|
+
# Class that provides a dependency via a "call" method. It may optionally depend on other dependencies,
|
4
|
+
# which will be available as methods inside the given Proc.
|
5
|
+
#
|
6
|
+
class Provider
|
7
|
+
#
|
8
|
+
# @param injector [DiFtw::Injection]
|
9
|
+
# @param name [Symbol]
|
10
|
+
# @param y [Proc]
|
11
|
+
# @param deps [Array<Symbol]
|
12
|
+
#
|
13
|
+
def initialize(injector, name, y, deps = [])
|
14
|
+
@name, @y = name, y
|
15
|
+
injector.inject_instance self, *deps if deps.any?
|
16
|
+
end
|
17
|
+
|
18
|
+
# Executes the proc and returns the dependency
|
19
|
+
def call
|
20
|
+
self.instance_exec(&@y)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
data/lib/diftw/singleton.rb
CHANGED
data/lib/diftw/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diftw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0.pre.rc1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- lib/diftw/builder.rb
|
23
23
|
- lib/diftw/factory.rb
|
24
24
|
- lib/diftw/injector.rb
|
25
|
+
- lib/diftw/provider.rb
|
25
26
|
- lib/diftw/singleton.rb
|
26
27
|
- lib/diftw/version.rb
|
27
28
|
homepage: https://github.com/jhollinger/ruby-diftw
|
@@ -39,9 +40,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
39
40
|
version: 2.0.0
|
40
41
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
41
42
|
requirements:
|
42
|
-
- - "
|
43
|
+
- - ">"
|
43
44
|
- !ruby/object:Gem::Version
|
44
|
-
version:
|
45
|
+
version: 1.3.1
|
45
46
|
requirements: []
|
46
47
|
rubyforge_project:
|
47
48
|
rubygems_version: 2.5.2
|