Mandel/Collection.pm

filter: Statementstmt 32.4% 37 / 114 Branchbran 17.6% 6 / 34 Conditioncond 13.3% 2 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 35.7% 10 / 28 Podpod 100% 10 / 10 totaltotal 31.4% 66 / 210 CC 25 SCAR 22.0
CC
25
Cov
28%
patch
30.0
_new_cursor
20.0
remove
12.0
CRAP 9.0
« Mandel/Relationship/HasMany.pm ↑ Summary Mandel/Document.pm »
1 package Mandel::Collection;
2 20 ▶use Mojo::Base -base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 20 ▶use Mandel::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use Mango::BSON ':bson';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Scalar::Util 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 20 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use constant DEBUG => $ENV{MANDEL_CURSOR_DEBUG} ? eval 'require Data::Dumper;1' : 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'MANDEL_CURSOR_DEBUG'} ? : 0 20 20
8  
9 1 ▶has connection => sub { confess "connection required in constructor" };
Subroutine __ANON__: called CC=1 SCAR=0.0
10 2 ▶has model => sub { confess "model required in constructor" };
Subroutine __ANON__: called CC=1 SCAR=0.0
11  
12 0 ▶has _storage_collection => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
13 0 my $self = shift;
14 0 $self->connection->_storage_collection($self->model->collection_name);
15 20 };
16  
17 0 ▶sub all {
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
18 0 my ($self, $cb) = @_;
19  
20 0 my $c = $self->_new_cursor;
21 0 ▶ return [map { $self->_new_document($_, 1) } @{$c->all}] unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
22  
23 $c->all(
24 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 0 my ($cursor, $err, $docs) = @_;
26 0 return $self->$cb($err, [map { $self->_new_document($_, 1) } @$docs]);
27 }
28 0 );
29 0 return $self;
30 }
31  
32 6 ▶sub create {
Subroutine create: called CC=3 SCAR=11.1
Pod documented
33 6 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 6 6
34 6 my $self = shift;
35  
36 6 ▶ $self->_new_document(shift || undef, 0)->validate_fields;
Condition100%2/2
shift() || undef
l!l
42
MC/DC100%1/1
shift() || undef
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
37 }
38  
39 0 ▶sub count {
Subroutine count: not called CC=2 SCAR=17.9
Pod documented
40 0 my ($self, $cb) = @_;
41  
42 0 my $c = $self->_new_cursor;
43 0 ▶ return $c->count unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
44  
45 0 ▶ $c->count(sub { shift; $self->$cb(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
46 0 return $self;
47 }
48  
49 0 ▶sub distinct {
Subroutine distinct: not called CC=2 SCAR=17.9
Pod documented
50 0 my ($self, $field, $cb) = @_;
51  
52 0 my $c = $self->_new_cursor;
53 0 ▶ return $c->distinct($field) unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
54  
55 0 ▶ $c->distinct(sub { shift; $self->$cb(@_) });
Subroutine __ANON__: not called CC=1 SCAR=6.9
56 0 return $self;
57 }
58  
59 0 ▶sub iterator {
Subroutine iterator: not called CC=1 SCAR=6.9
Pod documented
60 0 return Mandel::Iterator->new(cursor => $_[0]->_new_cursor, model => $_[0]->model);
61 }
62  
63 0 ▶sub patch {
Subroutine patch: not called CC=5 SCAR=34.0
Pod documented
64 0 my ($self, $changes, $cb) = @_;
65 0 my $extra = $self->{extra};
66  
67 warn '[Mandel::Collection::patch] ',
68 0 Data::Dumper->new([$changes, $self->{query}, $extra])->Indent(1)->Sortkeys(1)->Terse(1)->Dump
69 if DEBUG;
70 $self->_storage_collection->update(
71 $self->{query} || {},
72 {'$set' => $changes},
73 {upsert => $extra->{upsert} // bson_false, multi => $extra->{multi} // bson_true},
74 0 ▶ $cb ? (sub { $self->$cb($_[1]) }) : (),
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
Condition0%0/8
$self->{'query'} || {}
l!l
00
$extra->{"upsert"} // bson_false()
l!l&&r!l&&!r
000
$extra->{"multi"} // bson_true()
l!l&&r!l&&!r
000
MC/DC0%0/1
$self->{'query'} || {}
$self->{'query'}
MC/DC0%0/2
$extra->{"upsert"} // bson_false()
$extra->{"upsert"} bson_false()
MC/DC0%0/2
$extra->{"multi"} // bson_true()
$extra->{"multi"} bson_true()
Truth table0%0/2
A
A= $self->{'query'}
Aresult
11
00
Truth table0%0/3
A // B
A= $extra->{"upsert"}B= bson_false()
ABresult
1X1
011
000
Truth table0%0/3
A // B
A= $extra->{"multi"}B= bson_true()
ABresult
1X1
011
000
75 );
76  
77 0 $self;
78 }
79  
80 0 ▶sub remove {
Subroutine remove: not called CC=3 SCAR=24.8
Pod documented
81 0 ▶ my $cb = ref $_[-1] eq 'CODE' ? pop : undef;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[-1] eq 'CODE' ? : 0 0 0
82 0 my $self = shift;
83 0 my $c = $self->_storage_collection;
84 0 my @args = $self->{query};
85  
86 0 warn '[Mandel::Collection::remove] ', Data::Dumper->new([$self->{query}])->Indent(1)->Sortkeys(1)->Terse(1)->Dump
87 if DEBUG;
88 0 ▶ push @args, sub { $self->$cb($_[1]) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
89 0 ▶ if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
90  
91 0 $c->remove(@args);
92 0 $self;
93 }
94  
95 0 ▶sub save {
Subroutine save: not called CC=3 SCAR=24.8
Pod documented
96 0 my ($self, $raw, $cb) = @_;
97 0 my $c = $self->_storage_collection;
98  
99 0 ▶ $raw->{_id} ||= bson_oid;
Condition0%0/3
$raw->{"_id"} ||= bson_oid()
l!l&&r!l&&!r
000
MC/DC0%0/2
$raw->{"_id"} ||= bson_oid()
$raw->{"_id"} bson_oid()
Truth table0%0/3
A ||= B
A= $raw->{"_id"}B= bson_oid()
ABresult
1X1
011
000
100  
101 0 warn '[Mandel::Collection::save] ', Data::Dumper->new([$raw])->Indent(1)->Sortkeys(1)->Terse(1)->Dump if DEBUG;
102  
103 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
104 0 $c->save($raw);
105 0 return $self->_new_document($raw, 1);
106 }
107  
108 $c->save(
109 $raw,
110 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
111 0 my ($collection, $err, $doc) = @_;
112 0 $self->$cb($err, $self->_new_document($raw, 1));
113 }
114 0 );
115  
116 0 return $self;
117 }
118  
119 0 ▶sub search {
Subroutine search: not called CC=3 SCAR=24.8
Pod documented
120 0 my ($self, $query, $extra) = @_;
121 0 my $class = blessed $self;
122 0 my $clone = $class->new(%$self);
123  
124 0 ▶ @{$clone->{extra}}{keys %$extra} = values %$extra if $extra;
Branch: 1 branch
BranchTrueFalseTotal
if $extra 0 0 0
125 0 ▶ @{$clone->{query}}{keys %$query} = values %$query if $query;
Branch: 1 branch
BranchTrueFalseTotal
if $query 0 0 0
126 0 $clone;
127 }
128  
129 0 ▶sub single {
Subroutine single: not called CC=3 SCAR=24.8
Pod documented
130 0 my ($self, $cb) = @_;
131  
132 0 my $c = $self->_new_cursor->limit(-1);
133 0 ▶ unless ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cb) 0 0 0
134 0 ▶ my $doc = $c->next or return;
Branch: 1 branch
BranchTrueFalseTotal
my $doc = $c->next or return 0 0 0
135 0 return $self->_new_document($doc, 1);
136 }
137  
138 $c->next(
139 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
140 0 my ($cursor, $err, $doc) = @_;
141 0 ▶ $self->$cb($err, $doc ? $self->_new_document($doc, 1) : undef);
Branch: 1 branch
BranchTrueFalseTotal
$doc ? : 0 0 0
142 }
143 0 );
144 0 return $self;
145 }
146  
147 0 ▶sub _new_cursor {
Subroutine _new_cursor: not called CC=4 SCAR=30.0
148 0 my $self = shift;
149 0 ▶ my $extra = $self->{extra} || {};
Condition0%0/2
$self->{'extra'} || {}
l!l
00
MC/DC0%0/1
$self->{'extra'} || {}
$self->{'extra'}
Truth table0%0/2
A
A= $self->{'extra'}
Aresult
11
00
150 0 my $cursor = $self->_storage_collection->find;
151  
152 0 ▶ $cursor->query($self->{query}) if $self->{query};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"query"} 0 0 0
153 0 $cursor->$_($extra->{$_}) for keys %$extra;
154  
155 0 if (DEBUG) {
156 local $cursor->{collection}{db} = $cursor->{collection}{db}{name}; # hide big data structure
157 warn '[', +(caller 1)[3], '] ', Data::Dumper->new([$cursor])->Indent(1)->Sortkeys(1)->Terse(1)->Dump;
158 }
159  
160 0 $cursor;
161 }
162  
163 6 ▶sub _new_document {
Subroutine _new_document: called CC=3 SCAR=11.0
164 6 my ($self, $doc, $from_storage) = @_;
165 6 my $model = $self->model;
166 5 my @extra;
167  
168 5 ▶ if ($doc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doc) 4 1 5
169 4 push @extra, data => $doc;
170 4 push @extra, dirty => {map { $_, 1 } keys %$doc};
171 }
172 5 ▶ if (my $connection = $self->{connection}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $connection = $self->{"connection"}) 2 3 5
173 2 push @extra,
174 connection => $connection,
175 ;
176 }
177  
178 5 $model->document_class->new(model => $model, in_storage => $from_storage, @extra);
179 }
180  
181 20 1;
182  
183 =encoding utf8
184  
185 =head1 NAME
186  
187 Mandel::Collection - A collection of Mandel documents
188  
189 =head1 SYNOPSIS
190  
191 my $connection = MyModel->connect("mongodb://localhost/my_db");
192 my $persons = $connection->collection("person");
193  
194 $persons->count(sub {
195 my($persons, $err, $int) = @_;
196 });
197  
198 # ...
199  
200 =head1 DESCRIPTION
201  
202 This class is used to describe a group of mongodb documents.
203  
204 =head1 ATTRIBUTES
205  
206 =head2 connection
207  
208 An object that inherit from L<Mandel>.
209  
210 =head2 model
211  
212 An object that inherit from L<Mandel::Model>.
213  
214 =head1 METHODS
215  
216 =head2 all
217  
218 $self = $self->all(sub { my($self, $err, $docs) = @_; });
219 $docs = $self->all;
220  
221 Retrieves all documents from the database that match the given L</search>
222 query.
223  
224 =head2 create
225  
226 $document = $self->create;
227 $document = $self->create(\%args);
228  
229 Returns a new object of a given type. This object is NOT inserted into the
230 mongodb collection. You need to call L<Mandel::Document/save> for that to
231 happen.
232  
233 C<%args> is used to set the fields in the new document, NOT the attributes.
234  
235 =head2 count
236  
237 $self = $self->count(sub { my($self, $err, $int) = @_; });
238 $int = $self->count;
239  
240 Used to count how many documents the current L</search> query match.
241  
242 =head2 distinct
243  
244 $self = $self->distinct("field_name", sub { my($self, $err, $values) = @_; });
245 $values = $self->distinct("field_name");
246  
247 Get all distinct values for key in this collection.
248  
249 =head2 iterator
250  
251 $iterator = $self->iterator;
252  
253 Returns a L<Mandel::Iterator> object based on the L</search> performed.
254  
255 =head2 patch
256  
257 $self = $self->patch(\%changes, sub { my($self, $err, $doc) = @_ });
258 $self = $self->patch(\%changes);
259  
260 This method can be used to add C<%changes> to multiple documents
261 in the collection. Which documents to update will be decided by the
262 C<%query> given to L</search>.
263  
264 C<%extra> arguments default to:
265  
266 =over 4
267  
268 =item * upsert: false
269  
270 =item * multi: true
271  
272 =back
273  
274 =head2 remove
275  
276 $self = $self->remove(sub { my($self, $err) = @_; });
277 $self = $self->remove;
278  
279 Remove the documents that query given to L</search>.
280  
281 =head2 save
282  
283 $self = $self->save(\%document, sub { my($self, $err, $doc) = @_; );
284 $doc = $self->save(\%document);
285  
286 Used to save a document. The callback receives a L<Mandel::Document>.
287  
288 =head2 search
289  
290 $clone = $self->search(\%query, \%extra);
291  
292 Return a clone of the given collection, but with different C<%search> and
293 C<%extra> parameters. You can chain these calls to make the query more
294 precise.
295  
296 C<%extra> will be used to set extra parameters on the L<Mango::Cursor>, where
297 all the keys need to match the L<Mango::Cursor/ATTRIBUTES>.
298  
299 =head2 single
300  
301 $self = $self->single(sub { my($self, $err, $doc) = @_; });
302 $doc = $self->single;
303  
304 Will return the first object found in the callback, matching the given
305 C<%search> query.
306  
307 =head1 SEE ALSO
308  
309 L<Mojolicious>, L<Mango>, L<Mandel>
310  
311 =head1 AUTHOR
312  
313 Jan Henning Thorsen - C<jhthorsen@cpan.org>
314  
315 =cut
« Mandel/Relationship/HasMany.pm ↑ Summary Mandel/Document.pm »