diftw 1.0.0.pre.rc2 → 1.0.0.pre.rc3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -14
- data/lib/diftw/builder.rb +6 -11
- data/lib/diftw/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fa3e5cdd05bb21570dce716695928161bb60de29
|
4
|
+
data.tar.gz: c43c0ce2a92cc85dd6163970ed31ed915101efc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 161f0b4c19fb676274d1bfc3b45e5d3d1b6479ae8e8778d2db18cbef3353b1212d30773287d15a01938ace911095c5e68c819088ea1d72035d05c39e9f67b14d
|
7
|
+
data.tar.gz: 2cbb70d0f199d131cbea1650f9f62a936fc6c51f6ec4de9b75b4ca3aa86e90c979f7ae2e8bbfb61c795ae6696b1ab68ae9a69bd78980fa8e9fab0ce5d706c7b7
|
data/README.md
CHANGED
@@ -9,9 +9,9 @@ If your only concern is testing, mocks/stubs and `webmock` might be all you need
|
|
9
9
|
### Features
|
10
10
|
|
11
11
|
* DI container w/dead-simple registration
|
12
|
+
* Inject singletons or factories
|
12
13
|
* Lazy injection (by default)
|
13
14
|
* Inject into each of a class's instances, a single instance, a class itself, or a module
|
14
|
-
* Inject singletons or factories
|
15
15
|
* Uses parent-child injectors for max flexibility
|
16
16
|
* Threadsafe, after registration
|
17
17
|
|
@@ -89,19 +89,6 @@ Lazy injection is usually fine. But if it isn't, use `inject!`:
|
|
89
89
|
puts SomeModule.baz.message
|
90
90
|
=> 'Baz'
|
91
91
|
|
92
|
-
## Optionally injects singletons
|
93
|
-
|
94
|
-
DI = DiFtw::Injector.new do
|
95
|
-
singleton :bar do
|
96
|
-
OpenStruct.new(message: 'Bar')
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
Widget.new.bar.object_id == Widget.new.bar.object_id
|
101
|
-
=> true
|
102
|
-
|
103
|
-
Accessing injected singletons **is thread safe**. However, registering them is not.
|
104
|
-
|
105
92
|
## Parent-Child injectors
|
106
93
|
|
107
94
|
This is **maybe the coolest part**. Each time you call `inject` (or `inject_instance`) you're creating a fresh, empty child `DiFtw::Injector`. It will recursively look up dependencies through the parent chain until it finds the nearest registration of that dependency.
|
data/lib/diftw/builder.rb
CHANGED
@@ -35,22 +35,17 @@ module DiFtw
|
|
35
35
|
end
|
36
36
|
nil
|
37
37
|
end
|
38
|
-
|
39
|
-
# Define instance accessor methods
|
40
|
-
di_mod._diftw_dependencies.each do |dep|
|
41
|
-
define_method dep do
|
42
|
-
var = "@_diftw_#{dep}"
|
43
|
-
instance_variable_get(var) || instance_variable_set(var, self.injector[dep])
|
44
|
-
end
|
45
|
-
end
|
46
38
|
end
|
39
|
+
|
40
|
+
# Define instance accessor methods. Do as a string so we get/set instance variables directly.
|
41
|
+
base.class_eval _diftw_dependencies.map { |dep|
|
42
|
+
"def #{dep}; @_diftw_#{dep} ||= self.injector[:#{dep}]; end"
|
43
|
+
}.join $/
|
47
44
|
end
|
48
45
|
|
49
46
|
def self.extended(base)
|
50
47
|
di_mod = self
|
51
|
-
base.singleton_class.class_eval
|
52
|
-
include di_mod
|
53
|
-
end
|
48
|
+
base.singleton_class.class_eval { include di_mod }
|
54
49
|
end
|
55
50
|
}.tap { |mod|
|
56
51
|
mod.injector = Injector.new(parent: parent_injector)
|
data/lib/diftw/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: diftw
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.0.pre.
|
4
|
+
version: 1.0.0.pre.rc3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jordan Hollinger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-09-
|
11
|
+
date: 2017-09-15 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: A small dependency injection library for Ruby
|
14
14
|
email: jordan.hollinger@gmail.com
|