Mandel/Document.pm

filter: Statementstmt 62.5% 100 / 160 Branchbran 56.2% 36 / 64 Conditioncond 50.0% 7 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 56.1% 23 / 41 Podpod 100% 12 / 12 totaltotal 60.0% 180 / 300 CC 40 SCAR 20.2
CC
40
Cov
60%
save
30.0
patch
20.0
set
14.4
CRAP 7.5
« Mandel/Collection.pm ↑ Summary andel.pm »
1 package Mandel::Document;
2 15 ▶use Mojo::Base 'Mojo::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
3 15 ▶use Mojo::JSON::Pointer;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 15 ▶use Mojo::Util 'monkey_patch';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 15 ▶use Mandel::Model;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 15 ▶use Mango::BSON ':bson';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 15 ▶use Scalar::Util 'looks_like_number';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 15 ▶use Carp 'confess';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶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 15 15
10  
11 15 my $POINTER = Mojo::JSON::Pointer->new;
12  
13 sub id {
14 10 ▶ my $self = shift;
Subroutine id: called CC=4 SCAR=13.9
Pod documented
15 10 my $raw = $self->data;
16  
17 10 ▶ if (@_) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_) { } 3 7 10
elsif ($raw->{'_id'}) { } 5 2 7
18 3 $self->dirty->{_id} = 1;
19 3 ▶ $raw->{_id} = ref $_[0] ? $_[0] : bson_oid $_[0];
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 2 3
20 3 return $self;
21 }
22 elsif ($raw->{_id}) {
23 5 return $raw->{_id};
24 }
25 else {
26 2 $self->dirty->{_id} = 1;
27 2 return $raw->{_id} = bson_oid;
28 }
29 }
30  
31 0 ▶has connection => sub { confess "connection required in constructor" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
32 0 ▶has model => sub { confess "model required in constructor" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
33 3 ▶has dirty => sub { +{} };
Subroutine __ANON__: called CC=1 SCAR=0.0
34 15 has in_storage => 0;
35  
36 has _storage_collection => sub {
37 0 ▶ my $self = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
38 0 $self->connection->_storage_collection($self->model->collection_name);
39 15 };
40  
41 9 ▶has data => sub { shift->_build_data }; # raw mongodb document data
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 8 ▶sub _build_data { +{} }
Subroutine _build_data: called CC=1 SCAR=0.0
44  
45 sub new {
46 13 ▶ my $self = shift->SUPER::new(@_);
Subroutine new: called CC=2 SCAR=6.9
Pod documented
47 13 ▶ $self->id(delete $self->{id}) if $self->{id};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"id"} 1 12 13
48 13 $self;
49 }
50  
51 1 ▶sub initialize {shift}
Subroutine initialize: called CC=1 SCAR=0.0
Pod documented
52  
53 sub contains {
54 3 ▶ my $self = shift;
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
55 3 $POINTER->data($self->data)->contains(@_);
56 }
57  
58 sub fresh {
59 0 ▶ $_[0]->{fresh} = 1;
Subroutine fresh: not called CC=1 SCAR=6.9
Pod documented
60 0 $_[0];
61 }
62  
63 sub get {
64 2 ▶ my $self = shift;
Subroutine get: called CC=1 SCAR=0.0
Pod documented
65 2 $POINTER->data($self->data)->get(@_);
66 }
67  
68 sub is_changed {
69 2 ▶ return 0 unless $_[0]->{dirty};
Subroutine is_changed: called CC=3 SCAR=11.0
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0]{"dirty"} 1 1 2
70 1 ▶ return 0 unless keys %{$_[0]->{dirty}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$_[0]{"dirty"};} 0 1 1
71 1 return 1;
72 }
73  
74 sub patch {
75 0 ▶ my ($self, $changes, $cb) = @_;
Subroutine patch: not called CC=4 SCAR=30.0
Pod documented
76 0 my $data = $self->data;
77  
78 0 ▶ if ($changes) {
Branch: 1 branch
BranchTrueFalseTotal
if ($changes) 0 0 0
79 0 @$data{keys %$changes} = values %$changes;
80 }
81  
82 0 $data = {%$data};
83 0 delete $data->{_id}; # Mod on _id not allowed
84  
85 $self->_storage_collection->update(
86 {_id => $self->id},
87 {'$set' => $data},
88 {upsert => bson_true},
89 $cb
90 ? (
91 sub {
92 0 ▶ $self->_mark_stored_clean unless $_[1];
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[1] 0 0 0
93 0 $self->$cb($_[1]);
94 }
95 )
96 0 ▶ : (),
Branch: 1 branch
BranchTrueFalseTotal
$cb ? : 0 0 0
97 );
98  
99 0 ▶ $self->_mark_stored_clean unless $cb;
Branch: 1 branch
BranchTrueFalseTotal
unless $cb 0 0 0
100 0 $self;
101 }
102  
103 sub remove {
104 0 ▶ my ($self, $cb) = @_;
Subroutine remove: not called CC=2 SCAR=17.9
Pod documented
105 0 my $c = $self->_storage_collection;
106 0 my @args = ({_id => $self->id}, {single => 1});
107  
108 0 warn "[$self\::remove] @{[$self->id]}\n" if DEBUG;
109  
110 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 0 0
111 $c->remove(
112 @args,
113 sub {
114 0 ▶ my ($collection, $err, $doc) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
115 0 ▶ $self->_mark_removed_dirty unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
116 0 $self->$cb($err);
117 }
118 0 );
119 }
120 else {
121 0 $c->remove(@args);
122 0 $self->_mark_removed_dirty;
123 }
124  
125 0 return $self;
126 }
127  
128 sub _mark_removed_dirty {
129 0 ▶ my $self = shift;
Subroutine _mark_removed_dirty: not called CC=2 SCAR=17.9
130 0 $self->dirty->{$_} = 1 for keys %{$self->data};
131 0 $self->in_storage(0);
132 }
133  
134 sub save {
135 0 ▶ my ($self, $cb) = @_;
Subroutine save: not called CC=5 SCAR=34.0
Pod documented
136  
137 0 ▶ if (!$self->is_changed and $self->in_storage) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->is_changed and $self->in_storage) 0 0 0
Condition0%0/3
not $self->is_changed and $self->in_storage
!ll&&!rl&&r
000
MC/DC0%0/2
not $self->is_changed and $self->in_storage
not $self->is_changed $self->in_storage
Truth table0%0/3
not(A) and B
A= not $self->is_changedB= $self->in_storage
ABresult
0X0
100
111
138 0 ▶ $self->$cb('') if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
139 0 return $self;
140 }
141  
142 0 $self->id; # make sure we have an ObjectID
143  
144 0 warn "[$self\::save] ", Data::Dumper->new([$self->data])->Indent(1)->Sortkeys(1)->Terse(1)->Dump if DEBUG;
145 0 my $c = $self->_storage_collection;
146  
147 0 ▶ if ($cb) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cb) { } 0 0 0
148 $c->save(
149 $self->data,
150 sub {
151 0 ▶ my ($collection, $err, $doc) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
152 0 ▶ $self->_mark_stored_clean unless $err;
Branch: 1 branch
BranchTrueFalseTotal
unless $err 0 0 0
153 0 $self->$cb($err);
154 }
155 0 );
156 }
157 else {
158 0 $c->save($self->data);
159 0 $self->_mark_stored_clean;
160 }
161  
162 0 return $self;
163 }
164  
165 sub _mark_stored_clean {
166 0 ▶ my $self = shift;
Subroutine _mark_stored_clean: not called CC=1 SCAR=6.9
167 0 delete $self->{dirty};
168 0 $self->in_storage(1);
169 }
170  
171 sub set {
172 8 ▶ my ($self, $pointer, $val) = @_;
Subroutine set: called CC=14 SCAR=26.6
Pod documented
173 8 my $raw = $self->data;
174 8 my (@path, $field);
175  
176 8 ▶ return $self unless $pointer =~ s!^/!!;
Branch: 1 branch
BranchTrueFalseTotal
unless $pointer =~ s[^/][]u 0 8 8
177 8 @path = split '/', $pointer;
178 8 $field = $path[0];
179  
180 8 ▶ while (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) 26 5 31
181 26 my $p = shift @path;
182 26 my $type = ref $raw;
183 26 ▶ my $want = looks_like_number $p ? 'INDEX' : 'KEY';
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($p) ? : 8 18 26
184  
185 26 ▶ if ($type eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 16 10 26
elsif ($type eq 'ARRAY') { } 8 2 10
186 16 ▶ if (@path) {
Branch: 1 branch
BranchTrueFalseTotal
if (@path) { } 13 3 16
187 13 ▶ $raw = $raw->{$p} ||= looks_like_number $path[0] ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($path[0]) ? : 1 0 1
Condition66%2/3
$raw->{$p} ||= &looks_like_number($path[0]) ? [] : {}
l!l&&r!l&&!r
1210
MC/DC0%0/2
$raw->{$p} ||= &looks_like_number($path[0]) ? [] : {}
$raw->{$p} &looks_like_number($path[0]) ? [] : {}
Truth table66%2/3
A ||= B
A= $raw->{$p}B= &looks_like_number($path[0]) ? [] : {}
ABresult
1X1
011
000
188 }
189 else {
190 3 $raw->{$p} = $val;
191 }
192 }
193 elsif ($type eq 'ARRAY') {
194 8 ▶ if ($want ne 'INDEX') {
Branch: 2 branches
BranchTrueFalseTotal
if ($want ne 'INDEX') { } 1 7 8
elsif (@path) { } 5 2 7
195 1 confess "Cannot set $want in $type for /$pointer ($p)";
196 }
197 elsif (@path) {
198 5 ▶ $raw = $raw->[$p] ||= looks_like_number $path[0] ? [] : {};
Branch: 1 branch
BranchTrueFalseTotal
&looks_like_number($path[0]) ? : 0 2 2
Condition66%2/3
$raw->[$p] ||= &looks_like_number($path[0]) ? [] : {}
l!l&&r!l&&!r
320
MC/DC0%0/2
$raw->[$p] ||= &looks_like_number($path[0]) ? [] : {}
$raw->[$p] &looks_like_number($path[0]) ? [] : {}
Truth table66%2/3
A ||= B
A= $raw->[$p]B= &looks_like_number($path[0]) ? [] : {}
ABresult
1X1
011
000
199 }
200 else {
201 2 $raw->[$p] = $val;
202 }
203 }
204 else {
205 2 confess "Cannot set $want in SCALAR for /$pointer ($p)";
206 }
207 }
208  
209 5 ▶ $self->dirty->{$field} = 1 if defined $field;
Branch: 1 branch
BranchTrueFalseTotal
if defined $field 5 0 5
210 5 $self;
211 }
212  
213 sub import {
214 13 ▶ my $class = shift;
Subroutine import: called CC=6 SCAR=18.1
215 13 ▶ my %args = @_ == 1 ? (name => shift) : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 3 10 13
216 13 my $caller = caller;
217 13 my $model = Mandel::Model->new(document_class => $caller, %args);
218 13 my $base_class = 'Mandel::Document';
219  
220 13 for (qw(name extends)) {
221 26 ▶ if ($args{$_} and $args{$_} =~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{$_} and $args{$_} =~ /::/u) 1 25 26
Condition100%3/3
$args{$_} and $args{$_} =~ /::/u
!ll&&!rl&&r
2321
MC/DC100%2/2
$args{$_} and $args{$_} =~ /::/u
$args{$_} $args{$_} =~ /::/u
Truth table100%3/3
A and B
A= $args{$_}B= $args{$_} =~ /::/u
ABresult
0X0
100
111
222 1 $base_class = delete $args{$_};
223 }
224 }
225 13 ▶ if (!$args{name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"name"}) 11 2 13
226 11 $args{name} = Mojo::Util::decamelize(($caller =~ /(\w+)$/)[0]);
227 11 $model->name($args{name});
228 }
229  
230 13 ▶ monkey_patch $caller, belongs_to => sub { $model->relationship(belongs_to => @_)->monkey_patch };
Subroutine __ANON__: not called CC=1 SCAR=0.4
Subroutine belongs_to: called CC=1 SCAR=0.4
231 13 ▶ monkey_patch $caller, field => sub { $model->field(shift, {@_}) };
Subroutine __ANON__: called CC=1 SCAR=0.0
232 13 ▶ monkey_patch $caller, has_many => sub { $model->relationship(has_many => @_)->monkey_patch };
Subroutine __ANON__: not called CC=1 SCAR=1.2
233 13 ▶ monkey_patch $caller, has_one => sub { $model->relationship(has_one => @_)->monkey_patch };
Subroutine __ANON__: not called CC=1 SCAR=2.6
Subroutine has_one: not called CC=1 SCAR=2.6
234 13 ▶ monkey_patch $caller, list_of => sub { $model->relationship(list_of => @_)->monkey_patch };
Subroutine __ANON__: not called CC=1 SCAR=1.2
235 13 ▶ monkey_patch $caller, model => sub {$model};
Subroutine __ANON__: called CC=1 SCAR=0.0
236  
237 13 @_ = ($class, $base_class);
238 13 goto &Mojo::Base::import;
239 }
240  
241 1 ▶sub TO_JSON { shift->data }
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod documented
242  
243 sub _cache {
244 0 ▶ my $self = shift;
Subroutine _cache: not called CC=3 SCAR=24.8
245 0 ▶ my $cache = $self->{cache} ||= {};
Condition0%0/2
$self->{"cache"} ||= {}
l!l
00
MC/DC0%0/1
$self->{"cache"} ||= {}
$self->{"cache"}
Truth table0%0/2
A
A= $self->{"cache"}
Aresult
11
00
246  
247 0 ▶ return $cache->{$_[0]} if @_ == 1; # get
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 0 0 0
248 0 return $cache->{$_[0]} = $_[1]; # set
249 }
250  
251 15 1;
252  
253 =encoding utf8
254  
255 =head1 NAME
256  
257 Mandel::Document - A single MongoDB document with logic
258  
259 =head1 SYNOPSIS
260  
261 Extend a class with C<MyDocument::Class> instead of L<Mandel::Document>:
262  
263 package MyModel::Person;
264 use Mandel::Document "MyDocument::Class";
265  
266 Specify a default collection name, instead of the
267 L<default|Mandel::Model/collection>. L</import> will think you meant a base
268 class, if this argument contains "::".
269  
270 package MyModel::Person;
271 use Mandel::Document "some_collection_name";
272 use Types::Standard 'Str';
273  
274 field "foo";
275  
276 field "foo" => (
277 isa => Str,
278 builder => sub {
279 my $self = shift;
280 return "default value";
281 },
282 );
283  
284  
285 Spell out the options with a list:
286  
287 package MyModel::Person;
288  
289 use Mandel::Document (
290 extends => "My::Document::Class",
291 collection_name => "some_collection_name",
292 collection_class => "My::Custom::Collection",
293 );
294  
295 =head1 DESCRIPTION
296  
297 L<Mandel> is a simplistic model layer using the L<Mango> module to interact
298 with a MongoDB backend. The L<Mandel> class defines the overall model,
299 including high level interaction. Individual results, called Types inherit
300 from L<Mandel::Document>.
301  
302 An object of this class gets automatically serialized by L<Mojo::JSON>.
303 See L</TO_JSON> and L<Mojo::JSON#DESCRIPTION> for details.
304  
305 Example:
306  
307 use Mojolicious::Lite;
308 # ...
309 get '/some/resource' => sub {
310 my $c = shift;
311 # find some document...
312 $c->render(json => $mandel_doc_object);
313 };
314  
315 =head1 ATTRIBUTES
316  
317 L<Mandel> inherits all attributes from L<Mojo::Base> and implements the
318 following new ones.
319  
320 =head2 id
321  
322 $object_id = $self->id;
323 $self = $self->id("507f1f77bcf86cd799439011");
324 $self = $self->id(Mango::BSON::ObjectID->new);
325  
326 Returns the L<Mango::BSON::ObjectID> object for this document.
327 Will create one if it does not already exist.
328  
329 This can field can also be set.
330  
331 =head2 data
332  
333 $hash = $self->data;
334 $self = $self->data($hash);
335  
336 Holds the raw mongodb document. It is possible to define default values for
337 this attribute by defining L<builder|Mandel::Model::Field/builder> for the
338 fields.
339  
340 =head2 in_storage
341  
342 Boolean true if this document has been fetched from storage or L<saved|/save>
343 to storage.
344  
345 =head2 connection
346  
347 An instance of L<Mandel>. This is required.
348  
349 =head2 model
350  
351 Returns a L<Mandel::Model> object. This object is a class variable and
352 therefor shared between all instances.
353  
354 =head2 dirty
355  
356 This attribute holds a hash-ref where the keys are name of fields that has
357 been updated or otherwise not stored in database.
358  
359 TODO: Define what the values should hold. Timestamp? A counter for how
360 many times the field has been updated before saved..?
361  
362 =head1 METHODS
363  
364 L<Mandel::Document> inherits all of the methods from L<Mojo::Base> and
365 implements the following new ones.
366  
367 =head2 new
368  
369 Constructs a new object.
370  
371 =head2 initialize
372  
373 A no-op placeholder useful for initialization. See L<Mandel/initialize>.
374  
375 =head2 contains
376  
377 $bool = $self->contains('/json/2/pointer');
378  
379 Use L<Mojo::JSON::Pointer/contains> to check if a value exists inside the raw
380 mongodb document.
381  
382 =head2 fresh
383  
384 $self = $self->fresh;
385  
386 Calling this method will force the next relationship call to return fresh
387 data from database instead of cached. Example:
388  
389 $self->fresh->cats(sub {
390 my($self, $err, $cats) = @_;
391 });
392  
393 =head2 get
394  
395 $any = $self->get('/json/2/pointer');
396  
397 Use L<Mojo::JSON::Pointer/get> to retrieve a value inside the raw mongodb
398 document.
399  
400 =head2 is_changed
401  
402 Returns true if L</dirty> contains any field names.
403  
404 =head2 patch
405  
406 $self = $self->patch(\%changes, sub { my($self, $err) = @_ });
407 $self = $self->patch(\%changes);
408  
409 This method will insert/update a partial document. This is useful if C</data>
410 does not contain a complete document.
411  
412 It will also insert the document if a document with L</id> does not already
413 exist.
414  
415 =head2 remove
416  
417 $self = $self->remove(sub { my($self, $err) = @_; });
418 $self = $self->remove;
419  
420 Will remove this object from the L</collection> and set mark
421 all fields as L</dirty>.
422  
423 =head2 save
424  
425 $self = $self->save(sub { my($self, $err) = @_; });
426 $self = $self->save;
427  
428 This method stores the raw data in the database and collection. It clear
429 the L</dirty> attribute.
430  
431 NOTE: This method will call the callback (with $err set to empty string)
432 immediately unless L</is_changed> is true and L</in_storage> is false.
433  
434 =head2 set
435  
436 $self = $self->set('/json/2/pointer', $val);
437  
438 Use a JSON pointer to set data in the raw mongodb document. This method will
439 die if the pointer points to non-compatible data.
440  
441 =head2 import
442  
443 See L</SYNOPSIS>.
444  
445 =head2 TO_JSON
446  
447 Alias for L</data>.
448  
449 This method allow the document to get automatically serialized by
450 L<Mojo::JSON>.
451  
452 =head1 SEE ALSO
453  
454 L<Mojolicious>, L<Mango>, L<Mandel>
455  
456 =head1 AUTHOR
457  
458 Jan Henning Thorsen - C<jhthorsen@cpan.org>
459  
460 =cut
« Mandel/Collection.pm ↑ Summary andel.pm »