Mandel/Relationship/ListOf.pm

filter: Statementstmt 28.8% 34 / 118 Branchbran 0.0% 0 / 38 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 52.3% 11 / 21 Podpod 100% 1 / 1 totaltotal 20.1% 46 / 228 CC 34 SCAR 33.6
CC
34
Cov
18%
__ANON__
56.0
__ANON__
42.0
__ANON__
42.0
CRAP 28.8
« Mandel/Relationship/BelongsTo.pm ↑ Summary Mandel/Relationship/HasOne.pm »
1 package Mandel::Relationship::ListOf;
2 1 ▶use Mojo::Base 'Mandel::Relationship';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mango::BSON 'bson_dbref';
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶has push_method_name => sub { sprintf 'push_%s', shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
7 1 ▶has remove_method_name => sub { sprintf 'remove_%s', shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
8 1 ▶has search_method_name => sub { sprintf 'search_%s', shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
9  
10 1 ▶sub monkey_patch {
Subroutine monkey_patch: called CC=1 SCAR=0.0
Pod documented
11 1 shift->_monkey_patch_all_method->_monkey_patch_push_method->_monkey_patch_remove_method->_monkey_patch_search_method;
12 }
13  
14 1 ▶sub _monkey_patch_all_method {
Subroutine _monkey_patch_all_method: called CC=1 SCAR=5.0
15 1 my $self = shift;
16 1 my $accessor = $self->accessor;
17 1 my $search = $self->search_method_name;
18  
19 Mojo::Util::monkey_patch(
20 $self->document_class,
21 $accessor,
22 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
23 0 my ($doc, $cb) = @_;
24 0 ▶ my $cached = delete $doc->{fresh} ? undef : $doc->_cache($accessor);
Branch: 1 branch
BranchTrueFalseTotal
delete $doc->{'fresh'} ? : 0 0 0
25  
26 # Blocking
27 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
28 0 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
29 0 my %lookup = map { $_->id, $_ } @{$doc->$search->all};
30 0 ▶ return $doc->_cache($accessor => [map { $lookup{$_->{'$id'}} } @{$doc->data->{$accessor} || []}]);
Condition0%0/2
$doc->data->{$accessor} || []
l!l
00
MC/DC0%0/1
$doc->data->{$accessor} || []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
31 }
32  
33 # Non-blocking
34 0 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) { } 0 0 0
35 0 $doc->$cb('', $cached);
36 }
37 else {
38 $doc->$search->all(
39 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
40 0 my ($collection, $err, $objs) = @_;
41 0 my %lookup = map { $_->id, $_ } @$objs;
42 0 ▶ $doc->$cb($err, $doc->_cache($accessor => [map { $lookup{$_->{'$id'}} } @{$doc->data->{$accessor} || []}]));
Condition0%0/2
$doc->data->{$accessor} || []
l!l
00
MC/DC0%0/1
$doc->data->{$accessor} || []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
43 }
44 0 );
45 }
46  
47 0 return $doc;
48 }
49 1 );
50  
51 1 return $self;
52 }
53  
54 1 ▶sub _monkey_patch_push_method {
Subroutine _monkey_patch_push_method: called CC=1 SCAR=6.1
55 1 my $self = shift;
56 1 my $accessor = $self->accessor;
57  
58 Mojo::Util::monkey_patch(
59 $self->document_class,
60 $self->push_method_name,
61 0 ▶ sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
62 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
63 0 my ($doc, $obj, $pos) = @_;
64 0 my ($dbref, $push_ref, @update);
65 0 my $cached = $doc->_cache($accessor);
66  
67 0 ▶ if (ref $obj eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $obj eq 'HASH') { } 0 0 0
elsif (UNIVERSAL::isa($obj, 'Mango::BSON::ObjectID')) { } 0 0 0
68 0 $obj = $self->_related_model->new_collection($doc->connection)->create($obj);
69 }
70 elsif (UNIVERSAL::isa($obj, 'Mango::BSON::ObjectID')) {
71 0 $obj = $self->_related_model->new_collection($doc->connection)->create({id => $obj});
72 0 $obj->_mark_stored_clean; # prevent save() from actually doing something below
73 }
74  
75 0 $dbref = bson_dbref $obj->model->collection_name, $obj->id;
76  
77 0 ▶ @update = (
Branch: 1 branch
BranchTrueFalseTotal
defined $pos ? : 0 0 0
78 {_id => $doc->id},
79 {'$push' => {$accessor => {'$each' => [$dbref], defined $pos ? ('$position' => $pos + 0) : ()}}},
80 {upsert => 1},
81 );
82  
83 0 ▶ $push_ref = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
84 0 ▶ my $list = $doc->data->{$accessor} ||= [];
Condition0%0/2
$doc->data->{$accessor} ||= []
l!l
00
MC/DC0%0/1
$doc->data->{$accessor} ||= []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
85  
86 0 ▶ if (defined $pos and $pos < @$list) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pos and $pos < @$list) { } 0 0 0
Condition0%0/3
defined $pos and $pos < @$list
!ll&&!rl&&r
000
MC/DC0%0/2
defined $pos and $pos < @$list
defined $pos $pos < @$list
Truth table0%0/3
A and B
A= defined $posB= $pos < @$list
ABresult
0X0
100
111
87 0 ▶ splice @$cached, $pos, 0, $obj if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
88 0 splice @$list, $pos, 0, $dbref;
89 }
90 else {
91 0 ▶ push @$cached, $obj if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
92 0 push @$list, $dbref;
93 }
94  
95 0 $doc->in_storage(1);
96 0 };
97  
98 # Blocking
99 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
100 0 $obj->save;
101 0 $doc->_storage_collection->update(@update);
102 0 $push_ref->();
103 0 return $obj;
104 }
105  
106 # Non-blocking
107 Mojo::IOLoop->delay(
108 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
109 0 my ($delay) = @_;
110 0 $obj->save($delay->begin);
111 0 $doc->_storage_collection->update(@update, $delay->begin);
112 },
113 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
114 0 my ($delay, $o_err, $d_err, $updated) = @_;
115 0 ▶ my $err = $o_err || $d_err;
Condition0%0/3
$o_err || $d_err
l!l&&r!l&&!r
000
MC/DC0%0/2
$o_err || $d_err
$o_err $d_err
Truth table0%0/3
A || B
A= $o_errB= $d_err
ABresult
1X1
011
000
116 0 ▶ $err ||= 'Document was not stored. Unknown error' unless $updated and $updated->{n};
Branch: 1 branch
BranchTrueFalseTotal
unless $updated and $updated->{"n"} 0 0 0
Condition0%0/5
$updated and $updated->{"n"}
!ll&&!rl&&r
000
$err ||= "Document was not stored. Unknown error"
l!l
00
MC/DC0%0/2
$updated and $updated->{"n"}
$updated $updated->{"n"}
MC/DC0%0/1
$err ||= "Document was not stored. Unknown error"
$err
Truth table0%0/3
A and B
A= $updatedB= $updated->{"n"}
ABresult
0X0
100
111
Truth table0%0/2
A
A= $err
Aresult
11
00
117 0 ▶ $push_ref->() unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
118 0 ▶ $doc->$cb($err // '', $obj);
Condition0%0/2
$err // ""
l!l
00
MC/DC0%0/1
$err // ""
$err
Truth table0%0/2
A
A= $err
Aresult
11
00
119 },
120 0 );
121  
122 0 return $doc;
123 }
124 1 );
125  
126 1 return $self;
127 }
128  
129 1 ▶sub _monkey_patch_remove_method {
Subroutine _monkey_patch_remove_method: called CC=1 SCAR=5.7
130 1 my $self = shift;
131 1 my $accessor = $self->accessor;
132  
133 Mojo::Util::monkey_patch(
134 $self->document_class,
135 $self->remove_method_name,
136 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
137 0 my ($doc, $obj, $cb) = @_;
138 0 my $cached = $doc->_cache($accessor);
139 0 my @update;
140  
141 0 ▶ unless (UNIVERSAL::isa($obj, 'Mandel::Document')) {
Branch: 1 branch
BranchTrueFalseTotal
unless (UNIVERSAL::isa($obj, "Mandel::Document")) 0 0 0
142 0 $obj = $self->_related_model->new_collection($doc->connection)->create({_id => $obj});
143 }
144  
145 0 @update = ({_id => $doc->id}, {'$pull' => {$accessor => bson_dbref($obj->model->collection_name, $obj->id)}});
146  
147 # Blocking
148 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
149 0 $doc->_storage_collection->update(@update);
150 0 ▶ $doc->data->{$accessor} = [grep { $_->{'$id'} ne $obj->id } @{$doc->data->{$accessor} || []}];
Condition0%0/2
$doc->data->{$accessor} || []
l!l
00
MC/DC0%0/1
$doc->data->{$accessor} || []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
151 0 ▶ @$cached = grep { $_->id ne $obj->id } @$cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
152 0 return $doc;
153 }
154  
155 # Non-blocking
156 Mojo::IOLoop->delay(
157 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
158 0 my ($delay) = @_;
159 0 $doc->_storage_collection->update(@update, $delay->begin);
160 },
161 0 ▶ sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
162 0 my ($delay, $err, $updated) = @_;
163 0 ▶ $doc->data->{$accessor} = [grep { $_->{'$id'} ne $obj->id } @{$doc->data->{$accessor} || []}] unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
Condition0%0/2
$doc->data->{$accessor} || []
l!l
00
MC/DC0%0/1
$doc->data->{$accessor} || []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
164 0 ▶ @$cached = grep { $_->id ne $obj->id } @$cached if $cached and !$err;
Branch: 1 branch
BranchTrueFalseTotal
if $cached and not $err 0 0 0
Condition0%0/3
$cached and not $err
!ll&&!rl&&r
000
MC/DC0%0/2
$cached and not $err
$cached not $err
Truth table0%0/3
A and not(B)
A= $cachedB= not $err
ABresult
0X0
100
111
165 0 $doc->$cb($err);
166 },
167 0 );
168  
169 0 return $obj;
170 }
171 1 );
172  
173 1 return $self;
174 }
175  
176 1 ▶sub _monkey_patch_search_method {
Subroutine _monkey_patch_search_method: called CC=1 SCAR=3.5
177 1 my $self = shift;
178 1 my $accessor = $self->accessor;
179  
180 Mojo::Util::monkey_patch(
181 $self->document_class,
182 $self->search_method_name,
183 0 ▶ sub {
Subroutine search_kittens: not called CC=4 SCAR=30.0
184 0 my ($doc, $query, $extra) = @_;
185 0 my $related_model = $self->_related_model;
186  
187 return $related_model->new_collection(
188 $doc->connection,
189 extra => $extra || {},
190 0 ▶ query => {%{$query || {}}, _id => {'$in' => [map { $_->{'$id'} } @{$doc->data->{$accessor} || []}]}},
Condition0%0/6
$extra || {}
l!l
00
$query || {}
l!l
00
$doc->data->{$accessor} || []
l!l
00
MC/DC0%0/1
$extra || {}
$extra
MC/DC0%0/1
$query || {}
$query
MC/DC0%0/1
$doc->data->{$accessor} || []
$doc->data->{$accessor}
Truth table0%0/2
A
A= $extra
Aresult
11
00
Truth table0%0/2
A
A= $query
Aresult
11
00
Truth table0%0/2
A
A= $doc->data->{$accessor}
Aresult
11
00
191 );
192 }
193 1 );
194  
195 1 return $self;
196 }
197  
198 1 1;
199  
200 =encoding utf8
201  
202 =head1 NAME
203  
204 Mandel::Relationship::ListOf - A field points to many other MongoDB documents
205  
206 =head1 DESCRIPTION
207  
208 L<Mandel::Relationship::ListOf> is a class used to describe the relationship
209 where one document has a list of DBRefs that point to other documents.
210 The connection between the documents is described in the database using
211 L<DBRef|http://docs.mongodb.org/manual/reference/database-references/>.
212  
213 This relationship is EXPERIMENTAL. Let me of you are using it or don't like it.
214  
215 =head1 DATABASE STRUCTURE
216  
217 A "person" that has I<list of> "cats" will look like this in the database:
218  
219 mongodb> db.persons.find();
220 {
221 "_id" : ObjectId("5353ab13800fac3a0a8d5049"),
222 "kittens" : [
223 DBRef("cats", ObjectId("5353ab13c5483e16a1010000")),
224 DBRef("cats", ObjectId("5353ab13c5483e16a1020000"))
225 ]
226 }
227  
228 mongodb> db.cats.find();
229 { "_id" : ObjectId("5353ab13c5483e16a1010000") }
230 { "_id" : ObjectId("5353ab13c5483e16a1020000") }
231  
232 =head1 SYNOPSIS
233  
234 =head2 Using DSL
235  
236 package MyModel::Person;
237 use Mandel::Document;
238 list_of cats => 'MyModel::Cat';
239  
240 =head2 Using object oriented interface
241  
242 MyModel::Person->model->relationship(
243 "list_of",
244 "cats",
245 "MyModel::Cat",
246 );
247  
248 See also L<Mandel::Model/relationship>.
249  
250 =head2 Methods generated
251  
252 # non-blocking
253 $person = MyModel::Person->new->push_cats($bson_oid, $pos, sub {
254 my($person, $err, $cat_obj) = @_;
255 # Note! This $cat_obj has only "id()" set
256 # ...
257 });
258  
259 Add the C<$bson_oid> to the "cats" list in C<$person>.
260  
261 $person = MyModel::Person->new->push_cats(\%constructor_args, $pos, sub {
262 my($person, $err, $cat_obj) = @_;
263 # ...
264 });
265  
266 Pushing a new cat with C<%constructor_args> will also insert a new cat object
267 into the database.
268  
269 $person = MyModel::Person->new->push_cats($cat_obj, $pos, sub {
270 my($person, $err, $cat_obj) = @_;
271 # ...
272 });
273  
274 C<$pos> is optional. When omitted, C<push_cats()> will add the new element
275 to the end of list. See
276 L<http://docs.mongodb.org/manual/reference/operator/update/position/#up._S_position>
277 for details.
278  
279 $person = MyModel::Cat->new->remove_cats($bson_oid, sub {
280 my($self, $err) = @_;
281 # Note! This $cat_obj has only "id()" set
282 });
283  
284 $person = MyModel::Cat->new->remove_cats($cat_obj, sub {
285 my($self, $err) = @_;
286 # ...
287 });
288  
289 Calling C<remove_cats()> will only remove the link, and not the related
290 object.
291  
292 $person = MyModel::Cat->new->cats(sub {
293 my($self, $err, $array_of_cats) = @_;
294 # ...
295 });
296  
297 Retrieve all the related cat objects.
298  
299 # blocking
300 $cat_obj = MyModel::Person->new->push_cats($bson_oid);
301 $cat_obj = MyModel::Person->new->push_cats(\%args);
302 $cat_obj = MyModel::Person->new->push_cats($cat_obj);
303 $person = MyModel::Person->new->remove_cats($bson_oid);
304 $person = MyModel::Person->new->remove_cats($cat_obj);
305 $array_of_cats = MyModel::Person->new->cats;
306  
307 $cat_collection = MyModel::Person->new->search_cats;
308  
309 Note! C<search()> does not guaranty the order of the results, like C<cats()>
310 does.
311  
312 =head1 ATTRIBUTES
313  
314 L<Mandel::Relationship::ListOf> inherits all attributes from
315 L<Mandel::Relationship> and implements the following new ones.
316  
317 =head2 push_method_name
318  
319 The name of the method used to add another document to the relationship.
320  
321 =head2 remove_method_name
322  
323 The name of the method used to remove an item from the list.
324  
325 =head2 search_method_name
326  
327 The name of the method used to search related documents.
328  
329 =head1 METHODS
330  
331 L<Mandel::Relationship::ListOf> inherits all methods from
332 L<Mandel::Relationship> and implements the following new ones.
333  
334 =head2 monkey_patch
335  
336 Add methods to L<Mandel::Relationship/document_class>.
337  
338 =head1 SEE ALSO
339  
340 L<Mojolicious>, L<Mango>, L<Mandel::Relationship>
341  
342 =head1 AUTHOR
343  
344 Jan Henning Thorsen - C<jhthorsen@cpan.org>
345  
346 =cut
« Mandel/Relationship/BelongsTo.pm ↑ Summary Mandel/Relationship/HasOne.pm »