scorpion-ioc 0.5.13 → 0.5.14
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +43 -26
- data/lib/scorpion/dependency.rb +7 -0
- data/lib/scorpion/hunter.rb +9 -0
- data/lib/scorpion/version.rb +1 -1
- data/spec/lib/scorpion/hunter_spec.rb +6 -0
- 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: 0f144ad2a3f2570fbb1e71ed99d52dd5b1cecc33
|
4
|
+
data.tar.gz: 08ff7b8a52474b70dc790596f5869312cda8846d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ac170a30e129bbdcac5aa5e2f29987a71604bdadd3bda1db63b890b2b86230708d14ccb49c85468eb056232ecba6a899f510395426bc292fb63554d909ffb40
|
7
|
+
data.tar.gz: 699520623d7dbe81f57de933e1a1fa571ee5371b06b63d67953fec8fae1eeb9628dcc412760bffe56564c356d3e6b2f5e0ef296adbda2122d878468ef581182e
|
data/README.md
CHANGED
@@ -140,6 +140,9 @@ class Hunter
|
|
140
140
|
depend_on do
|
141
141
|
weapon Weapon
|
142
142
|
end
|
143
|
+
|
144
|
+
# or
|
145
|
+
attr_dependency :weapon, Weapon
|
143
146
|
end
|
144
147
|
```
|
145
148
|
|
@@ -168,17 +171,17 @@ hunter.weapon # => an Axe
|
|
168
171
|
Overriding hunters!
|
169
172
|
|
170
173
|
```ruby
|
171
|
-
|
172
|
-
|
174
|
+
class Axe < Weapon; end
|
175
|
+
class Predator < Hunter; end
|
173
176
|
|
174
|
-
|
175
|
-
|
176
|
-
|
177
|
-
|
177
|
+
scorpion.prepare do
|
178
|
+
hunt_for Predator
|
179
|
+
hunt_for Axe
|
180
|
+
end
|
178
181
|
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
+
hunter = scorpion.fetch Hunter
|
183
|
+
hunter # => Predator
|
184
|
+
hunter.weapon # => an Axe
|
182
185
|
```
|
183
186
|
|
184
187
|
|
@@ -210,10 +213,14 @@ end
|
|
210
213
|
class Zoo
|
211
214
|
include Scorpion::Object
|
212
215
|
|
213
|
-
depend_on do
|
216
|
+
depend_on do
|
214
217
|
keeper Zoo::Keeper
|
215
218
|
vet Zoo::Vet, lazy: true
|
216
219
|
end
|
220
|
+
|
221
|
+
# or with like attr_accessor
|
222
|
+
attr_dependency :keeper, Zook::Keeper
|
223
|
+
attr_dependency :vet, Zoo::Vet, lazy: true
|
217
224
|
end
|
218
225
|
|
219
226
|
zoo = scorpion.fetch Zoo
|
@@ -285,9 +292,9 @@ poker.poke # => "Sword"
|
|
285
292
|
|
286
293
|
#### Traits
|
287
294
|
|
288
|
-
Traits can be used to distinguish between
|
289
|
-
a scorpion may be
|
290
|
-
blunt weapon.
|
295
|
+
Traits can be used to distinguish between dependencies of the same type. For
|
296
|
+
example a scorpion may be prepared to hunt for several weapons and the object
|
297
|
+
needs a blunt weapon.
|
291
298
|
|
292
299
|
```ruby
|
293
300
|
class Weapon; end
|
@@ -351,13 +358,14 @@ arguments.
|
|
351
358
|
```ruby
|
352
359
|
class City
|
353
360
|
def self.create( scorpion, name )
|
354
|
-
klass =
|
355
|
-
|
356
|
-
|
357
|
-
|
358
|
-
|
361
|
+
klass =
|
362
|
+
if name == "New York"
|
363
|
+
BigCity
|
364
|
+
else
|
365
|
+
SmallCity
|
366
|
+
end
|
359
367
|
|
360
|
-
|
368
|
+
scorpion.new klass, name
|
361
369
|
end
|
362
370
|
|
363
371
|
def initialize( name )
|
@@ -421,9 +429,9 @@ scorpion.fetch Logger # => Logger.new
|
|
421
429
|
scorpion.fetch Logger # => Previously captured logger
|
422
430
|
```
|
423
431
|
|
424
|
-
Captured dependencies are not shared with child scorpions (for example when
|
425
|
-
conceiving scorpions from a [Nest](Nests)). To share captured dependency with
|
426
|
-
use `share`.
|
432
|
+
> Captured dependencies are not shared with child scorpions (for example when
|
433
|
+
> conceiving scorpions from a [Nest](Nests)). To share captured dependency with
|
434
|
+
> children use `share`.
|
427
435
|
|
428
436
|
### Nests
|
429
437
|
|
@@ -447,8 +455,8 @@ scorpion.fetch Logger # => Logger.new
|
|
447
455
|
|
448
456
|
#### ActionController
|
449
457
|
|
450
|
-
Scorpion provides simple integration
|
451
|
-
|
458
|
+
Scorpion provides simple integration for rails controllers to establish a
|
459
|
+
scorpion for each request.
|
452
460
|
|
453
461
|
```ruby
|
454
462
|
# user_service.rb
|
@@ -456,7 +464,7 @@ class UserService
|
|
456
464
|
def find( username ) ... end
|
457
465
|
end
|
458
466
|
|
459
|
-
# config/nest.rb
|
467
|
+
# config/initializers/nest.rb
|
460
468
|
require 'scorpion'
|
461
469
|
|
462
470
|
Scorpion.prepare do
|
@@ -494,6 +502,11 @@ injection into ActiveJob objects.
|
|
494
502
|
|
495
503
|
# avatar_job.rb
|
496
504
|
class AvatarJob < ActiveJob::Base
|
505
|
+
depend_on do
|
506
|
+
users UserService, lazy: true
|
507
|
+
logger Logger
|
508
|
+
end
|
509
|
+
|
497
510
|
def perform( id )
|
498
511
|
user = users.find( id )
|
499
512
|
logger.write "Found a user: #{ user }"
|
@@ -506,6 +519,9 @@ end
|
|
506
519
|
Scorpion enhances ActiveRecord models to support resolving dependencies from
|
507
520
|
a scorpion and sharing that scorpion with all associations.
|
508
521
|
|
522
|
+
> Consider using a SOA framework like [Shamu](https://github.com/phallguy/shamu)
|
523
|
+
> for managing complex resource relationships.
|
524
|
+
|
509
525
|
```ruby
|
510
526
|
class User < ActiveRecord::Base
|
511
527
|
depend_on do
|
@@ -518,7 +534,6 @@ class User < ActiveRecord::Base
|
|
518
534
|
end
|
519
535
|
|
520
536
|
class SessionsController < ActionController::Base
|
521
|
-
|
522
537
|
def create
|
523
538
|
user = User.with_scorpion( scorpion ).find params[:id]
|
524
539
|
user = scorpion( User ).find params[:id]
|
@@ -542,3 +557,5 @@ end
|
|
542
557
|
[The MIT License (MIT)](http://opensource.org/licenses/MIT)
|
543
558
|
|
544
559
|
Copyright (c) 2015 Paul Alexander
|
560
|
+
|
561
|
+
[@phallguy](http://twitter.com/phallguy) / http://phallguy.com
|
data/lib/scorpion/dependency.rb
CHANGED
@@ -64,6 +64,13 @@ module Scorpion
|
|
64
64
|
traits.hash
|
65
65
|
end
|
66
66
|
|
67
|
+
def inspect
|
68
|
+
result = "<#{ contract.inspect }"
|
69
|
+
result << " traits=#{ traits.to_a.inspect }" if traits.present?
|
70
|
+
result << ">"
|
71
|
+
result
|
72
|
+
end
|
73
|
+
|
67
74
|
private
|
68
75
|
|
69
76
|
# @return [Boolean] true if the pray satisfies the given contract.
|
data/lib/scorpion/hunter.rb
CHANGED
@@ -64,5 +64,14 @@ module Scorpion
|
|
64
64
|
dependency_map.reset
|
65
65
|
end
|
66
66
|
|
67
|
+
# @return [String]
|
68
|
+
def inspect
|
69
|
+
dependencies = dependency_map.to_a
|
70
|
+
result = "<#{ self.class.name } contracts=#{ dependencies.inspect }"
|
71
|
+
result << " parent=#{ parent.inspect }" if parent
|
72
|
+
result << ">"
|
73
|
+
result
|
74
|
+
end
|
75
|
+
|
67
76
|
end
|
68
77
|
end
|
data/lib/scorpion/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scorpion-ioc
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.14
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Alexander
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|