scorpion-ioc 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +53 -17
- data/lib/scorpion/hunting_map.rb +27 -3
- data/lib/scorpion/prey/builder_prey.rb +6 -6
- data/lib/scorpion/version.rb +1 -1
- data/spec/lib/scorpion/hunting_map_spec.rb +8 -0
- data/spec/lib/scorpion/prey/builder_prey_spec.rb +42 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ec2ff4bcf7bacc05263624d567a42ffa57d614c
|
4
|
+
data.tar.gz: 1f52f7c0031504a0b0e926ff507d49af659fa717
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6b6e3b77742c496ff2b3ceee4484bcd8e8eb69d63029e1d232453c34775135c1c0088a144df31528724866c2e84209cd17f2bcb3cf47cbf90b2840315aa99c12
|
7
|
+
data.tar.gz: b55c22cd4c397caf6c9ea10bfde2f892dc083f6eae906c54b7b724103f6b88fb627ee2ff8638667df68f538fbb503b4bd7a5a1a5048062c77ebab0e978e7475b
|
data/README.md
CHANGED
@@ -14,9 +14,16 @@ Add IoC to rails with minimal fuss and ceremony.
|
|
14
14
|
|
15
15
|
- [Dependency Injection](#dependency-injection)
|
16
16
|
- [Why might you _Want_ a DI FRamework?](#why-might-you-_want_-a-di-framework)
|
17
|
+
- [Using a Framework...like Scorpion](#using-a-frameworklike-scorpion)
|
17
18
|
- [Using Scorpion](#using-scorpion)
|
18
19
|
- [Kings](#kings)
|
19
20
|
- [Configuration](#configuration)
|
21
|
+
- [Classes](#classes)
|
22
|
+
- [Modules](#modules)
|
23
|
+
- [Traits](#traits)
|
24
|
+
- [Builders](#builders)
|
25
|
+
- [Hunting Delegates](#hunting-delegates)
|
26
|
+
- [Singletons](#singletons)
|
20
27
|
- [Nests](#nests)
|
21
28
|
- [Rails](#rails)
|
22
29
|
- [Contributing](#contributing)
|
@@ -138,7 +145,7 @@ and setting the weapon. When a Hunter is created it's dependencies are also
|
|
138
145
|
created - and any of their dependencies and so on. Usage is equally simple
|
139
146
|
|
140
147
|
```ruby
|
141
|
-
hunter = scorpion.hunt
|
148
|
+
hunter = scorpion.hunt Hunter
|
142
149
|
hunter.weapon # => a Weapon
|
143
150
|
```
|
144
151
|
|
@@ -151,7 +158,7 @@ scorpion.prepare do
|
|
151
158
|
hunt_for Axe
|
152
159
|
end
|
153
160
|
|
154
|
-
hunter = scorpion.hunt
|
161
|
+
hunter = scorpion.hunt Hunter
|
155
162
|
hunter.weapon # => an Axe
|
156
163
|
```
|
157
164
|
|
@@ -166,7 +173,7 @@ Overriding hunters!
|
|
166
173
|
hunt_for Axe
|
167
174
|
end
|
168
175
|
|
169
|
-
hunter = scorpion.hunt
|
176
|
+
hunter = scorpion.hunt Hunter
|
170
177
|
hunter # => Predator
|
171
178
|
hunter.weapon # => an Axe
|
172
179
|
```
|
@@ -178,7 +185,7 @@ Out of the box Scorpion does not need any configuration and will work
|
|
178
185
|
immediately. You can hunt for any Class even if it hasn't been configured.
|
179
186
|
|
180
187
|
```ruby
|
181
|
-
hash = Scorpion.instance.hunt
|
188
|
+
hash = Scorpion.instance.hunt Hash
|
182
189
|
hash # => {}
|
183
190
|
```
|
184
191
|
|
@@ -206,7 +213,7 @@ class Zoo
|
|
206
213
|
end
|
207
214
|
end
|
208
215
|
|
209
|
-
zoo = scorpion.hunt
|
216
|
+
zoo = scorpion.hunt Zoo
|
210
217
|
zoo.keeper # => an instance of a Zoo::Keeper
|
211
218
|
zoo.vet? # => false it hasn't been hunted down yet
|
212
219
|
zoo.vet # => an instnace of a Zoo::Vet
|
@@ -235,13 +242,13 @@ derived class). In the absence of any configuration, Scorpion will simply create
|
|
235
242
|
an instance of the specific class requested.
|
236
243
|
|
237
244
|
```ruby
|
238
|
-
scorpion.hunt
|
245
|
+
scorpion.hunt Hash # => Hash.new
|
239
246
|
|
240
247
|
scorpion.prepare do
|
241
248
|
hunt_for Object::HashWithIndifferentAccess
|
242
249
|
end
|
243
250
|
|
244
|
-
scorpion.hunt
|
251
|
+
scorpion.hunt Hash # => Object::HashWithIndifferentAccess.new
|
245
252
|
```
|
246
253
|
|
247
254
|
#### Modules
|
@@ -262,14 +269,14 @@ class Sword
|
|
262
269
|
include Sharp
|
263
270
|
end
|
264
271
|
|
265
|
-
poker = scorpion.hunt
|
272
|
+
poker = scorpion.hunt Sharp
|
266
273
|
poker.poke # => "Module"
|
267
274
|
|
268
275
|
scorpion.prepare do
|
269
276
|
hunt_for Sword
|
270
277
|
end
|
271
278
|
|
272
|
-
poker = scorpion.hunt
|
279
|
+
poker = scorpion.hunt Sharp
|
273
280
|
poker.poke # => "Sword"
|
274
281
|
```
|
275
282
|
|
@@ -291,9 +298,9 @@ scorpion.prepare do
|
|
291
298
|
hunt_for Mace, :blunt, :sharp
|
292
299
|
end
|
293
300
|
|
294
|
-
scorpion.hunt
|
295
|
-
scorpion.hunt
|
296
|
-
scorpion.hunt
|
301
|
+
scorpion.hunt Weapon, :blunt # => Hammer.new
|
302
|
+
scorpion.hunt Weapon, :sharp # => Sword.new
|
303
|
+
scorpion.hunt Weapon, :sharp, :blunt # => Mace.new
|
297
304
|
```
|
298
305
|
|
299
306
|
Modules can also be used to identify specific traits desired from the hunted
|
@@ -315,8 +322,8 @@ scorpion.prepare do
|
|
315
322
|
hunt_for SysLog
|
316
323
|
end
|
317
324
|
|
318
|
-
scorpion.hunt
|
319
|
-
scorpion.hunt
|
325
|
+
scorpion.hunt Logger, Color # => Console.new
|
326
|
+
scorpion.hunt Logger, Streaming # => SysLog.new
|
320
327
|
```
|
321
328
|
|
322
329
|
#### Builders
|
@@ -335,6 +342,35 @@ scorpion.prepare do
|
|
335
342
|
end
|
336
343
|
```
|
337
344
|
|
345
|
+
#### Hunting Delegates
|
346
|
+
|
347
|
+
For really complex dependencies you may want to delegate the effort to retrieve
|
348
|
+
the dependencies to another type - a factory module for example. Scorpion
|
349
|
+
allows you to delegate hunting prey using the `:with` option.
|
350
|
+
|
351
|
+
```ruby
|
352
|
+
module ChocolateFactory
|
353
|
+
module_function
|
354
|
+
|
355
|
+
def call( scorpion, *args, &block )
|
356
|
+
case args.first
|
357
|
+
when Nuget then scorpion.spawn Snickers, *args, &block
|
358
|
+
when Butterscotch then scorpion.spawn Butterfinger, *args, &block
|
359
|
+
when Coconut then scorpion.spawn Garbage, *args, &block
|
360
|
+
end
|
361
|
+
end
|
362
|
+
end
|
363
|
+
|
364
|
+
scorpion.prepare do
|
365
|
+
hunt_for Candy, with: ChocolateFactory
|
366
|
+
end
|
367
|
+
|
368
|
+
scorpion.hunt Candy, Nuget.new #=> Snickers.new Nugget.new
|
369
|
+
```
|
370
|
+
|
371
|
+
Any object that responds to `#call( scorpion, *args, &block )` can be used as
|
372
|
+
a hunting delegate.
|
373
|
+
|
338
374
|
#### Singletons
|
339
375
|
|
340
376
|
Scorpion allows you to capture prey and feed the same instance to everyone that
|
@@ -353,8 +389,8 @@ scorpion.prepare do
|
|
353
389
|
capture Logger
|
354
390
|
end
|
355
391
|
|
356
|
-
scorpion.hunt
|
357
|
-
scorpion.hunt
|
392
|
+
scorpion.hunt Logger # => Logger.new
|
393
|
+
scorpion.hunt Logger # => Previously captured logger
|
358
394
|
```
|
359
395
|
|
360
396
|
Captured dependencies are not shared with child scorpions (for example when
|
@@ -376,7 +412,7 @@ nest.prepare do
|
|
376
412
|
end
|
377
413
|
|
378
414
|
scorpion = nest.conceive
|
379
|
-
scorpion.hunt
|
415
|
+
scorpion.hunt Logger # => Logger.new
|
380
416
|
```
|
381
417
|
|
382
418
|
### Rails
|
data/lib/scorpion/hunting_map.rb
CHANGED
@@ -66,7 +66,7 @@ module Scorpion
|
|
66
66
|
# @param [Array<Symbol>] traits found on the {Prey}.
|
67
67
|
# @return [Scorpion::Prey] the prey to be hunted for.
|
68
68
|
def hunt_for( contract, traits = nil, &builder )
|
69
|
-
active_prey_set.unshift
|
69
|
+
active_prey_set.unshift define_prey( contract, traits, &builder )
|
70
70
|
end
|
71
71
|
alias_method :offer, :hunt_for
|
72
72
|
|
@@ -74,7 +74,7 @@ module Scorpion
|
|
74
74
|
# for the resource.
|
75
75
|
# @see #hunt_for
|
76
76
|
def capture( contract, traits = nil, &builder )
|
77
|
-
active_prey_set.unshift Scorpion::Prey::CapturedPrey.new(
|
77
|
+
active_prey_set.unshift Scorpion::Prey::CapturedPrey.new( define_prey( contract, traits, &builder ) )
|
78
78
|
end
|
79
79
|
alias_method :singleton, :capture
|
80
80
|
|
@@ -103,8 +103,32 @@ module Scorpion
|
|
103
103
|
end
|
104
104
|
|
105
105
|
private
|
106
|
+
|
107
|
+
def define_prey( contract, traits, &builder )
|
108
|
+
options, traits = extract_options!( traits )
|
109
|
+
|
110
|
+
if with = options[:with]
|
111
|
+
Scorpion::Prey::BuilderPrey.new( contract, traits, with )
|
112
|
+
elsif block_given?
|
113
|
+
Scorpion::Prey::BuilderPrey.new( contract, traits, builder)
|
114
|
+
else
|
115
|
+
prey_class( contract ).new( contract, traits, &builder )
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
def extract_options!( traits )
|
120
|
+
case traits
|
121
|
+
when Hash then return [ traits, nil ]
|
122
|
+
when Array then
|
123
|
+
if traits.last.is_a? Hash
|
124
|
+
return [ traits.pop, traits ]
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
[ {}, traits]
|
129
|
+
end
|
130
|
+
|
106
131
|
def prey_class( contract, &builder )
|
107
|
-
return Scorpion::Prey::BuilderPrey if block_given?
|
108
132
|
return Scorpion::Prey::HuntedPrey if contract.respond_to? :hunt
|
109
133
|
return Scorpion::Prey::ClassPrey if contract.is_a? Class
|
110
134
|
return Scorpion::Prey::ClassPrey if contract.is_a? Module
|
@@ -2,7 +2,8 @@ require 'scorpion/prey'
|
|
2
2
|
|
3
3
|
module Scorpion
|
4
4
|
class Prey
|
5
|
-
# {Prey}
|
5
|
+
# {Prey} that delegates to another object that implements
|
6
|
+
# #call( scorpion, *args, &block ).
|
6
7
|
class BuilderPrey < Scorpion::Prey
|
7
8
|
|
8
9
|
# ============================================================================
|
@@ -10,16 +11,15 @@ module Scorpion
|
|
10
11
|
#
|
11
12
|
|
12
13
|
# @!attribute
|
13
|
-
# @return [#call(scorpion)] the builder to use to fetch instances of the prey.
|
14
|
+
# @return [#call(scorpion,*args,&block)] the builder to use to fetch instances of the prey.
|
14
15
|
attr_reader :builder
|
15
16
|
|
16
17
|
#
|
17
18
|
# @!endgroup Attributes
|
18
19
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
super
|
20
|
+
def initialize( contract, traits = nil, builder = nil, &block )
|
21
|
+
@builder = block_given? ? block : builder
|
22
|
+
super contract, traits
|
23
23
|
end
|
24
24
|
|
25
25
|
# @see Scorpion::Prey#fetch
|
data/lib/scorpion/version.rb
CHANGED
@@ -88,6 +88,14 @@ describe Scorpion::HuntingMap do
|
|
88
88
|
|
89
89
|
expect( map.first ).to be_a Scorpion::Prey::BuilderPrey
|
90
90
|
end
|
91
|
+
|
92
|
+
it "adds a BuilderPrey for with: option" do
|
93
|
+
map.chart do
|
94
|
+
hunt_for String, with: ->(scorpion,*args,&block){ "YASSS" }
|
95
|
+
end
|
96
|
+
|
97
|
+
expect( map.first ).to be_a Scorpion::Prey::BuilderPrey
|
98
|
+
end
|
91
99
|
end
|
92
100
|
|
93
101
|
describe "#replicate_from" do
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Test
|
4
|
+
module BuilderPrey
|
5
|
+
class ClassDelegate
|
6
|
+
def call( scorpion, *args, &block )
|
7
|
+
Test
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
module ModDelegate
|
12
|
+
module_function
|
13
|
+
|
14
|
+
def call( scorpion, *args, &block )
|
15
|
+
Test
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Scorpion::Prey::BuilderPrey do
|
22
|
+
let( :scorpion ){ double }
|
23
|
+
|
24
|
+
it "supports class hunting delegates" do
|
25
|
+
prey = Scorpion::Prey::BuilderPrey.new( String, nil, Test::BuilderPrey::ClassDelegate.new )
|
26
|
+
expect( prey.fetch( scorpion ) ).to be Test
|
27
|
+
end
|
28
|
+
|
29
|
+
it "supports module hunting delegates" do
|
30
|
+
prey = Scorpion::Prey::BuilderPrey.new( String, nil, Test::BuilderPrey::ModDelegate )
|
31
|
+
expect( prey.fetch( scorpion ) ).to be Test
|
32
|
+
end
|
33
|
+
|
34
|
+
it "supports block hunting delegates" do
|
35
|
+
prey = Scorpion::Prey::BuilderPrey.new( String, nil ) do
|
36
|
+
Test
|
37
|
+
end
|
38
|
+
expect( prey.fetch( scorpion ) ).to be Test
|
39
|
+
end
|
40
|
+
|
41
|
+
|
42
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scorpion-ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Alexander
|
@@ -149,6 +149,7 @@ files:
|
|
149
149
|
- spec/lib/scorpion/hunting_map_spec.rb
|
150
150
|
- spec/lib/scorpion/instance_spec.rb
|
151
151
|
- spec/lib/scorpion/king_spec.rb
|
152
|
+
- spec/lib/scorpion/prey/builder_prey_spec.rb
|
152
153
|
- spec/lib/scorpion/prey/module_prey_spec.rb
|
153
154
|
- spec/lib/scorpion/prey_spec.rb
|
154
155
|
- spec/lib/scorpion/rails/controller_spec.rb
|
@@ -174,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
175
|
version: '0'
|
175
176
|
requirements: []
|
176
177
|
rubyforge_project:
|
177
|
-
rubygems_version: 2.4.
|
178
|
+
rubygems_version: 2.4.5
|
178
179
|
signing_key:
|
179
180
|
specification_version: 4
|
180
181
|
summary: Add IoC to rails with minimal fuss and ceremony
|
@@ -192,6 +193,7 @@ test_files:
|
|
192
193
|
- spec/lib/scorpion/hunting_map_spec.rb
|
193
194
|
- spec/lib/scorpion/instance_spec.rb
|
194
195
|
- spec/lib/scorpion/king_spec.rb
|
196
|
+
- spec/lib/scorpion/prey/builder_prey_spec.rb
|
195
197
|
- spec/lib/scorpion/prey/module_prey_spec.rb
|
196
198
|
- spec/lib/scorpion/prey_spec.rb
|
197
199
|
- spec/lib/scorpion/rails/controller_spec.rb
|