Mandel/Relationship/HasMany.pm

filter: Statementstmt 45.4% 30 / 66 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 60.0% 9 / 15 Podpod 100% 1 / 1 totaltotal 33.9% 40 / 118 CC 15 SCAR 27.2
CC
15
Cov
31%
cats
30.0
add_cats
30.0
__ANON__
20.0
CRAP 15.2
« Mandel/Relationship/HasOne.pm ↑ Summary Mandel/Collection.pm »
1 package Mandel::Relationship::HasMany;
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 add_method_name => sub { sprintf 'add_%s', shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
7 1 ▶has search_method_name => sub { sprintf 'search_%s', shift->accessor };
Subroutine __ANON__: called CC=1 SCAR=0.0
8  
9 1 ▶sub monkey_patch {
Subroutine monkey_patch: called CC=1 SCAR=0.0
Pod documented
10 1 shift->_monkey_patch_all_method->_monkey_patch_add_method->_monkey_patch_search_method;
11 }
12  
13 1 ▶sub _monkey_patch_all_method {
Subroutine _monkey_patch_all_method: called CC=1 SCAR=4.3
14 1 my $self = shift;
15 1 my $search = $self->search_method_name;
16 1 my $accessor = $self->accessor;
17  
18 Mojo::Util::monkey_patch(
19 $self->document_class,
20 $accessor,
21 0 ▶ sub {
Subroutine cats: not called CC=5 SCAR=34.0
22 0 my ($doc, $cb) = @_;
23 0 ▶ my $cached = delete $doc->{fresh} ? undef : $doc->_cache($accessor);
Branch: 1 branch
BranchTrueFalseTotal
delete $doc->{'fresh'} ? : 0 0 0
24  
25 # Blocking
26 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
27 0 ▶ return $cached if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
28 0 return $doc->_cache($accessor => $doc->$search->all);
29 }
30  
31 0 ▶ if ($cached) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cached) { } 0 0 0
32 0 $doc->$cb('', $cached);
33 }
34 else {
35 $doc->$search->all(
36 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
37 0 my ($collection, $err, $objs) = @_;
38 0 ▶ $doc->_cache($accessor => $objs) unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
39 0 $doc->$cb($err, $objs);
40 }
41 0 );
42 }
43  
44 0 return $doc;
45 }
46 1 );
47  
48 1 return $self;
49 }
50  
51 1 ▶sub _monkey_patch_add_method {
Subroutine _monkey_patch_add_method: called CC=1 SCAR=5.2
52 1 my $self = shift;
53 1 my $foreign_field = $self->foreign_field;
54 1 my $accessor = $self->accessor;
55  
56 Mojo::Util::monkey_patch(
57 $self->document_class,
58 $self->add_method_name,
59 0 ▶ sub {
Subroutine add_cats: not called CC=5 SCAR=34.0
60 0 my ($doc, $obj, $cb) = @_;
61 0 my $cached = $doc->_cache($accessor);
62  
63 0 ▶ if (ref $obj eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj eq "HASH") 0 0 0
64 0 $obj = $self->_related_model->new_collection($doc->connection)->create($obj);
65 }
66  
67 0 $obj->data->{$foreign_field} = bson_dbref $doc->model->collection_name, $doc->id;
68 0 ▶ $obj->dirty->{$foreign_field} = 1 if $obj->dirty;
Branch: 1 branch
BranchTrueFalseTotal
if $obj->dirty 0 0 0
69  
70 # Blocking
71 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
72 0 ▶ push @$cached, $obj if $cached;
Branch: 1 branch
BranchTrueFalseTotal
if $cached 0 0 0
73 0 $obj->save;
74 0 $doc->save;
75 0 return $obj;
76 }
77  
78 # Non-blocking
79 Mojo::IOLoop->delay(
80 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 0 my ($delay) = @_;
82 0 $obj->save($delay->begin);
83 0 $doc->save($delay->begin);
84 },
85 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
86 0 my ($delay, $o_err, $d_err) = @_;
87 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
88 0 ▶ push @$cached, $obj if !$err and $cached;
Branch: 1 branch
BranchTrueFalseTotal
if not $err and $cached 0 0 0
Condition0%0/3
not $err and $cached
!ll&&!rl&&r
000
MC/DC0%0/2
not $err and $cached
not $err $cached
Truth table0%0/3
not(A) and B
A= not $errB= $cached
ABresult
0X0
100
111
89 0 $doc->$cb($err, $obj);
90 },
91 0 );
92  
93 0 return $doc;
94 }
95 1 );
96  
97 1 return $self;
98 }
99  
100 1 ▶sub _monkey_patch_search_method {
Subroutine _monkey_patch_search_method: called CC=1 SCAR=2.1
101 1 my $self = shift;
102 1 my $foreign_field = $self->foreign_field;
103 1 my $related_class = $self->related_class;
104  
105 Mojo::Util::monkey_patch(
106 $self->document_class,
107 $self->search_method_name,
108 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
109 0 my ($doc, $query, $extra) = @_;
110 0 my $related_model = $self->_related_model;
111  
112 return $related_model->new_collection(
113 $doc->connection,
114 extra => $extra || {},
115 0 ▶ query => {%{$query || {}}, sprintf('%s.$id', $foreign_field) => $doc->id},
Condition0%0/4
$extra || {}
l!l
00
$query || {}
l!l
00
MC/DC0%0/1
$extra || {}
$extra
MC/DC0%0/1
$query || {}
$query
Truth table0%0/2
A
A= $extra
Aresult
11
00
Truth table0%0/2
A
A= $query
Aresult
11
00
116 );
117 }
118 1 );
119  
120 1 return $self;
121 }
122  
123 1 1;
124  
125 =encoding utf8
126  
127 =head1 NAME
128  
129 Mandel::Relationship::HasMany - A field relates to many other mongodb document
130  
131 =head1 DESCRIPTION
132  
133 L<Mandel::Relationship::HasMany> is a class used to describe the relationship
134 between one document that has a relationship to many other documents.
135 The connection between the documents is described in the database using
136 L<DBRef|http://docs.mongodb.org/manual/reference/database-references/>.
137  
138 =head1 DATABASE STRUCTURE
139  
140 A "person" that I<has many> "cats" will look like this in the database:
141  
142 mongodb# db.persons.find();
143 { "_id" : ObjectId("53529f28c5483e4977020000") }
144  
145 mongodb# db.cats.find({ "person.$id": ObjectId("53529f28c5483e4977020000") })
146 {
147 "_id" : ObjectId("53529f28c5483e5077040000"),
148 "person" : DBRef("persons", ObjectId("53529f28c5483e4977020000"))
149 }
150 {
151 "_id" : ObjectId("6432574384483e4978010000"),
152 "person" : DBRef("persons", ObjectId("53529f28c5483e4977020000"))
153 }
154  
155 A "has many" on one side is L<Mandel::Relationship::BelongsTo> on the other
156 side.
157  
158 =head1 SYNOPSIS
159  
160 =head2 Using DSL
161  
162 package MyModel::Person;
163 use Mandel::Document;
164 has_many cats => 'MyModel::Cat';
165  
166 =head2 Using object oriented interface
167  
168 MyModel::Person->model->relationship(
169 "has_many",
170 "cats",
171 "MyModel::Cat",
172 );
173  
174 See also L<Mandel::Model/relationship>.
175  
176 =head2 Methods generated
177  
178 # non-blocking
179 $person = MyModel::Person->new->add_cats(\%constructor_args, sub {
180 my($person, $err, $cat_obj) = @_;
181 # ...
182 });
183  
184 $person = MyModel::Person->new->add_cats($cat_obj, sub {
185 my($person, $err, $cat_obj) = @_;
186 # ...
187 });
188  
189 $person = MyModel::Cat->new->cats(sub {
190 my($self, $err, $array_of_cats) = @_;
191 # ...
192 });
193  
194 # blocking
195 $cat_obj = MyModel::Person->new->add_cats(\%args);
196 $cat_obj = MyModel::Person->new->add_cats($cat_obj);
197 $array_of_cats = MyModel::Person->new->cats;
198  
199 $cat_collection = MyModel::Person->new->search_cats;
200  
201 =head1 ATTRIBUTES
202  
203 L<Mandel::Relationship::HasMany> inherits all attributes from
204 L<Mandel::Relationship> and implements the following new ones.
205  
206 =head2 add_method_name
207  
208 The name of the method used to add another document to the relationship.
209  
210 =head2 search_method_name
211  
212 The name of the method used to search related documents.
213  
214 =head1 METHODS
215  
216 L<Mandel::Relationship::HasMany> inherits all methods from
217 L<Mandel::Relationship> and implements the following new ones.
218  
219 =head2 monkey_patch
220  
221 Add methods to L<Mandel::Relationship/document_class>.
222  
223 =head1 SEE ALSO
224  
225 L<Mojolicious>, L<Mango>, L<Mandel::Relationship>
226  
227 =head1 AUTHOR
228  
229 Jan Henning Thorsen - C<jhthorsen@cpan.org>
230  
231 =cut
« Mandel/Relationship/HasOne.pm ↑ Summary Mandel/Collection.pm »