patriarch 0.0.4 → 0.1.0
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.
- data/.gitignore +1 -0
- data/README.md +43 -3
- data/lib/generators/patriarch/behaviour_generator.rb +117 -0
- data/lib/generators/patriarch/install_generator.rb +27 -10
- data/lib/generators/patriarch/templates/after_manager_service.rb +2 -1
- data/lib/generators/patriarch/templates/authorization_service.rb +1 -1
- data/lib/generators/patriarch/templates/before_manager_service.rb +2 -1
- data/lib/generators/patriarch/templates/before_service.rb +1 -1
- data/lib/generators/patriarch/templates/empty_behaviours_declaration.rb +2 -0
- data/lib/generators/patriarch/templates/empty_services_declaration.rb +2 -0
- data/lib/generators/patriarch/templates/manager_service-tripartite.rb +36 -0
- data/lib/generators/patriarch/templates/manager_service.rb +15 -8
- data/lib/generators/patriarch/templates/service-tripartite.rb +5 -0
- data/lib/generators/patriarch/templates/service.rb +1 -1
- data/lib/generators/patriarch/templates/tools_methods.rb +2 -1
- data/lib/generators/patriarch/templates/undo_service-tripartite.rb +5 -0
- data/lib/patriarch/behaviours.rb +170 -13
- data/lib/patriarch/dao_services/bipartite_relationship_builder_service.rb +0 -4
- data/lib/patriarch/dao_services/redis_mapper_service.rb +45 -8
- data/lib/patriarch/dao_services/retriever_service.rb +26 -4
- data/lib/patriarch/dao_services/tripartite_relationship_builder_service.rb +51 -0
- data/lib/patriarch/manager_service.rb +0 -9
- data/lib/patriarch/tool_services/redis_cleaner_service.rb +64 -0
- data/lib/patriarch/tool_services/redis_extractor_service.rb +33 -4
- data/lib/patriarch/transaction.rb +9 -4
- data/lib/patriarch/transaction_services/transaction_manager_service.rb +9 -0
- data/lib/patriarch/transaction_step.rb +10 -2
- data/lib/patriarch/version.rb +1 -1
- data/lib/patriarch.rb +11 -5
- data/patriarch.gemspec +2 -4
- data/spec/lib/patriarch/behaviours_spec.rb +233 -0
- data/spec/lib/patriarch/{bipartite_relationship_builder_spec.rb → dao_services/bipartite_relationship_builder_spec.rb} +19 -23
- data/spec/lib/patriarch/dao_services/tripartite_relationship_builder_spec.rb +49 -0
- data/spec/lib/patriarch/redis_mapper_service_spec.rb +46 -14
- data/spec/lib/patriarch/retriever_service_spec.rb +27 -8
- data/spec/lib/patriarch/tool_services/redis_cleaner_service_spec.rb +48 -0
- data/spec/lib/patriarch/transaction_spec.rb +13 -0
- data/spec/lib/patriarch/transaction_step_spec.rb +29 -0
- data/spec/spec_helper.rb +45 -11
- metadata +30 -53
- data/lib/generators/patriarch/patriarch_generator.rb +0 -114
- data/lib/patriarch/dao_services/.DAOinstancier.rb.swp +0 -0
- data/lib/patriarch/tool_services/redis_cleaner.rb +0 -47
- data/spec/lib/patriarch/add_behaviour_spec.rb +0 -59
- data/spec/lib/patriarch/like_service_spec.rb +0 -19
@@ -0,0 +1,233 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Patriarch::Behaviours do
|
4
|
+
describe '#add_behaviour' do
|
5
|
+
|
6
|
+
let(:monster) { Monster.new }
|
7
|
+
let(:fallen_angel) { FallenAngel.new }
|
8
|
+
let(:love_letter) { LoveLetter.new }
|
9
|
+
|
10
|
+
|
11
|
+
|
12
|
+
context 'on a bipartite' do
|
13
|
+
|
14
|
+
before do
|
15
|
+
# Monster is the actor model and FallenAngel the target model here
|
16
|
+
Monster.add_behaviour :like, :on => [:fallenAngel]
|
17
|
+
FallenAngel.add_behaviour :like, :by => [:monster]
|
18
|
+
monster.like fallen_angel
|
19
|
+
end
|
20
|
+
|
21
|
+
context "call from a model including Patriarch::Behaviours" do
|
22
|
+
|
23
|
+
it "defines instances methods to retrieve model instances having relationships with actor model" do
|
24
|
+
monster.should respond_to("#{"fallen_angel".pluralize}_i_like")
|
25
|
+
end
|
26
|
+
|
27
|
+
it "defines instances methods to retrieve ids of model instances having relationships with actor model" do
|
28
|
+
monster.should respond_to("#{"fallen_angel".pluralize}_i_like_ids")
|
29
|
+
end
|
30
|
+
|
31
|
+
it "defines instances methods to retrieve model instances having relationships with target model" do
|
32
|
+
progressive_like = (Verbs::Conjugator.conjugate :like, :aspect => :progressive).split(/ /).last
|
33
|
+
fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_like}_me")
|
34
|
+
end
|
35
|
+
|
36
|
+
it "defines instances methods to retrieve ids of model instances having relationships with target model" do
|
37
|
+
progressive_like = (Verbs::Conjugator.conjugate :like, :aspect => :progressive).split(/ /).last
|
38
|
+
fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_like}_me_ids")
|
39
|
+
end
|
40
|
+
|
41
|
+
it "defines behaviour instance method on actor model" do
|
42
|
+
monster.should respond_to(:like)
|
43
|
+
end
|
44
|
+
|
45
|
+
it "defines undo behaviour instance method on actor model" do
|
46
|
+
monster.should respond_to(:undo_like)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "defines alias for behaviour instance methods on actor model" do
|
50
|
+
pending("implement this with add_behaviour ARGUMENTS, [:as => alias]")
|
51
|
+
end
|
52
|
+
|
53
|
+
it "defines alias for undo behaviour instance methods on actor model" do
|
54
|
+
pending("implement this with add_behaviour ARGUMENTS, [:undo_as => alias]")
|
55
|
+
end
|
56
|
+
|
57
|
+
it "does not define behaviour instance method on target model" do
|
58
|
+
fallen_angel.should_not respond_to(:like)
|
59
|
+
end
|
60
|
+
|
61
|
+
it "does not define undo behaviour instance method on target model" do
|
62
|
+
fallen_angel.should_not respond_to(:undo_like)
|
63
|
+
end
|
64
|
+
|
65
|
+
it "does not define alias for behaviour instance methods on target model" do
|
66
|
+
pending("implement this with add_behaviour ARGUMENTS, [:as => alias]")
|
67
|
+
end
|
68
|
+
|
69
|
+
it "does not define alias for undo behaviour instance methods on target model" do
|
70
|
+
pending("implement this with add_behaviour ARGUMENTS, [:undo_as => alias]")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
context 'implements methods that' do
|
75
|
+
|
76
|
+
# Move tests in da right place ? Does it belongs to manager_service ?
|
77
|
+
it 'lets transaction build without running' do
|
78
|
+
monster.like(fallen_angel, :stop_execution => true).should be_an_instance_of(Patriarch::Transaction)
|
79
|
+
end
|
80
|
+
|
81
|
+
it 'lets undo transaction build without running' do
|
82
|
+
monster.undo_like(fallen_angel, :stop_execution => true).should be_an_instance_of(Patriarch::Transaction)
|
83
|
+
end
|
84
|
+
|
85
|
+
before(:all) { monster.like fallen_angel }
|
86
|
+
|
87
|
+
context 'when doing behaviour' do
|
88
|
+
it "allows actor to remember target id" do
|
89
|
+
monster.fallen_angels_i_like_ids.should == [fallen_angel.id]
|
90
|
+
end
|
91
|
+
|
92
|
+
it "allows actor to remember target instance" do
|
93
|
+
monster.fallen_angels_i_like.should == [fallen_angel]
|
94
|
+
end
|
95
|
+
|
96
|
+
it "allows target to remember actor id" do
|
97
|
+
fallen_angel.monsters_liking_me_ids.should == [monster.id]
|
98
|
+
end
|
99
|
+
|
100
|
+
it "allows target to remember actor instance" do
|
101
|
+
fallen_angel.monsters_liking_me.should == [monster]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
context 'when undoing behaviour' do
|
106
|
+
it "erase target id from actor's memory" do
|
107
|
+
monster.fallen_angels_i_like_ids.should == [fallen_angel.id]
|
108
|
+
end
|
109
|
+
|
110
|
+
it "erase target instance from actor's memory" do
|
111
|
+
monster.fallen_angels_i_like.should == [fallen_angel]
|
112
|
+
end
|
113
|
+
|
114
|
+
it "erase actor id from target's memory" do
|
115
|
+
fallen_angel.monsters_liking_me_ids.should == [monster.id]
|
116
|
+
end
|
117
|
+
|
118
|
+
it "erase actor instance from target's memory" do
|
119
|
+
fallen_angel.monsters_liking_me.should == [monster]
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
after(:all) { monster.undo_like fallen_angel }
|
124
|
+
end
|
125
|
+
end # context bipartite
|
126
|
+
|
127
|
+
context 'on a tripartite' do
|
128
|
+
|
129
|
+
before do
|
130
|
+
# Monster is the actor model and FallenAngel the target model here
|
131
|
+
Monster.add_behaviour :praise, :on => [:fallenAngel], :via => [:loveLetter]
|
132
|
+
FallenAngel.add_behaviour :praise, :by => [:monster], :via => [:loveLetter]
|
133
|
+
LoveLetter.add_behaviour :praise, :medium_between => [:monster,:fallenAngel]
|
134
|
+
monster.praise(fallen_angel,love_letter)
|
135
|
+
end
|
136
|
+
|
137
|
+
context "call from a model including Patriarch::Behaviours" do
|
138
|
+
it "defines instances methods to retrieve model instances having relationships with actor model" do
|
139
|
+
monster.should respond_to("#{"fallen_angel".pluralize}_i_praise_via_#{"love_letter".pluralize}")
|
140
|
+
end
|
141
|
+
|
142
|
+
it "defines instances methods to retrieve ids of model instances having relationships with actor model" do
|
143
|
+
monster.should respond_to("#{"fallen_angel".pluralize}_i_praise_via_#{"love_letter".pluralize}_ids")
|
144
|
+
end
|
145
|
+
|
146
|
+
it "defines instances methods to retrieve model instances having relationships with target model" do
|
147
|
+
progressive_praise = (Verbs::Conjugator.conjugate :praise, :aspect => :progressive).split(/ /).last
|
148
|
+
fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_praise}_me_via_#{"love_letter".pluralize}")
|
149
|
+
end
|
150
|
+
|
151
|
+
it "defines instances methods to retrieve ids of model instances having relationships with target model" do
|
152
|
+
progressive_praise = (Verbs::Conjugator.conjugate :praise, :aspect => :progressive).split(/ /).last
|
153
|
+
fallen_angel.should respond_to("#{"monster".pluralize}_#{progressive_praise}_me_via_#{"love_letter".pluralize}_ids")
|
154
|
+
end
|
155
|
+
|
156
|
+
it "defines behaviour instance method on actor model" do
|
157
|
+
monster.should respond_to(:praise)
|
158
|
+
end
|
159
|
+
|
160
|
+
it "defines undo behaviour instance method on actor model" do
|
161
|
+
monster.should respond_to(:undo_praise)
|
162
|
+
end
|
163
|
+
|
164
|
+
it "does not define behaviour instance method on target model" do
|
165
|
+
fallen_angel.should_not respond_to(:praise)
|
166
|
+
end
|
167
|
+
|
168
|
+
it "does not define undo behaviour instance method on target model" do
|
169
|
+
fallen_angel.should_not respond_to(:undo_praise)
|
170
|
+
end
|
171
|
+
|
172
|
+
it "does not define behaviour instance method on medium model" do
|
173
|
+
love_letter.should_not respond_to(:praise)
|
174
|
+
end
|
175
|
+
|
176
|
+
it "does not define undo behaviour instance method on medium model" do
|
177
|
+
love_letter.should_not respond_to(:undo_praise)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
context 'implements methods that' do
|
182
|
+
it 'lets transaction build without running' do
|
183
|
+
monster.praise(fallen_angel, love_letter, :stop_execution => true).should be_an_instance_of(Patriarch::Transaction)
|
184
|
+
end
|
185
|
+
|
186
|
+
it 'lets undo transaction build without running' do
|
187
|
+
monster.praise(fallen_angel, love_letter, :stop_execution => true).should be_an_instance_of(Patriarch::Transaction)
|
188
|
+
end
|
189
|
+
|
190
|
+
before(:all) { monster.praise fallen_angel,love_letter }
|
191
|
+
|
192
|
+
context 'when doing behaviour' do
|
193
|
+
it "allows actor to remember target id" do
|
194
|
+
monster.fallen_angels_i_praise_via_love_letters_ids.should == [fallen_angel.id]
|
195
|
+
end
|
196
|
+
|
197
|
+
it "allows actor to remember target instance" do
|
198
|
+
monster.fallen_angels_i_praise_via_love_letters.should == [fallen_angel]
|
199
|
+
end
|
200
|
+
|
201
|
+
it "allows target to remember actor id" do
|
202
|
+
fallen_angel.monsters_praising_me_via_love_letters_ids.should == [monster.id]
|
203
|
+
end
|
204
|
+
|
205
|
+
it "allows target to remember actor instance" do
|
206
|
+
fallen_angel.monsters_praising_me_via_love_letters.should == [monster]
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
context 'when undoing behaviour' do
|
211
|
+
it "erase target id from actor's memory" do
|
212
|
+
monster.fallen_angels_i_praise_via_love_letters_ids.should == [fallen_angel.id]
|
213
|
+
end
|
214
|
+
|
215
|
+
it "erase target instance from actor's memory" do
|
216
|
+
monster.fallen_angels_i_praise_via_love_letters.should == [fallen_angel]
|
217
|
+
end
|
218
|
+
|
219
|
+
it "erase actor id from target's memory" do
|
220
|
+
fallen_angel.monsters_praising_me_via_love_letters_ids.should == [monster.id]
|
221
|
+
end
|
222
|
+
|
223
|
+
it "erase actor instance from target's memory" do
|
224
|
+
fallen_angel.monsters_praising_me_via_love_letters.should == [monster]
|
225
|
+
end
|
226
|
+
end
|
227
|
+
|
228
|
+
after(:all) { monster.undo_praise fallen_angel,love_letter }
|
229
|
+
end
|
230
|
+
end # context tripartite
|
231
|
+
|
232
|
+
end # desc '#add_behaviour'
|
233
|
+
end # desc Patriarch::Behaviours
|
@@ -1,24 +1,27 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
|
4
|
+
|
5
|
+
let(:monster) { Monster.new }
|
6
|
+
let(:fallen_angel) { FallenAngel.new }
|
7
|
+
let(:instance) { Patriarch::DAOServices::BipartiteRelationshipBuilderService.instance }
|
8
|
+
|
4
9
|
before(:each) do
|
5
|
-
|
6
|
-
@monster = new_monster
|
7
|
-
@fallen_angel = new_fallen_angel
|
10
|
+
# Deals with transaction
|
8
11
|
@transac ||= Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction(:like)
|
9
|
-
@transac.add_step(:like
|
10
|
-
|
11
|
-
|
12
|
-
it "shall insert processable lambdas into queues when create is called" do
|
12
|
+
@transac.add_step(:like,monster,fallen_angel)
|
13
|
+
|
13
14
|
# Stubs the DAO retriever service so it gives DAO arrays back
|
14
15
|
dao_sample = {
|
15
|
-
:actor => Redis::SortedSet.new("monster:
|
16
|
-
:target => Redis::SortedSet.new("fallen_angel:
|
16
|
+
:actor => Redis::SortedSet.new("monster:#{monster.id}:patriarch_fallen_angels_i_like"),
|
17
|
+
:target => Redis::SortedSet.new("fallen_angel:#{fallen_angel.id}:patriarch_monsters_liking_me")
|
17
18
|
}
|
18
|
-
Patriarch::DAOServices::RetrieverService.instance.stub(:call).twice.with(any_args()).and_return(dao_sample)
|
19
|
-
|
19
|
+
Patriarch::DAOServices::RetrieverService.instance.stub(:call).twice.with(any_args()).and_return(dao_sample)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "shall insert processable lambdas into queues when create is called" do
|
20
23
|
expect{
|
21
|
-
|
24
|
+
instance.create(@transac)
|
22
25
|
}.to change{ @transac.transaction_queue.select{ |queue_element| queue_element.is_a? Proc}.size }.by(2)
|
23
26
|
|
24
27
|
expect{
|
@@ -27,15 +30,8 @@ describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
|
|
27
30
|
end
|
28
31
|
|
29
32
|
it "shall insert processable lambdas into queues when destroy is called" do
|
30
|
-
# Stubs the DAO retriever service so it gives DAO arrays back
|
31
|
-
dao_sample = {
|
32
|
-
:actor => Redis::SortedSet.new("monster:1:patriarch_fallen_angels_i_like"),
|
33
|
-
:target => Redis::SortedSet.new("fallen_angel:1:patriarch_monsters_liking_me")
|
34
|
-
}
|
35
|
-
Patriarch::DAOServices::RetrieverService.instance.stub(:call).twice.with(any_args()).and_return(dao_sample)
|
36
|
-
|
37
33
|
expect{
|
38
|
-
|
34
|
+
instance.destroy(@transac)
|
39
35
|
}.to change{ @transac.transaction_queue.select{ |queue_element| queue_element.is_a? Proc}.size }.by(2)
|
40
36
|
|
41
37
|
expect{
|
@@ -44,7 +40,7 @@ describe Patriarch::DAOServices::BipartiteRelationshipBuilderService do
|
|
44
40
|
end
|
45
41
|
|
46
42
|
after(:each) do
|
47
|
-
$redis.del "monster:#{
|
48
|
-
$redis.del "fallen_angel:#{
|
43
|
+
$redis.del "monster:#{monster.id}:patriarch_fallen_angels_i_like"
|
44
|
+
$redis.del "fallen_angel:#{fallen_angel.id}:patriarch_monsters_liking_me"
|
49
45
|
end
|
50
|
-
end
|
46
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Patriarch::DAOServices::TripartiteRelationshipBuilderService do
|
4
|
+
|
5
|
+
let(:monster) { Monster.new }
|
6
|
+
let(:fallen_angel) { FallenAngel.new }
|
7
|
+
let(:love_letter) { LoveLetter.new }
|
8
|
+
let(:instance) { Patriarch::DAOServices::TripartiteRelationshipBuilderService.instance }
|
9
|
+
|
10
|
+
before(:each) do
|
11
|
+
# Deals with transaction
|
12
|
+
@transac ||= Patriarch::TransactionServices::TransactionManagerService.instance.new_transaction(:praise)
|
13
|
+
@transac.add_step(:praise,monster,fallen_angel,love_letter)
|
14
|
+
|
15
|
+
# Stubs the DAO retriever service so it gives DAO arrays back
|
16
|
+
dao_sample = {
|
17
|
+
:actor => Redis::SortedSet.new("monster:#{monster.id}:patriarch_fallen_angels_i_praise_via_love_letters",:marshal => true),
|
18
|
+
:target => Redis::SortedSet.new("fallen_angel:#{fallen_angel.id}:patriarch_monsters_prasing_me_via_love_letters",:marshal => true),
|
19
|
+
:medium => Redis::SortedSet.new("love_letter:#{fallen_angel.id}:patriarch_monsters_prasing_fallen_angels_via_me",:marshal => true)
|
20
|
+
}
|
21
|
+
Patriarch::DAOServices::RetrieverService.instance.stub(:call).exactly(3).times.with(any_args()).and_return(dao_sample)
|
22
|
+
end
|
23
|
+
|
24
|
+
it "shall insert processable lambdas into queues when create is called" do
|
25
|
+
expect{
|
26
|
+
instance.create(@transac)
|
27
|
+
}.to change{ @transac.transaction_queue.select{ |queue_element| queue_element.is_a? Proc}.size }.by(3)
|
28
|
+
|
29
|
+
expect{
|
30
|
+
@transac.transaction_queue.each{ |proc| proc.call }
|
31
|
+
}.to change{ $redis.keys.size}.by(3)
|
32
|
+
end
|
33
|
+
|
34
|
+
it "shall insert processable lambdas into queues when destroy is called" do
|
35
|
+
expect{
|
36
|
+
instance.destroy(@transac)
|
37
|
+
}.to change{ @transac.transaction_queue.select{ |queue_element| queue_element.is_a? Proc}.size }.by(3)
|
38
|
+
|
39
|
+
expect{
|
40
|
+
@transac.transaction_queue.each{ |proc| proc.call }
|
41
|
+
}.to change{ $redis.info["total_commands_processed"].to_i}.by(4)
|
42
|
+
end
|
43
|
+
|
44
|
+
after(:each) do
|
45
|
+
$redis.del "monster:#{monster.id}:patriarch_fallen_angels_i_praise_via_love_letters"
|
46
|
+
$redis.del "fallen_angel:#{fallen_angel.id}:patriarch_monsters_prasing_me_via_love_letters"
|
47
|
+
$redis.del "love_letter:#{fallen_angel.id}:patriarch_monsters_prasing_fallen_angels_via_me"
|
48
|
+
end
|
49
|
+
end
|
@@ -1,21 +1,53 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Patriarch::DAOServices::RedisMapperService do
|
4
|
-
before(:each) do
|
5
|
-
@instance = Patriarch::DAOServices::RedisMapperService.instance
|
6
|
-
@context = {:relation_type => :like, :actor_type => :monster, :target_type => :fallen_angel,
|
7
|
-
:actor_id => 1, :target_id => 1}
|
8
|
-
@transaction_item = double()
|
9
|
-
@transaction_item.stub(:relation_type).and_return(@context[:relation_type])
|
10
|
-
@transaction_item.stub(:actor_type).and_return(@context[:actor_type])
|
11
|
-
@transaction_item.stub(:target_type).and_return(@context[:target_type])
|
12
|
-
end
|
13
4
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
5
|
+
context "in bipartite situations" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@instance = Patriarch::DAOServices::RedisMapperService.instance
|
9
|
+
@context = {:relation_type => :like, :actor_type => :monster, :target_type => :fallen_angel,
|
10
|
+
:actor_id => 1, :target_id => 1}
|
11
|
+
@transaction_item = double()
|
12
|
+
@transaction_item.stub(:relation_type).and_return(@context[:relation_type])
|
13
|
+
@transaction_item.stub(:actor_type).and_return(@context[:actor_type])
|
14
|
+
@transaction_item.stub(:target_type).and_return(@context[:target_type])
|
15
|
+
@transaction_item.stub(:medium_type).and_return(@context[:medium_type])
|
16
|
+
@transaction_item.stub(:tripartite?).and_return(false)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "retrieves DAO" do
|
20
|
+
@instance.call(@transaction_item,:actor).should eq({:type => "sorted_set", :key => "patriarch_fallen_angels_i_like"})
|
21
|
+
@instance.call(@transaction_item,:target).should eq({:type => "sorted_set", :key => "patriarch_monsters_liking_me"})
|
22
|
+
end
|
23
|
+
end # bipartite context
|
24
|
+
|
25
|
+
context "in tripartite situations" do
|
26
|
+
before do
|
27
|
+
@instance = Patriarch::DAOServices::RedisMapperService.instance
|
28
|
+
@context = {:relation_type => :tease, :actor_type => :fallen_angel, :target_type => :monster, :medium_type => :love_letter,
|
29
|
+
:actor_id => 1, :target_id => 1, :medium_id => 1}
|
30
|
+
@transaction_item = double()
|
31
|
+
@transaction_item.stub(:relation_type).and_return(@context[:relation_type])
|
32
|
+
@transaction_item.stub(:actor_type).and_return(@context[:actor_type])
|
33
|
+
@transaction_item.stub(:target_type).and_return(@context[:target_type])
|
34
|
+
@transaction_item.stub(:medium_type).and_return(@context[:medium_type])
|
35
|
+
@transaction_item.stub(:tripartite?).and_return(true)
|
36
|
+
end
|
37
|
+
|
38
|
+
it "retrieves DAO" do
|
39
|
+
@instance.call(@transaction_item,:actor).should eq(
|
40
|
+
{:type => "sorted_set", :key => "patriarch_monsters_i_tease_via_love_letters", :options => {:marshal => true}}
|
41
|
+
)
|
42
|
+
@instance.call(@transaction_item,:target).should eq(
|
43
|
+
{:type => "sorted_set", :key => "patriarch_fallen_angels_teasing_me_via_love_letters",:options => {:marshal => true}}
|
44
|
+
)
|
45
|
+
@instance.call(@transaction_item,:medium).should eq(
|
46
|
+
{:type => "sorted_set", :key => "patriarch_fallen_angels_teasing_monsters_via_me",:options => {:marshal => true}}
|
47
|
+
)
|
48
|
+
end
|
49
|
+
end #tripartite context
|
50
|
+
end # describe
|
19
51
|
|
20
52
|
|
21
53
|
|
@@ -1,28 +1,47 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Patriarch::DAOServices::RetrieverService do
|
4
|
+
|
4
5
|
before(:each) do
|
5
6
|
@instance = Patriarch::DAOServices::RetrieverService.instance
|
6
|
-
@context = {:relation_type => :like, :actor_type => :
|
7
|
+
@context = {:relation_type => :like, :actor_type => :monster, :target_type => :fallen_angel,
|
7
8
|
:actor_id => 1, :target_id => 1}
|
8
9
|
@transaction_item = double()
|
9
10
|
@transaction_item.stub(:actor_id).and_return(@context[:actor_id])
|
10
11
|
@transaction_item.stub(:actor_type).and_return(@context[:actor_type])
|
11
12
|
@transaction_item.stub(:target_type).and_return(@context[:target_type])
|
12
|
-
@transaction_item.stub(:target_id).and_return(@context[:target_id])
|
13
|
+
@transaction_item.stub(:target_id).and_return(@context[:target_id])
|
13
14
|
end
|
14
15
|
|
15
|
-
it "
|
16
|
+
it "returns DAO for actor" do
|
17
|
+
@transaction_item.stub(:tripartite?).and_return(false)
|
16
18
|
Patriarch::DAOServices::RedisMapperService.instance.should_receive(:call).twice.with(any_args()).and_return({:type => "sorted_set", :key =>"abc"})
|
17
19
|
dao_tab = @instance.call(@transaction_item)
|
18
20
|
|
19
|
-
dao_tab[:actor].key.should == "
|
20
|
-
dao_tab[:actor].is_a?(Redis::SortedSet).should be_true
|
21
|
-
|
22
|
-
dao_tab[:target].key.should == "item:1:abc"
|
23
|
-
dao_tab[:target].is_a?(Redis::SortedSet).should be_true
|
21
|
+
dao_tab[:actor].key.should == "monster:1:abc"
|
22
|
+
dao_tab[:actor].is_a?(Redis::SortedSet).should be_true
|
24
23
|
end
|
25
24
|
|
25
|
+
it "returns DAO for target" do
|
26
|
+
@transaction_item.stub(:tripartite?).and_return(false)
|
27
|
+
Patriarch::DAOServices::RedisMapperService.instance.should_receive(:call).twice.with(any_args()).and_return({:type => "sorted_set", :key =>"abc"})
|
28
|
+
dao_tab = @instance.call(@transaction_item)
|
29
|
+
|
30
|
+
dao_tab[:target].key.should == "fallen_angel:1:abc"
|
31
|
+
dao_tab[:target].is_a?(Redis::SortedSet).should be_true
|
32
|
+
end
|
33
|
+
|
34
|
+
it "returns DAO for medium" do
|
35
|
+
@context.merge! :medium_type => :love_letter, :medium_id => 1
|
36
|
+
@transaction_item.stub(:medium_type).and_return(@context[:medium_type])
|
37
|
+
@transaction_item.stub(:medium_id).and_return(@context[:medium_id])
|
38
|
+
@transaction_item.stub(:tripartite?).and_return(true)
|
39
|
+
Patriarch::DAOServices::RedisMapperService.instance.should_receive(:call).exactly(3).times.with(any_args()).and_return({:type => "sorted_set", :key =>"abc"})
|
40
|
+
dao_tab = @instance.call(@transaction_item)
|
41
|
+
|
42
|
+
dao_tab[:medium].key.should == "love_letter:1:abc"
|
43
|
+
dao_tab[:medium].is_a?(Redis::SortedSet).should be_true
|
44
|
+
end
|
26
45
|
|
27
46
|
it "shall reject types that are not suited" do
|
28
47
|
Patriarch::DAOServices::RedisMapperService.instance.should_receive(:call).with(any_args()).and_return({:type => "sorted", :key =>"abc"})
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Patriarch::ToolServices::RedisCleanerService.instance do
|
4
|
+
|
5
|
+
let(:monster) { Monster.new }
|
6
|
+
let(:beast) { Monster.new }
|
7
|
+
# Wrong usage when summoning several times fallen_angel within tests; bad knowledge of
|
8
|
+
# let is in cause i think
|
9
|
+
# let(:fallen_angel) { FallenAngel.new }
|
10
|
+
let(:instance) { Patriarch::ToolServices::RedisCleanerService.instance }
|
11
|
+
|
12
|
+
context "#clean_all" do
|
13
|
+
it "removes presence into other sets for each behaviour" do
|
14
|
+
pending("implement this")
|
15
|
+
end
|
16
|
+
|
17
|
+
it "removes presence into other set for a given behaviour" do
|
18
|
+
pending("implement this")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
context "#clean_behaviour" do
|
23
|
+
it "removes given behaviour binds on self" do
|
24
|
+
f = FallenAngel.new
|
25
|
+
f.lure Monster.new
|
26
|
+
instance.clean_behaviour(f,:lure)
|
27
|
+
f.monsters_i_lure.should be_empty
|
28
|
+
end
|
29
|
+
|
30
|
+
it "removes given behaviour binds on other entities" do
|
31
|
+
f = FallenAngel.new
|
32
|
+
f.lure Monster.new
|
33
|
+
instance.clean_behaviour(f,:lure)
|
34
|
+
beast.fallen_angels_luring_me.include?(f).should be_false
|
35
|
+
monster.fallen_angels_luring_me.include?(f).should be_false
|
36
|
+
end
|
37
|
+
|
38
|
+
it "runs within a unique transaction" do
|
39
|
+
f = FallenAngel.new
|
40
|
+
10.times { |t| f.lure Monster.new }
|
41
|
+
before_count = Patriarch::TransactionServices::TransactionManagerService.instance.count
|
42
|
+
instance.clean_behaviour(f,:lure)
|
43
|
+
after_count = Patriarch::TransactionServices::TransactionManagerService.instance.count
|
44
|
+
(after_count - before_count).should eq(1)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
end # desc Patriarch::Behaviours::RedisCleanerService.instance
|
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Patriarch::Transaction do
|
4
|
+
|
5
|
+
let(:monster) { Monster.new }
|
6
|
+
let(:fallen_angel) { FallenAngel.new }
|
7
|
+
let(:love_letter) { LoveLetter.new }
|
8
|
+
let(:transaction_step){ Patriarch::TransactionStep.new(:pick_up,monster,fallen_angel,love_letter) }
|
9
|
+
let(:transaction) { Patriarch::Transaction.new(:transaction_test,42) }
|
10
|
+
|
11
|
+
context "forwards to current step" do
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Patriarch::TransactionStep do
|
4
|
+
|
5
|
+
let(:monster) { Monster.new }
|
6
|
+
let(:fallen_angel) { FallenAngel.new }
|
7
|
+
let(:love_letter) { LoveLetter.new }
|
8
|
+
let(:transaction_step){ Patriarch::TransactionStep.new(:pick_up,monster,fallen_angel,love_letter) }
|
9
|
+
|
10
|
+
it "#actor returns the actor instance" do
|
11
|
+
transaction_step.actor.should == [monster]
|
12
|
+
end
|
13
|
+
|
14
|
+
it "#medium returns the medium instance" do
|
15
|
+
transaction_step.medium.should == [love_letter]
|
16
|
+
end
|
17
|
+
|
18
|
+
it "#target returns the target instance" do
|
19
|
+
transaction_step.target.should == [fallen_angel]
|
20
|
+
end
|
21
|
+
|
22
|
+
it "#add_to_queue appends proc to queue" do
|
23
|
+
p = Proc.new { p "do_nothing"}
|
24
|
+
q = Proc.new { p "do_nothing"}
|
25
|
+
transaction_step.add_to_queue p
|
26
|
+
transaction_step.add_to_queue q
|
27
|
+
transaction_step.queue.should ==[p,q]
|
28
|
+
end
|
29
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -12,7 +12,7 @@ require 'action_view'
|
|
12
12
|
require 'rails/generators'
|
13
13
|
require 'rspec/autorun'
|
14
14
|
|
15
|
-
require File.expand_path("../../lib/generators/patriarch/
|
15
|
+
#require File.expand_path("../../lib/generators/patriarch/behaviour_generator", __FILE__)
|
16
16
|
|
17
17
|
RSpec.configure do |config|
|
18
18
|
# ## Mock Framework
|
@@ -36,18 +36,22 @@ puts "Cleaning previous dummy"
|
|
36
36
|
# Destroy previously initialized files in dummy, clean up !
|
37
37
|
puts "Generating files to be tested in a dummy"
|
38
38
|
|
39
|
+
puts "Installing gem .."
|
40
|
+
Patriarch::Generators::InstallGenerator.start(%w(fake_arg),:destination_root => dummy_location)
|
41
|
+
|
39
42
|
# Initialize files in dummy :)
|
40
43
|
# Capture prevents stdout outputs from code between curly braces to be displayed
|
41
44
|
# This avoid polluting user interface
|
42
|
-
|
45
|
+
Patriarch::Generators::BehaviourGenerator.start(%w(like),{:destination_root => dummy_location})
|
46
|
+
Patriarch::Generators::BehaviourGenerator.start(%w(lure),{:destination_root => dummy_location})
|
47
|
+
Patriarch::Generators::BehaviourGenerator.start(%w(praise tripartite),{:destination_root => dummy_location})
|
43
48
|
|
44
49
|
#Enhance load_path with dummy located files ...
|
45
50
|
$LOAD_PATH << File.expand_path("../../spec/dummy/lib", __FILE__)
|
46
51
|
$LOAD_PATH << File.expand_path("../../lib", __FILE__)
|
47
52
|
|
48
|
-
# load files living in the dummy
|
49
|
-
require File.expand_path("../../spec/dummy/
|
50
|
-
require File.expand_path("../../spec/dummy/lib/patriarch/behaviours/like", __FILE__)
|
53
|
+
# load files living in the dummy using initilizer like in a rails situation
|
54
|
+
require File.expand_path("../../spec/dummy/config/initializers/patriarch", __FILE__)
|
51
55
|
|
52
56
|
class Monster
|
53
57
|
attr_reader :id
|
@@ -105,13 +109,43 @@ class FallenAngel
|
|
105
109
|
end
|
106
110
|
end
|
107
111
|
|
112
|
+
class LoveLetter
|
113
|
+
attr_reader :id
|
114
|
+
|
115
|
+
class << self
|
116
|
+
attr_accessor :count, :object_mapping
|
117
|
+
end
|
118
|
+
|
119
|
+
self.count = 1
|
120
|
+
self.object_mapping = {}
|
121
|
+
|
122
|
+
def initialize
|
123
|
+
@id = self.class.count
|
124
|
+
self.class.count += 1
|
125
|
+
self.class.object_mapping.merge!({@id => self.object_id})
|
126
|
+
end
|
127
|
+
|
128
|
+
def self.find(ids)
|
129
|
+
ids = Array(ids)
|
130
|
+
result = []
|
131
|
+
ids.each do |id|
|
132
|
+
object_ref = self.object_mapping[id]
|
133
|
+
record = ObjectSpace._id2ref(object_ref)
|
134
|
+
result << record unless record.nil?
|
135
|
+
end
|
136
|
+
result
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
108
140
|
Monster.send(:include,Patriarch::Behaviours)
|
109
141
|
FallenAngel.send(:include,Patriarch::Behaviours)
|
142
|
+
LoveLetter.send(:include,Patriarch::Behaviours)
|
110
143
|
|
111
|
-
|
112
|
-
|
113
|
-
|
144
|
+
Monster.add_behaviour :like, :on => [:fallenAngel]
|
145
|
+
Monster.add_behaviour :lure, :by => [:fallenAngel]
|
146
|
+
FallenAngel.add_behaviour :like, :by => [:monster]
|
147
|
+
FallenAngel.add_behaviour :lure, :on => [:monster]
|
114
148
|
|
115
|
-
|
116
|
-
|
117
|
-
|
149
|
+
LoveLetter.add_behaviour :praise, :medium_between => [:monster,:fallenAngel]
|
150
|
+
Monster.add_behaviour :praise, :on => [:fallenAngel], :via => [:LoveLetter]
|
151
|
+
FallenAngel.add_behaviour :praise, :by => [:monster], :via => [:LoveLetter]
|