scorpion-ioc 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.rspec +2 -1
- data/README.md +111 -44
- data/lib/scorpion/attribute.rb +0 -1
- data/lib/scorpion/attribute_set.rb +15 -7
- data/lib/scorpion/dependency/argument_dependency.rb +25 -0
- data/lib/scorpion/{prey/builder_prey.rb → dependency/builder_dependency.rb} +8 -8
- data/lib/scorpion/dependency/captured_dependency.rb +44 -0
- data/lib/scorpion/dependency/class_dependency.rb +25 -0
- data/lib/scorpion/dependency/module_dependency.rb +14 -0
- data/lib/scorpion/dependency.rb +137 -0
- data/lib/scorpion/dependency_map.rb +135 -0
- data/lib/scorpion/hunt.rb +158 -0
- data/lib/scorpion/hunter.rb +21 -20
- data/lib/scorpion/locale/en.yml +5 -1
- data/lib/scorpion/{king.rb → object.rb} +72 -53
- data/lib/scorpion/object_constructor.rb +55 -0
- data/lib/scorpion/rails/active_record/association.rb +65 -0
- data/lib/scorpion/rails/active_record/model.rb +28 -0
- data/lib/scorpion/rails/active_record/relation.rb +66 -0
- data/lib/scorpion/rails/active_record.rb +21 -0
- data/lib/scorpion/rails/controller.rb +22 -62
- data/lib/scorpion/rails/job.rb +30 -0
- data/lib/scorpion/rails/nest.rb +86 -0
- data/lib/scorpion/rails/railtie.rb +16 -0
- data/lib/scorpion/rails.rb +4 -0
- data/lib/scorpion/rspec/helper.rb +25 -0
- data/lib/scorpion/rspec.rb +17 -0
- data/lib/scorpion/stinger.rb +69 -0
- data/lib/scorpion/version.rb +1 -1
- data/lib/scorpion.rb +91 -44
- data/scorpion.gemspec +1 -1
- data/spec/internal/app/models/author.rb +17 -0
- data/spec/internal/app/models/todo.rb +14 -0
- data/spec/internal/db/schema.rb +12 -1
- data/spec/lib/scorpion/dependency/argument_dependency_spec.rb +18 -0
- data/spec/lib/scorpion/dependency/builder_dependency_spec.rb +41 -0
- data/spec/lib/scorpion/dependency/module_dependency_spec.rb +16 -0
- data/spec/lib/scorpion/dependency_map_spec.rb +108 -0
- data/spec/lib/scorpion/dependency_spec.rb +131 -0
- data/spec/lib/scorpion/hunt_spec.rb +93 -0
- data/spec/lib/scorpion/hunter_spec.rb +53 -14
- data/spec/lib/scorpion/object_constructor_spec.rb +49 -0
- data/spec/lib/scorpion/object_spec.rb +214 -0
- data/spec/lib/scorpion/rails/active_record/association_spec.rb +26 -0
- data/spec/lib/scorpion/rails/active_record/model_spec.rb +33 -0
- data/spec/lib/scorpion/rails/active_record/relation_spec.rb +72 -0
- data/spec/lib/scorpion/rails/controller_spec.rb +9 -9
- data/spec/lib/scorpion/rails/job_spec.rb +34 -0
- data/spec/lib/scorpion/rspec/helper_spec.rb +44 -0
- data/spec/lib/scorpion_spec.rb +0 -35
- data/spec/spec_helper.rb +1 -0
- metadata +54 -26
- data/lib/scorpion/hunting_map.rb +0 -139
- data/lib/scorpion/prey/captured_prey.rb +0 -44
- data/lib/scorpion/prey/class_prey.rb +0 -13
- data/lib/scorpion/prey/hunted_prey.rb +0 -14
- data/lib/scorpion/prey/module_prey.rb +0 -14
- data/lib/scorpion/prey.rb +0 -94
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/lib/scorpion/hunting_map_spec.rb +0 -126
- data/spec/lib/scorpion/instance_spec.rb +0 -5
- data/spec/lib/scorpion/king_spec.rb +0 -198
- data/spec/lib/scorpion/prey/builder_prey_spec.rb +0 -42
- data/spec/lib/scorpion/prey/module_prey_spec.rb +0 -16
- data/spec/lib/scorpion/prey_spec.rb +0 -76
@@ -0,0 +1,93 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
|
4
|
+
module Test
|
5
|
+
module Hunt
|
6
|
+
class Logger; end
|
7
|
+
|
8
|
+
class Target
|
9
|
+
include ::Scorpion::Object
|
10
|
+
|
11
|
+
depend_on do
|
12
|
+
logger Logger, public: true
|
13
|
+
sailor Logger, public: true, lazy: true
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe Scorpion::Hunt do
|
21
|
+
|
22
|
+
let( :scorpion ){ double Scorpion }
|
23
|
+
let( :hunt ){ Scorpion::Hunt.new scorpion, String, nil }
|
24
|
+
|
25
|
+
describe "#fetch_by_traits" do
|
26
|
+
it "changes context" do
|
27
|
+
expect( scorpion ).to receive( :execute ) do |hunt|
|
28
|
+
expect( hunt.contract ).to eq Regexp
|
29
|
+
end
|
30
|
+
|
31
|
+
hunt.fetch_by_traits Regexp, nil
|
32
|
+
end
|
33
|
+
|
34
|
+
it "restores context" do
|
35
|
+
expect( scorpion ).to receive( :execute )
|
36
|
+
|
37
|
+
hunt.fetch_by_traits Numeric, nil
|
38
|
+
expect( hunt.contract ).to eq String
|
39
|
+
end
|
40
|
+
|
41
|
+
it "finds matching argument in parent" do
|
42
|
+
hunt.arguments << "Hello"
|
43
|
+
|
44
|
+
expect( hunt.fetch String ).to eq "Hello"
|
45
|
+
end
|
46
|
+
|
47
|
+
it "finds matching argument in grandparent" do
|
48
|
+
hunt = Scorpion::Hunt.new scorpion, String, nil, "Hello"
|
49
|
+
hunt.send :push, Regexp, nil, [], nil
|
50
|
+
|
51
|
+
expect( scorpion ).to receive( :execute ) do |hunt|
|
52
|
+
next if hunt.contract == String
|
53
|
+
|
54
|
+
expect( hunt.fetch String ).to eq "Hello"
|
55
|
+
end.at_least(:once)
|
56
|
+
|
57
|
+
hunt.fetch Numeric
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#inject" do
|
62
|
+
let( :target ) do
|
63
|
+
Test::Hunt::Target.new
|
64
|
+
end
|
65
|
+
|
66
|
+
before( :each ) do
|
67
|
+
allow( scorpion ).to receive( :execute ) do |hunt|
|
68
|
+
Test::Hunt::Logger.new if hunt.contract == Test::Hunt::Logger
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
it "injects attributes" do
|
73
|
+
hunt.inject target
|
74
|
+
|
75
|
+
expect( target.logger? ).to be_truthy
|
76
|
+
expect( target.logger ).to be_a Test::Hunt::Logger
|
77
|
+
end
|
78
|
+
|
79
|
+
it "does not overwrite existing attributes" do
|
80
|
+
logger = Test::Hunt::Logger.new
|
81
|
+
target.logger = logger
|
82
|
+
hunt.inject target
|
83
|
+
|
84
|
+
expect( target.logger ).to be logger
|
85
|
+
end
|
86
|
+
|
87
|
+
it "does not fetch lazy attributes" do
|
88
|
+
hunt.inject target
|
89
|
+
expect( target.sailor? ).to be_falsy
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
@@ -15,12 +15,35 @@ module Test
|
|
15
15
|
class Implicit; end
|
16
16
|
|
17
17
|
class Zoo
|
18
|
-
include Scorpion::
|
18
|
+
include Scorpion::Object
|
19
19
|
|
20
|
-
|
20
|
+
depend_on do
|
21
21
|
bear Bear
|
22
22
|
end
|
23
23
|
end
|
24
|
+
|
25
|
+
class City; end
|
26
|
+
|
27
|
+
class Park
|
28
|
+
include Scorpion::Object
|
29
|
+
|
30
|
+
depend_on do
|
31
|
+
city City
|
32
|
+
end
|
33
|
+
|
34
|
+
initialize( zoo: Test::Hunter::Zoo )
|
35
|
+
end
|
36
|
+
|
37
|
+
class City
|
38
|
+
include Scorpion::Object
|
39
|
+
|
40
|
+
depend_on do
|
41
|
+
park Park
|
42
|
+
end
|
43
|
+
|
44
|
+
initialize( zoo: Test::Hunter::Zoo )
|
45
|
+
end
|
46
|
+
|
24
47
|
end
|
25
48
|
end
|
26
49
|
|
@@ -39,36 +62,52 @@ describe Scorpion::Hunter do
|
|
39
62
|
end
|
40
63
|
end
|
41
64
|
|
42
|
-
it "spawns
|
43
|
-
expect( hunter.
|
65
|
+
it "spawns dependency" do
|
66
|
+
expect( hunter.fetch Test::Hunter::Beast ).to be_a Test::Hunter::Beast
|
44
67
|
end
|
45
68
|
|
46
69
|
it "spawns a new instance for multiple requests" do
|
47
|
-
first = hunter.
|
48
|
-
expect( hunter.
|
70
|
+
first = hunter.fetch Test::Hunter::Beast
|
71
|
+
expect( hunter.fetch Test::Hunter::Beast ).not_to be first
|
49
72
|
end
|
50
73
|
|
51
|
-
it "spawns the same instance for captured
|
52
|
-
first = hunter.
|
53
|
-
expect( hunter.
|
74
|
+
it "spawns the same instance for captured dependency" do
|
75
|
+
first = hunter.fetch_by_traits Test::Hunter::Beast, :tame
|
76
|
+
expect( hunter.fetch_by_traits Test::Hunter::Beast, :tame ).to be first
|
54
77
|
end
|
55
78
|
|
56
|
-
it "injects nested
|
57
|
-
zoo = hunter.
|
79
|
+
it "injects nested objects" do
|
80
|
+
zoo = hunter.fetch Test::Hunter::Zoo
|
58
81
|
expect( zoo.bear ).to be_a Test::Hunter::Bear
|
59
82
|
end
|
60
83
|
|
61
84
|
it "accepts arguments that are passed to constructor" do
|
62
|
-
obj = hunter.
|
85
|
+
obj = hunter.fetch Test::Hunter::Argumented, :awesome
|
63
86
|
expect( obj.arg ).to eq :awesome
|
64
87
|
end
|
65
88
|
|
66
89
|
it "implicitly spawns Class contracts" do
|
67
|
-
expect( hunter.
|
90
|
+
expect( hunter.fetch Test::Hunter::Implicit ).to be_a Test::Hunter::Implicit
|
68
91
|
end
|
69
92
|
|
70
93
|
it "implicitly spawns Class contracts with empty traits" do
|
71
|
-
expect( hunter.
|
94
|
+
expect( hunter.fetch_by_traits Test::Hunter::Implicit, [] ).to be_a Test::Hunter::Implicit
|
95
|
+
end
|
96
|
+
|
97
|
+
context "child dependencies" do
|
98
|
+
it "passes initializer args to child dependencies" do
|
99
|
+
zoo = Test::Hunter::Zoo.new
|
100
|
+
city = hunter.fetch Test::Hunter::City, zoo
|
101
|
+
|
102
|
+
expect( city.park.zoo ).to be zoo
|
103
|
+
end
|
104
|
+
|
105
|
+
it "passes self to child dependencies" do
|
106
|
+
zoo = Test::Hunter::Zoo.new
|
107
|
+
city = hunter.fetch Test::Hunter::City, zoo
|
108
|
+
|
109
|
+
expect( city.park.city ).to be city
|
110
|
+
end
|
72
111
|
end
|
73
112
|
|
74
113
|
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scorpion::ObjectConstructor do
|
4
|
+
it 'defined an initializer' do
|
5
|
+
klass = Class.new do
|
6
|
+
include Scorpion::Object
|
7
|
+
|
8
|
+
initialize logger: String
|
9
|
+
end
|
10
|
+
|
11
|
+
expect( klass.instance_method(:initialize).arity ).to eq 1
|
12
|
+
end
|
13
|
+
|
14
|
+
it "executes initializer code" do
|
15
|
+
expect do |b|
|
16
|
+
klass = Class.new do
|
17
|
+
include Scorpion::Object
|
18
|
+
|
19
|
+
initialize label: String, &b
|
20
|
+
end
|
21
|
+
|
22
|
+
klass.new "home"
|
23
|
+
end.to yield_control
|
24
|
+
end
|
25
|
+
|
26
|
+
it "creates an initializer that accepts a block" do
|
27
|
+
klass = Class.new do
|
28
|
+
include Scorpion::Object
|
29
|
+
|
30
|
+
initialize label: String do |&block|
|
31
|
+
block.call
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
expect do |b|
|
36
|
+
klass.new "apples", &b
|
37
|
+
end.to yield_control
|
38
|
+
end
|
39
|
+
|
40
|
+
it "it defines matching attributes" do
|
41
|
+
klass = Class.new do
|
42
|
+
include Scorpion::Object
|
43
|
+
|
44
|
+
initialize label: String
|
45
|
+
end
|
46
|
+
|
47
|
+
expect( klass.new( "apples" ).label ).to eq "apples"
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Test
|
4
|
+
module Object
|
5
|
+
class UserService; end
|
6
|
+
class Logger; end
|
7
|
+
class BackLogger; end
|
8
|
+
class ColorLogger; end
|
9
|
+
|
10
|
+
class Mamal
|
11
|
+
include Scorpion::Object
|
12
|
+
|
13
|
+
def initialize( family, parent = nil, options={}, &block )
|
14
|
+
@family = family
|
15
|
+
@parent = parent
|
16
|
+
@options = inject_from! options
|
17
|
+
|
18
|
+
|
19
|
+
yield if block_given?
|
20
|
+
end
|
21
|
+
|
22
|
+
depend_on do
|
23
|
+
user_service Test::Object::UserService
|
24
|
+
logger Test::Object::Logger
|
25
|
+
manager Test::Object::Logger, public: true
|
26
|
+
executive_manager Test::Object::Logger, private: true
|
27
|
+
end
|
28
|
+
|
29
|
+
attr_accessor :family
|
30
|
+
attr_accessor :parent
|
31
|
+
attr_accessor :options
|
32
|
+
end
|
33
|
+
|
34
|
+
class Mouse < Mamal
|
35
|
+
|
36
|
+
depend_on do
|
37
|
+
cheese Test::Object::Logger
|
38
|
+
logger Test::Object::BackLogger
|
39
|
+
end
|
40
|
+
def initialize( options = {} )
|
41
|
+
super 'mouse', nil, options
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
class Bear < Mamal
|
46
|
+
depend_on do
|
47
|
+
logger Test::Object::ColorLogger
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe Scorpion::Object do
|
55
|
+
|
56
|
+
let( :scorpion ){ double Scorpion }
|
57
|
+
let( :hunt ) { double Scorpion::Hunt }
|
58
|
+
|
59
|
+
before( :each ) do
|
60
|
+
allow( scorpion ).to receive( :prepare )
|
61
|
+
|
62
|
+
allow( hunt ).to receive( :inject )
|
63
|
+
allow( hunt ).to receive( :scorpion ).and_return scorpion
|
64
|
+
end
|
65
|
+
|
66
|
+
describe ".spawn" do
|
67
|
+
|
68
|
+
it "can spawn" do
|
69
|
+
mamal = Test::Object::Mamal.spawn hunt, 'mouse', 'rodent', name: 'name'
|
70
|
+
expect( mamal ).to be_a Test::Object::Mamal
|
71
|
+
end
|
72
|
+
|
73
|
+
it "calls inject" do
|
74
|
+
expect( hunt ).to receive( :inject )
|
75
|
+
Test::Object::Mouse.spawn hunt
|
76
|
+
end
|
77
|
+
|
78
|
+
it "can inherit" do
|
79
|
+
mouse = Test::Object::Mouse.spawn hunt, name: 'name'
|
80
|
+
expect( mouse.family ).to eq 'mouse'
|
81
|
+
expect( mouse.options ).to include name: 'name'
|
82
|
+
end
|
83
|
+
|
84
|
+
it "yields to constructor" do
|
85
|
+
expect do |b|
|
86
|
+
Test::Object::Mouse.spawn hunt, name: 'name', &b
|
87
|
+
end.to yield_control
|
88
|
+
end
|
89
|
+
|
90
|
+
it "invokes on_injected" do
|
91
|
+
expect_any_instance_of( Test::Object::Mouse ).to receive( :on_injected )
|
92
|
+
Test::Object::Mouse.spawn hunt
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "accessors" do
|
97
|
+
let( :object ) do
|
98
|
+
Test::Object::Mamal.spawn hunt, 'harry', 'jim', name: 'name', manager: double
|
99
|
+
end
|
100
|
+
|
101
|
+
subject{ object }
|
102
|
+
|
103
|
+
it "defines accessors" do
|
104
|
+
expect( object ).to respond_to :user_service
|
105
|
+
expect( object ).not_to respond_to :user_service=
|
106
|
+
end
|
107
|
+
|
108
|
+
it "supports private reader" do
|
109
|
+
expect( object.respond_to? :executive_manager, false ).to be_falsy
|
110
|
+
expect( object.respond_to? :executive_manager, true ).to be_truthy
|
111
|
+
end
|
112
|
+
|
113
|
+
it "supports public writer" do
|
114
|
+
expect( object.respond_to? :manager=, false ).to be_truthy
|
115
|
+
end
|
116
|
+
|
117
|
+
describe "#attr_dependency" do
|
118
|
+
it "defines attributes" do
|
119
|
+
klass = Class.new do
|
120
|
+
include Scorpion::Object
|
121
|
+
|
122
|
+
attr_dependency :logger, Test::Object::Logger
|
123
|
+
end
|
124
|
+
|
125
|
+
expect( klass.new ).to respond_to :logger
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
describe "inheritance" do
|
130
|
+
let( :object ) do
|
131
|
+
Test::Object::Mouse.spawn hunt
|
132
|
+
end
|
133
|
+
|
134
|
+
it "inherits attributes" do
|
135
|
+
expect( object.injected_attributes[:user_service].contract ).to be Test::Object::UserService
|
136
|
+
end
|
137
|
+
|
138
|
+
it "overrides attributes" do
|
139
|
+
expect( object.injected_attributes[:logger].contract ).to be Test::Object::BackLogger
|
140
|
+
end
|
141
|
+
|
142
|
+
it "doesn't effect other classes" do
|
143
|
+
expect( Test::Object::Bear.spawn( hunt, 'Yogi' ).injected_attributes[:logger].contract ).to be Test::Object::ColorLogger
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
end
|
149
|
+
|
150
|
+
describe "feasting" do
|
151
|
+
let( :logger ) { Test::Object::Logger.new }
|
152
|
+
let( :options ) { { manager: logger, color: :red } }
|
153
|
+
let( :object ) { Test::Object::Mouse.new name: 'mighty' }
|
154
|
+
|
155
|
+
|
156
|
+
describe "#inject_from" do
|
157
|
+
it "assigns attributes" do
|
158
|
+
object.send :inject_from, options
|
159
|
+
expect( object.manager ).to be logger
|
160
|
+
end
|
161
|
+
|
162
|
+
it "doesn't overwrite" do
|
163
|
+
object.manager = Test::Object::Logger.new
|
164
|
+
object.send :inject_from, options
|
165
|
+
expect( object.manager ).not_to be logger
|
166
|
+
end
|
167
|
+
|
168
|
+
it "overwrites when asked" do
|
169
|
+
object.manager = Test::Object::Logger.new
|
170
|
+
object.send :inject_from, options, true
|
171
|
+
expect( object.manager ).to be logger
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
describe "inject_from!" do
|
176
|
+
it "assigns attributes" do
|
177
|
+
object.send :inject_from!, options
|
178
|
+
expect( object.manager ).to be logger
|
179
|
+
end
|
180
|
+
|
181
|
+
it "doesn't overwrite" do
|
182
|
+
object.manager = Test::Object::Logger.new
|
183
|
+
object.send :inject_from!, options
|
184
|
+
expect( object.manager ).not_to be logger
|
185
|
+
end
|
186
|
+
|
187
|
+
it "overwrites when asked" do
|
188
|
+
object.manager = Test::Object::Logger.new
|
189
|
+
object.send :inject_from!, options, true
|
190
|
+
expect( object.manager ).to be logger
|
191
|
+
end
|
192
|
+
|
193
|
+
it "removes injected attributes" do
|
194
|
+
expect( options ).to have_key :manager
|
195
|
+
object.send :inject_from!, options
|
196
|
+
expect( options ).not_to have_key :manager
|
197
|
+
end
|
198
|
+
|
199
|
+
it "removes injected attribute even if already set" do
|
200
|
+
expect( options ).to have_key :manager
|
201
|
+
object.manager = Test::Object::Logger.new
|
202
|
+
object.send :inject_from!, options
|
203
|
+
expect( options ).not_to have_key :manager
|
204
|
+
end
|
205
|
+
|
206
|
+
it "doesn't remove other options" do
|
207
|
+
object.send :inject_from!, options
|
208
|
+
expect( options ).to have_key :color
|
209
|
+
end
|
210
|
+
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scorpion::Rails::ActiveRecord::Association, type: :model do
|
4
|
+
include Scorpion::Rspec::Helper
|
5
|
+
|
6
|
+
before( :each ) do
|
7
|
+
author = Author.create! name: "Pitbull"
|
8
|
+
todo = Todo.create! name: "Be even more awesome", author: author
|
9
|
+
end
|
10
|
+
|
11
|
+
it "shares scorpion with associations" do
|
12
|
+
author = Author.with_scorpion( scorpion ).first
|
13
|
+
expect( author.todos.scorpion ).to be scorpion
|
14
|
+
end
|
15
|
+
|
16
|
+
it "shares scorpion with association results" do
|
17
|
+
author = Author.with_scorpion( scorpion ).first
|
18
|
+
expect( author.todos.first.scorpion ).to be scorpion
|
19
|
+
end
|
20
|
+
|
21
|
+
it "shares scorpion with single associations" do
|
22
|
+
todo = Todo.with_scorpion( scorpion ).first
|
23
|
+
expect( todo.author.scorpion ).to be scorpion
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Scorpion::Rails::ActiveRecord::Model, type: :model do
|
4
|
+
include Scorpion::Rspec::Helper
|
5
|
+
|
6
|
+
before( :each ) do
|
7
|
+
author = Author.create! name: "Pitbull"
|
8
|
+
todo = Todo.create! name: "Be even more awesome", author: author
|
9
|
+
end
|
10
|
+
|
11
|
+
it "shares scorpion with associations" do
|
12
|
+
author = Author.with_scorpion( scorpion ).first
|
13
|
+
expect( author.todos.scorpion ).to be scorpion
|
14
|
+
end
|
15
|
+
|
16
|
+
it "shares scorpion with single associations" do
|
17
|
+
todo = Todo.with_scorpion( scorpion ).first
|
18
|
+
expect( todo.author.scorpion ).to be scorpion
|
19
|
+
end
|
20
|
+
|
21
|
+
it "shares scorpion with custom scope" do
|
22
|
+
expect( Author.with_scorpion( scorpion ).named( "Pitbull" ).scorpion ).to be scorpion
|
23
|
+
end
|
24
|
+
|
25
|
+
it "shares scorpion with custom scope results" do
|
26
|
+
expect( Author.with_scorpion( scorpion ).named( "Pitbull" ).first.scorpion ).to be scorpion
|
27
|
+
end
|
28
|
+
|
29
|
+
it "shares scorpion with custom scope extension results" do
|
30
|
+
expect( Author.with_scorpion( scorpion ).named( "Pitbull" ).alphabetical.first.scorpion ).to be scorpion
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,72 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'scorpion/rails'
|
3
|
+
|
4
|
+
describe Scorpion::Rails::ActiveRecord::Relation, type: :model do
|
5
|
+
include Scorpion::Rspec::Helper
|
6
|
+
|
7
|
+
let( :criteria ) { Todo.with_scorpion( scorpion ) }
|
8
|
+
|
9
|
+
it "shares scorpion with fetched records" do
|
10
|
+
Todo.create! name: "Be awesome"
|
11
|
+
|
12
|
+
expect( criteria.first.scorpion ).to eq scorpion
|
13
|
+
end
|
14
|
+
|
15
|
+
it "shares scorpion with chained relations" do
|
16
|
+
expect( criteria.where( name: "" ).scorpion ).to be scorpion
|
17
|
+
end
|
18
|
+
|
19
|
+
it "shares scorpion with new records" do
|
20
|
+
expect( criteria.new.scorpion ).to be scorpion
|
21
|
+
expect( criteria.build.scorpion ).to be scorpion
|
22
|
+
end
|
23
|
+
|
24
|
+
it "shares scorpion with new records builder block" do
|
25
|
+
criteria.new do |todo|
|
26
|
+
expect( todo.scorpion ).to be scorpion
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
it "shares scorpion with created records" do
|
31
|
+
expect( criteria.create.scorpion ).to be scorpion
|
32
|
+
end
|
33
|
+
|
34
|
+
it "shares scorpion with created records builder block" do
|
35
|
+
criteria.create do |todo|
|
36
|
+
expect( todo.scorpion ).to be scorpion
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
it "shares scorpion with created! records" do
|
41
|
+
expect( criteria.create!.scorpion ).to be scorpion
|
42
|
+
end
|
43
|
+
|
44
|
+
it "shares scorpion with created! records builder block" do
|
45
|
+
criteria.create! do |todo|
|
46
|
+
expect( todo.scorpion ).to be scorpion
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
context "find methods" do
|
51
|
+
let!( :todo ){ Todo.create! name: "Bill" }
|
52
|
+
|
53
|
+
it "shares scorpion with found records" do
|
54
|
+
expect( criteria.find( todo.id ).scorpion ).to be scorpion
|
55
|
+
end
|
56
|
+
|
57
|
+
it "shares scorpion with find_by records" do
|
58
|
+
expect( criteria.find_by( name: "Bill" ).scorpion ).to be scorpion
|
59
|
+
end
|
60
|
+
|
61
|
+
it "shares scorpion with first record" do
|
62
|
+
expect( criteria.first.scorpion ).to be scorpion
|
63
|
+
end
|
64
|
+
|
65
|
+
it "shares scorpion with enumerated records" do
|
66
|
+
criteria.each do |todo|
|
67
|
+
expect( todo.scorpion ).to be scorpion
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
end
|
@@ -12,14 +12,14 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
12
12
|
controller ActionController::Base do
|
13
13
|
include Scorpion::Rails::Controller
|
14
14
|
|
15
|
-
|
15
|
+
depend_on do
|
16
16
|
service Test::Nest::Service
|
17
17
|
cache Test::Nest::Cache
|
18
18
|
end
|
19
19
|
|
20
20
|
def index
|
21
|
-
@guard1 = scorpion.
|
22
|
-
@guard2 = scorpion.
|
21
|
+
@guard1 = scorpion.fetch Test::Nest::Guard
|
22
|
+
@guard2 = scorpion.fetch Test::Nest::Guard
|
23
23
|
render nothing: true
|
24
24
|
end
|
25
25
|
end
|
@@ -62,8 +62,8 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
62
62
|
|
63
63
|
it "spawns the same service during the same request" do
|
64
64
|
allow( subject ).to receive( :index ) do
|
65
|
-
service = subject.scorpion.
|
66
|
-
expect( subject.scorpion.
|
65
|
+
service = subject.scorpion.fetch Test::Nest::Service
|
66
|
+
expect( subject.scorpion.fetch Test::Nest::Service ).to be service
|
67
67
|
controller.render nothing: true
|
68
68
|
end
|
69
69
|
|
@@ -74,7 +74,7 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
74
74
|
service = subject.service
|
75
75
|
|
76
76
|
allow( subject ).to receive( :index ) do
|
77
|
-
expect( subject.scorpion.
|
77
|
+
expect( subject.scorpion.fetch Test::Nest::Service ).not_to be service
|
78
78
|
controller.render nothing: true
|
79
79
|
end
|
80
80
|
|
@@ -83,7 +83,7 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
83
83
|
|
84
84
|
it "hunts for controller" do
|
85
85
|
allow( subject ).to receive( :index ) do
|
86
|
-
expect( subject.scorpion.
|
86
|
+
expect( subject.scorpion.fetch( AbstractController::Base ) ).to be subject
|
87
87
|
controller.render nothing: true
|
88
88
|
end
|
89
89
|
|
@@ -92,7 +92,7 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
92
92
|
|
93
93
|
it "hunts for response" do
|
94
94
|
allow( subject ).to receive( :index ) do
|
95
|
-
expect( subject.scorpion.
|
95
|
+
expect( subject.scorpion.fetch( ActionDispatch::Response ) ).to be subject.response
|
96
96
|
controller.render nothing: true
|
97
97
|
end
|
98
98
|
|
@@ -101,7 +101,7 @@ describe Scorpion::Rails::Controller, type: :controller do
|
|
101
101
|
|
102
102
|
it "hunts for request" do
|
103
103
|
allow( subject ).to receive( :index ) do
|
104
|
-
expect( subject.scorpion.
|
104
|
+
expect( subject.scorpion.fetch( ActionDispatch::Request ).object_id ).to be subject.request.object_id
|
105
105
|
controller.render nothing: true
|
106
106
|
end
|
107
107
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module Test
|
4
|
+
module Job
|
5
|
+
class Compass
|
6
|
+
def directions; end
|
7
|
+
end
|
8
|
+
|
9
|
+
class Journey < ActiveJob::Base
|
10
|
+
include Scorpion::Rails::Job
|
11
|
+
|
12
|
+
depend_on do
|
13
|
+
compass Test::Job::Compass
|
14
|
+
end
|
15
|
+
|
16
|
+
def perform
|
17
|
+
compass.directions
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
describe Scorpion::Rails::Job do
|
24
|
+
it "perform has been feed" do
|
25
|
+
compass = Test::Job::Compass.new
|
26
|
+
|
27
|
+
Test::Job::Journey.scorpion_nest do |hunter|
|
28
|
+
hunter.hunt_for Test::Job::Compass, return: compass
|
29
|
+
end
|
30
|
+
|
31
|
+
expect( compass ).to receive( :directions )
|
32
|
+
Test::Job::Journey.perform_now
|
33
|
+
end
|
34
|
+
end
|