Mongoose/Engine.pm

filter: Statementstmt 9.7% 23 / 235 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 50 MC/DCmcdc 0.0% 0 / 30 Subroutinesub 20.0% 6 / 30 Podpod 100% 14 / 14 totaltotal 8.4% 43 / 507 CC 103 SCAR 59.9
CC
103
Cov
5%
expand
1260.0
_collapse
240.0
collapse
182.0
CRAP 401.4
↑ Summary Mongoose/Class.pm »
1 package Mongoose::Engine;
2 1 $Mongoose::Engine::VERSION = '2.02';
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util qw/refaddr reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::Util qw/first/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use boolean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Mongoose::Cursor; #initializes moose
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 with 'Mongoose::Role::Collapser';
13 1 with 'Mongoose::Role::Expander';
14 1 with 'Mongoose::Role::Engine';
15  
16 0 ▶sub collapse {
Subroutine collapse: not called CC=13 SCAR=52.0
Pod documented
17 0 my ($self, @scope) = @_;
18  
19 # circularity ?
20 0 ▶ if( my $duplicate = first { refaddr($self) == refaddr($_) } @scope ) {
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $duplicate = &first(sub { builtin::refaddr($self) == builtin::refaddr($_); } , @scope)) 0 0 0
21 0 my $class = blessed $duplicate;
22 0 my $ref_id = $duplicate->_id;
23 0 ▶ return undef unless defined $class && $ref_id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $class and $ref_id 0 0 0
Condition0%0/3
defined $class and $ref_id
!ll&&!rl&&r
000
MC/DC0%0/2
defined $class and $ref_id
defined $class $ref_id
Truth table0%0/3
A and B
A= defined $classB= $ref_id
ABresult
0X0
100
111
24 0 ▶ return undef if $self->_id && $self->_id eq $ref_id; # prevent self references?
Branch: 1 branch
BranchTrueFalseTotal
if $self->_id and $self->_id eq $ref_id 0 0 0
Condition0%0/3
$self->_id and $self->_id eq $ref_id
!ll&&!rl&&r
000
MC/DC0%0/2
$self->_id and $self->_id eq $ref_id
$self->_id $self->_id eq $ref_id
Truth table0%0/3
A and B
A= $self->_idB= $self->_id eq $ref_id
ABresult
0X0
100
111
25 return BSON::DBRef->new(
26 ref => Mongoose->class_config($class)->{collection_name},
27 0 id => $ref_id
28 );
29 }
30  
31 0 my $packed = { %$self }; # cheesely clone the data
32 0 for my $key ( keys %$packed ) {
33 # treat special cases based on Moose attribute defs or traits
34 0 ▶ if ( my $attr = $self->meta->get_attribute($key) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $attr = $self->meta->get_attribute($key)) 0 0 0
35 0 ▶ delete $packed->{$key},
Branch: 1 branch
BranchTrueFalseTotal
if $attr->does("Mongoose::Meta::Attribute::Trait::DoNotMongoSerialize") 0 0 0
36 next if $attr->does('Mongoose::Meta::Attribute::Trait::DoNotMongoSerialize');
37  
38 0 ▶ next if $attr->does('Mongoose::Meta::Attribute::Trait::Raw');
Branch: 1 branch
BranchTrueFalseTotal
if $attr->does("Mongoose::Meta::Attribute::Trait::Raw") 0 0 0
39  
40 0 ▶ if ( my $type = $attr->type_constraint ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = $attr->type_constraint) 0 0 0
41 0 ▶ if ( $type->is_a_type_of('Num') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type->is_a_type_of('Num')) { } 0 0 0
elsif ($type->is_a_type_of('FileHandle')) { } 0 0 0
42 0 $packed->{$key} += 0; # Ensure it's saved as a number
43 0 next;
44 }
45 elsif ( $type->is_a_type_of('FileHandle') ) {
46 $packed->{$key} = BSON::DBRef->new(
47 ref => 'FileHandle',
48 0 id => $self->db->gfs->upload_from_stream( $key. time, delete($packed->{$key}) )
49 );
50 0 next;
51 }
52 }
53 }
54  
55 0 $packed->{$key} = $self->_collapse( $packed->{$key}, @scope );
56 }
57  
58 0 $packed;
59 }
60  
61 0 ▶sub _collapse {
Subroutine _collapse: not called CC=15 SCAR=54.8
62 0 my ($self, $value, @scope ) = @_;
63  
64 0 ▶ if ( my $class = blessed $value ) {
Branch: 3 branches
BranchTrueFalseTotal
if (my $class = builtin::blessed($value)) { } 0 0 0
elsif (ref $value eq 'ARRAY') { } 0 0 0
elsif (ref $value eq 'HASH') { } 0 0 0
65 0 ▶ if ( ref $value eq 'HASH' && defined ( my $ref_id = $value->{_id} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq "HASH" and defined(my $ref_id = $value->{"_id"})) 0 0 0
Condition0%0/3
ref $value eq "HASH" and defined(my $ref_id = $value->{"_id"})
!ll&&!rl&&r
000
MC/DC0%0/2
ref $value eq "HASH" and defined(my $ref_id = $value->{"_id"})
ref $value eq "HASH" defined(my $ref_id = $value->{"_id"})
Truth table0%0/3
A and B
A= ref $value eq "HASH"B= defined(my $ref_id = $value->{"_id"})
ABresult
0X0
100
111
66 # it has an id, so join ref it
67 return BSON::DBRef->new(
68 ref => Mongoose->class_config($class)->{collection_name},
69 0 id => $ref_id
70 );
71 }
72  
73 0 return $self->_unbless( $value, $class, @scope );
74 }
75 elsif ( ref $value eq 'ARRAY' ) {
76 0 my @arr;
77 0 for my $item ( @$value ) {
78 0 ▶ my $aryclass ||= blessed( $item );
Condition0%0/3
my $aryclass ||= builtin::blessed($item)
l!l&&r!l&&!r
000
MC/DC0%0/2
my $aryclass ||= builtin::blessed($item)
my $aryclass builtin::blessed($item)
Truth table0%0/3
A ||= B
A= my $aryclassB= builtin::blessed($item)
ABresult
1X1
011
000
79 0 ▶ if ( $aryclass && $aryclass->does('Mongoose::EmbeddedDocument') ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($aryclass and $aryclass->does('Mongoose::EmbeddedDocument')) { } 0 0 0
elsif ($aryclass and $aryclass->does('Mongoose::Document')) { } 0 0 0
Condition0%0/3
$aryclass and $aryclass->does("Mongoose::EmbeddedDocument")
!ll&&!rl&&r
000
MC/DC0%0/2
$aryclass and $aryclass->does("Mongoose::EmbeddedDocument")
$aryclass $aryclass->does("Mongoose::EmbeddedDocument")
Truth table0%0/3
A and B
A= $aryclassB= $aryclass->does("Mongoose::EmbeddedDocument")
ABresult
0X0
100
111
80 0 push @arr, $item->collapse(@scope, $self);
81 }
82 ▶ elsif ( $aryclass && $aryclass->does('Mongoose::Document') ) {
Condition0%0/3
$aryclass and $aryclass->does("Mongoose::Document")
!ll&&!rl&&r
000
MC/DC0%0/2
$aryclass and $aryclass->does("Mongoose::Document")
$aryclass $aryclass->does("Mongoose::Document")
Truth table0%0/3
A and B
A= $aryclassB= $aryclass->does("Mongoose::Document")
ABresult
0X0
100
111
83 0 $item->_save( @scope, $self );
84 push @arr, BSON::DBRef->new(
85 ref => Mongoose->class_config($aryclass)->{collection_name},
86 0 id => $item->_id
87 );
88 }
89 else {
90 0 push @arr, $item;
91 }
92 }
93 0 return \@arr;
94 }
95 elsif ( ref $value eq 'HASH' ) {
96 0 my $ret = {};
97 0 my @docs;
98 0 for my $key ( keys %$value ) {
99 0 ▶ if ( blessed $value->{$key} ) {
Branch: 2 branches
BranchTrueFalseTotal
if (builtin::blessed($value->{$key})) { } 0 0 0
elsif (ref $value->{$key} eq 'ARRAY') { } 0 0 0
100 0 $ret->{$key} = $self->_unbless( $value->{$key}, blessed($value->{$key}), @scope );
101 }
102 elsif ( ref $value->{$key} eq 'ARRAY' ) {
103 0 $ret->{$key} = [ map { $self->_collapse( $_, @scope ) } @{ $value->{$key} } ];
104 }
105 else {
106 0 $ret->{$key} = $value->{$key};
107 }
108 }
109 0 return $ret;
110 }
111  
112 0 $value;
113 }
114  
115 0 ▶sub _unbless {
Subroutine _unbless: not called CC=10 SCAR=47.0
116 0 my ($self, $obj, $class, @scope ) = @_;
117  
118 0 my $ret = $obj;
119 0 ▶ if ( $class->can('meta') ) { # only mooses from here on
Branch: 2 branches
BranchTrueFalseTotal
if ($class->can('meta')) { } 0 0 0
elsif (not $class =~ /^(?: DateTime(?:\:\:Tiny)? | boolean | ^BSON::)$/x) { } 0 0 0
120 0 ▶ if ( $class->does('Mongoose::EmbeddedDocument') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($class->does('Mongoose::EmbeddedDocument')) { } 0 0 0
elsif ($class->does('Mongoose::Document')) { } 0 0 0
elsif ($class->isa('Mongoose::Join')) { } 0 0 0
121 0 ▶ $ret = $obj->collapse( @scope, $self ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$ret = $obj->collapse(@scope, $self) or next 0 0 0
122 }
123 elsif ( $class->does('Mongoose::Document') ) {
124 0 $obj->_save( @scope, $self );
125 $ret = BSON::DBRef->new(
126 ref => Mongoose->class_config($class)->{collection_name},
127 0 id => $obj->_id
128 );
129 }
130 elsif ( $class->isa('Mongoose::Join') ) {
131 0 my @objs = $obj->_save( $self, @scope );
132 0 $ret = \@objs;
133 }
134 }
135 # non-moose class
136 elsif ( $class !~ /^(?: DateTime(?:\:\:Tiny)? | boolean | ^BSON::)$/x ) { # Types accepted by the driver
137 0 my $reftype = reftype($obj);
138 0 ▶ if ( $reftype eq 'ARRAY' ) { $ret = [@$obj] }
Branch: 3 branches
BranchTrueFalseTotal
if ($reftype eq 'ARRAY') { } 0 0 0
elsif ($reftype eq 'SCALAR') { } 0 0 0
elsif ($reftype eq 'HASH') { } 0 0 0
139 0 elsif ( $reftype eq 'SCALAR' ) { $ret = $$obj }
140 0 elsif ( $reftype eq 'HASH' ) { $ret = {%$obj} }
141 }
142  
143 0 $ret;
144 }
145  
146 0 ▶sub _expand_subtype {
Subroutine _expand_subtype: not called CC=7 SCAR=40.3
147 0 my ( $self, $param_class, $value, $scope ) = @_;
148  
149 0 ▶ return $value->as_datetime if $param_class eq 'DateTime';
Branch: 1 branch
BranchTrueFalseTotal
if $param_class eq "DateTime" 0 0 0
150  
151 0 ▶ return $param_class->expand($value) if $param_class->does('Mongoose::EmbeddedDocument');
Branch: 1 branch
BranchTrueFalseTotal
if $param_class->does("Mongoose::EmbeddedDocument") 0 0 0
152  
153 0 ▶ if ( $param_class->does('Mongoose::Document') ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($param_class->does("Mongoose::Document")) 0 0 0
154 0 ▶ if ( my $circ_doc = $scope->{ $value->id } ) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $circ_doc = $$scope{$value->id}) { } 0 0 0
elsif (my $obj = $param_class->find_one({'_id', $value->id}, undef, $scope)) { } 0 0 0
155 0 return bless( $circ_doc, $param_class );
156 }
157 elsif ( my $obj = $param_class->find_one({ _id => $value->id }, undef, $scope ) ) {
158 0 return $obj;
159 }
160 }
161  
162 0 ▶ warn "Unable to expand subtype $param_class from ". ref($value) || "unblesed value: $value";
Condition0%0/2
"Unable to expand subtype $param_class from " . ref($value) || "unblesed value: $value"
l!l
00
MC/DC0%0/1
"Unable to expand subtype $param_class from " . ref($value) || "unblesed value: $value"
"Unable to expand subtype $param_class from " . ref($value)
Truth table0%0/2
A
A= "Unable to expand subtype $param_class from " . ref($value)
Aresult
11
00
163 0 $value; # Not expanded :-/
164 }
165  
166 0 ▶sub expand {
Subroutine expand: not called CC=35 SCAR=71.4
Pod documented
167 0 my ( $self, $doc, $fields, $scope ) = @_;
168 0 my $config = Mongoose->class_config($self);
169 0 ▶ my $class_main = ref $self || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
170 0 my @later;
171  
172 0 ▶ $scope = {} unless ref $scope eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $scope eq "HASH" 0 0 0
173  
174 # check if it's an straight ref
175 0 ▶ if ( ref $doc eq 'BSON::DBRef' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc eq "BSON::DBRef") 0 0 0
176 return defined $scope->{$doc->id}
177 0 ▶ ? $scope->{$doc->id}
Branch: 1 branch
BranchTrueFalseTotal
defined $$scope{$doc->id} ? : 0 0 0
178 : $class_main->find_one({ _id => $doc->id });
179 #TODO: set at $scope?
180 }
181  
182 0 for my $attr ( $class_main->meta->get_all_attributes ) {
183 0 my $name = $attr->name;
184  
185 0 ▶ next unless exists $doc->{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $doc->{$name} 0 0 0
186 0 ▶ next if $attr->does('Mongoose::Meta::Attribute::Trait::Raw');
Branch: 1 branch
BranchTrueFalseTotal
if $attr->does("Mongoose::Meta::Attribute::Trait::Raw") 0 0 0
187  
188 0 ▶ my $type = $attr->type_constraint or next;
Branch: 1 branch
BranchTrueFalseTotal
my $type = $attr->type_constraint or next 0 0 0
189 0 ▶ my $class = $self->_get_blessed_type( $type ) or next;
Branch: 1 branch
BranchTrueFalseTotal
my $class = $self->_get_blessed_type($type) or next 0 0 0
190  
191 0 ▶ if ( $type->is_a_type_of('HashRef') ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($type->is_a_type_of('HashRef')) { } 0 0 0
elsif ($type->is_a_type_of('ArrayRef')) { } 0 0 0
elsif ($type->is_a_type_of('DateTime')) { } 0 0 0
elsif ($type->is_a_type_of('FileHandle')) { } 0 0 0
192 # HashRef[ parameter ]
193 0 ▶ if( defined $type->{type_parameter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type->{"type_parameter"}) 0 0 0
194 0 my $param = $type->{type_parameter};
195 0 ▶ if ( my $param_class = $param->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param_class = $param->{'class'}) { } 0 0 0
196 0 ▶ for my $key ( keys %{ $doc->{$name} || {} } ) {
Condition0%0/2
$doc->{$name} || {}
l!l
00
MC/DC0%0/1
$doc->{$name} || {}
$doc->{$name}
Truth table0%0/2
A
A= $doc->{$name}
Aresult
11
00
197 0 $doc->{$name}{$key} = $self->_expand_subtype( $param_class, $doc->{$name}{$key}, $scope );
198 }
199 }
200 else {
201 0 ▶ $doc->{$name} ||= {};
Condition0%0/2
$doc->{$name} ||= {}
l!l
00
MC/DC0%0/1
$doc->{$name} ||= {}
$doc->{$name}
Truth table0%0/2
A
A= $doc->{$name}
Aresult
11
00
202 }
203 }
204  
205 0 next;
206 }
207 elsif ( $type->is_a_type_of('ArrayRef') ) {
208 0 ▶ if ( defined $type->{type_parameter} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $type->{"type_parameter"}) 0 0 0
209 # ArrayRef[ parameter ]
210 0 my $param = $type->{type_parameter};
211 0 ▶ if ( my $param_class = $param->{class} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $param_class = $param->{'class'}) { } 0 0 0
212 0 my @objs;
213 0 ▶ for my $item ( @{ $doc->{$name} || [] } ) {
Condition0%0/2
$doc->{$name} || []
l!l
00
MC/DC0%0/1
$doc->{$name} || []
$doc->{$name}
Truth table0%0/2
A
A= $doc->{$name}
Aresult
11
00
214 0 push @objs, $self->_expand_subtype( $param_class, $item, $scope );
215 }
216 0 $doc->{$name} = \@objs;
217 }
218 else {
219 0 ▶ $doc->{$name} ||= [];
Condition0%0/2
$doc->{$name} ||= []
l!l
00
MC/DC0%0/1
$doc->{$name} ||= []
$doc->{$name}
Truth table0%0/2
A
A= $doc->{$name}
Aresult
11
00
220 }
221 }
222  
223 0 next;
224 }
225 elsif ( $type->is_a_type_of('DateTime') ) {
226 0 $doc->{$name} = $doc->{$name}->as_datetime,
227 next;
228 }
229 elsif( $type->is_a_type_of('FileHandle') ) {
230 $doc->{$name} = Mongoose::File->new(
231 0 file_id => $doc->{$name}->id,
232 bucket => $self->db->gfs
233 );
234 0 next;
235 }
236  
237 0 ▶ if( $class->can('meta') ) { # moose subobject
Branch: 1 branch
BranchTrueFalseTotal
if ($class->can('meta')) { } 0 0 0
238  
239 0 ▶ if ( $class->does('Mongoose::EmbeddedDocument') ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($class->does('Mongoose::EmbeddedDocument')) { } 0 0 0
elsif ($class->does('Mongoose::Document')) { } 0 0 0
elsif ($class->isa('Mongoose::Join')) { } 0 0 0
240 0 $doc->{$name} = bless $doc->{$name}, $class;
241 }
242 elsif ( $class->does('Mongoose::Document') ) {
243 0 ▶ if ( ref $doc->{$name} eq 'BSON::DBRef' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $doc->{$name} eq "BSON::DBRef") 0 0 0
244 0 my $_id = $doc->{$name}->id;
245 0 ▶ if ( my $circ_doc = $scope->{"$_id"} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $circ_doc = $$scope{"$_id"}) { } 0 0 0
246 0 $doc->{$name} = bless( $circ_doc , $class );
247 0 $scope->{ "$circ_doc->{_id}" } = $doc->{$name};
248 }
249 else {
250 0 $scope->{ "$doc->{_id}" } = $doc;
251 0 $doc->{$name} = $class->find_one({ _id=>$_id }, undef, $scope );
252 }
253 }
254 }
255 elsif( $class->isa('Mongoose::Join') ) {
256 0 my $ref_arr = delete( $doc->{$name} );
257 0 my $ref_class = $type->type_parameter->class ;
258 $doc->{$name} = bless {
259 class => $class_main, field => $name, parent => $doc->{_id},
260 0 with_class => $ref_class, children => $ref_arr, buffer => {}
261 } => $class;
262 }
263 }
264 else { #non-moose
265 0 my $data = delete $doc->{$name};
266 0 ▶ if ( my $data_class = ref $data ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $data_class = ref $data) { } 0 0 0
267 0 ▶ $doc->{$name} = $data_class eq 'boolean' ? $data : bless $data => $class;
Branch: 1 branch
BranchTrueFalseTotal
$data_class eq 'boolean' ? : 0 0 0
268 }
269 else {
270 0 push @later, { attrib => $name, value => $data };
271 }
272 }
273 }
274  
275 0 ▶ return undef unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 0 0
276 0 bless $doc => $class_main;
277 0 for ( @later ) {
278 0 my $attr = $class_main->meta->get_attribute($_->{attrib});
279 0 ▶ if( defined $attr ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attr) { } 0 0 0
280 # works for read-only values
281 0 $attr->set_value($doc, $_->{value});
282 } else {
283 # sometimes get_attribute is undef, old method instead:
284 0 my $meth = $_->{attrib};
285 0 $doc->$meth($_->{value});
286 }
287 }
288  
289 0 $doc->expanded;
290 0 $doc;
291 }
292  
293 # Called after doc is expanded, a good point for some black magic
294 # Mostly needed to allow old mongoose document classes to
295 # manipulate dates on nested types.
296 0 ▶sub expanded {}
Subroutine expanded: not called CC=1 SCAR=0.0
Pod documented
297  
298 0 ▶sub _joint_fields {
Subroutine _joint_fields: not called CC=1 SCAR=6.9
299 0 my $self = shift;
300 0 return map { $_->name }
301 0 grep { $_->type_constraint->isa('Mongoose::Join') }
302 $self->meta->get_all_attributes;
303 }
304  
305 0 ▶sub fix_integrity {
Subroutine fix_integrity: not called CC=3 SCAR=24.8
Pod documented
306 0 my ($self, @fields ) = @_;
307 0 my $id = $self->_id;
308 0 ▶ @fields = $self->_joint_fields unless scalar @fields;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @fields 0 0 0
309 0 for my $field ( @fields ) {
310 0 my @children = $self->$field->_children_refs;
311 0 $self->collection->update( { _id=>$id }, { '$set'=>{ $field=>\@children } } );
312 }
313 }
314  
315 0 ▶sub _unbless_full {
Subroutine _unbless_full: not called CC=1 SCAR=6.9
316 0 require Data::Structure::Util;
317 0 Data::Structure::Util::unbless( shift );
318 }
319  
320 0 ▶sub save { _save(@_) }
Subroutine save: not called CC=1 SCAR=6.9
Pod documented
321 0 ▶sub _save {
Subroutine _save: not called CC=8 SCAR=42.8
322 0 my ( $self, @scope ) = @_;
323 0 my $coll = $self->collection;
324 0 my $doc = $self->collapse( @scope );
325 0 ▶ return unless defined $doc;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $doc 0 0 0
326  
327 0 ▶ if ( my $id = $self->_id ) { ## update on my id
Branch: 1 branch
BranchTrueFalseTotal
if (my $id = $self->_id) { } 0 0 0
328 0 my $ret = $coll->replace_one( { _id => $id }, $doc, { upsert => 1 } );
329 0 return $id;
330 }
331 else {
332 0 ▶ if ( ref Mongoose->class_config($self)->{pk} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref 'Mongoose'->class_config($self)->{'pk'}) { } 0 0 0
333 # if we have a pk and no _id, we must have a new
334 # document, so we insert to allow the pk constraint
335 # to ensure uniqueness; the 'safe' parameter ensures
336 # an exception is thrown on a duplicate
337 0 my $id = $coll->insert_one( $doc )->inserted_id;
338 0 $self->_id( $id );
339 0 return $id;
340 }
341 else {
342 # save without pk
343 0 my $id = $coll->insert_one( $doc )->inserted_id;
344 0 $self->_id( $id );
345  
346 # if there are any new, unsaved, documents in the scope,
347 # we have circular relation between $self and @scope
348 0 my @unsaved;
349 0 for my $x ( @scope ) {
350 0 ▶ unless( $x->_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($x->_id) 0 0 0
351 0 push @unsaved, $x;
352 }
353 }
354  
355 0 ▶ if (@unsaved) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unsaved) 0 0 0
356 0 ▶ while ( my $x = pop(@unsaved) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $x = pop @unsaved) 0 0 0
357 0 $x->_save(@unsaved);
358 }
359 0 $self->_save;
360 }
361  
362 0 return $id;
363 }
364 }
365 }
366  
367 0 ▶sub _get_blessed_type {
Subroutine _get_blessed_type: not called CC=4 SCAR=30.0
368 0 my ($self,$type) = @_;
369 0 ▶ my $class = $type->name or return;
Branch: 1 branch
BranchTrueFalseTotal
my $class = $type->name or return 0 0 0
370 0 my $parent = $type->parent;
371 0 ▶ return $class unless defined $parent;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parent 0 0 0
372 0 ▶ return $class if $parent eq 'Object';
Branch: 1 branch
BranchTrueFalseTotal
if $parent eq "Object" 0 0 0
373 0 return $parent->name;
374 }
375  
376 # shallow delete
377 0 ▶sub delete {
Subroutine delete: not called CC=3 SCAR=24.8
Pod documented
378 0 my ( $self, $args ) = @_;
379  
380 0 ▶ if ( ref $args ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args) { } 0 0 0
elsif (my $pk = $self->_primary_key_query) { } 0 0 0
381 0 return $self->collection->remove($args);
382 }
383 elsif ( my $pk = $self->_primary_key_query ) {
384 0 return $self->collection->delete_one($pk);
385 }
386  
387 0 return undef;
388 }
389  
390 #sub delete_cascade {
391 # my ($self, $args )=@_;
392 # #TODO delete related collections
393 #}
394  
395 0 ▶sub db {
Subroutine db: not called CC=3 SCAR=24.8
Pod documented
396 0 my $self=shift;
397 0 ▶ return Mongoose->connection( ref $self || $self )
Condition0%0/5
'Mongoose'->connection(ref $self || $self) || croak('MongoDB database need to be configured. Set Mongoose->db(...) first')
l!l
00
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/1
'Mongoose'->connection(ref $self || $self) || croak('MongoDB database need to be configured. Set Mongoose->db(...) first')
'Mongoose'->connection(ref $self || $self)
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/2
A
A= 'Mongoose'->connection(ref $self || $self)
Aresult
11
00
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
398 || croak 'MongoDB database need to be configured. Set Mongoose->db(...) first';
399 }
400  
401 0 ▶sub collection {
Subroutine collection: not called CC=1 SCAR=6.9
Pod documented
402 0 my $self = shift;
403 0 $self->db->get_collection( $self->_collection_name );
404 }
405  
406 0 ▶sub _primary_key_query {
Subroutine _primary_key_query: not called CC=3 SCAR=24.8
407 0 my ( $self, $hash ) = @_;
408 0 ▶ my @keys = @{ Mongoose->class_config($self)->{pk} || ['_id'] };
Condition0%0/2
'Mongoose'->class_config($self)->{'pk'} || ['_id']
l!l
00
MC/DC0%0/1
'Mongoose'->class_config($self)->{'pk'} || ['_id']
'Mongoose'->class_config($self)->{'pk'}
Truth table0%0/2
A
A= 'Mongoose'->class_config($self)->{'pk'}
Aresult
11
00
409 0 my @pairs = map { $_ => $self->{$_} } grep { $self->{$_} } @keys;
410 # Query need to have all pk's
411 0 ▶ return {@pairs} if @pairs == @keys * 2;
Branch: 1 branch
BranchTrueFalseTotal
if @pairs == @keys * 2 0 0 0
412 }
413  
414 0 ▶sub _collection_name { Mongoose->class_config(shift)->{collection_name} }
Subroutine _collection_name: not called CC=1 SCAR=6.9
415  
416 0 ▶sub find {
Subroutine find: not called CC=2 SCAR=17.9
Pod documented
417 0 my $self = shift;
418 0 my $cursor = bless $self->collection->find(@_), 'Mongoose::Cursor';
419 0 $cursor->_collection_name( $self->_collection_name );
420 0 ▶ $cursor->_class( ref $self || $self );
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
421 0 return $cursor;
422 }
423  
424 0 ▶sub query {
Subroutine query: not called CC=1 SCAR=6.9
Pod documented
425 0 my $self = shift;
426 0 $self->collection->_warn_deprecated( 'query' => ['find'] );
427 0 $self->find(@_);
428 }
429  
430 0 ▶sub find_one {
Subroutine find_one: not called CC=8 SCAR=42.8
Pod documented
431 0 my $self = shift;
432  
433 0 ▶ if( @_ == 1 && ( !ref($_[0]) || ref($_[0]) eq 'BSON::OID' ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and !ref($_[0]) || ref $_[0] eq 'BSON::OID') { } 0 0 0
Condition0%0/6
@_ == 1 and !ref($_[0]) || ref $_[0] eq "BSON::OID"
!ll&&!rl&&r
000
not ref $_[0] or ref $_[0] eq "BSON::OID"
l!l&&r!l&&!r
000
MC/DC0%0/3
@_ == 1 and !ref($_[0]) || ref $_[0] eq "BSON::OID"
@_ == 1 not ref $_[0] ref $_[0] eq "BSON::OID"
Truth table0%0/4
A and not(B) or C
A= @_ == 1B= not ref $_[0]C= ref $_[0] eq "BSON::OID"
ABCresult
0XX0
1000
11X1
1011
434 0 ▶ my $query = { _id=> ref $_[0] ? $_[0] : eval{BSON::OID->new( oid => pack("H*",$_[0]) )}||$_[0] };
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
Condition0%0/3
eval { do { "BSON::OID"->new("oid", pack("H*", $_[0])) } } or $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { "BSON::OID"->new("oid", pack("H*", $_[0])) } } or $_[0]
eval { do { "BSON::OID"->new("oid", pack("H*", $_[0])) } } $_[0]
Truth table0%0/3
A or B
A= eval { do { "BSON::OID"->new("oid", pack("H*", $_[0])) } }B= $_[0]
ABresult
1X1
011
000
435 0 ▶ if ( my $doc = $self->collection->find_one($query) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $self->collection->find_one($query)) 0 0 0
436 0 return $self->expand( $doc );
437 }
438 }
439 else {
440 0 my ($query,$fields, $scope) = @_;
441 0 ▶ if ( my $doc = $self->collection->find_one( $query, $fields ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $doc = $self->collection->find_one($query, $fields)) 0 0 0
442 0 return $self->expand( $doc, $fields, $scope );
443 }
444 }
445  
446 0 undef;
447 }
448  
449 0 ▶sub count {
Subroutine count: not called CC=2 SCAR=17.9
Pod documented
450 0 my $self = shift;
451 0 ▶ @_ ? $self->count_documents(@_) : $self->estimated_document_count;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
452 }
453 0 ▶sub count_documents { shift->collection->count_documents(@_) }
Subroutine count_documents: not called CC=1 SCAR=6.9
Pod documented
454 0 ▶sub estimated_document_count { shift->collection->estimated_document_count(@_) }
Subroutine estimated_document_count: not called CC=1 SCAR=6.9
Pod documented
455  
456 =head1 NAME
457  
458 Mongoose::Engine - serialization for MongoDB driver
459  
460 =head1 DESCRIPTION
461  
462 The Mongoose standard engine. Does all the dirty work. Very monolithic.
463 Replace it with your engine if you want.
464  
465 =head1 METHODS
466  
467 =head2 find_one
468  
469 Just like L<MongoDB::Collection/find_one>, but blesses the hash document
470 into your class package.
471  
472 Also has a handy mode which allows
473 retrieving an C<_id> directly from a BSON:OID or just a string:
474  
475 my $author = Author->find_one( '4dd77f4ebf4342d711000000' );
476  
477 Which expands onto:
478  
479 my $author = Author->find_one({
480 _id => BSON::OID->new( value => pack( 'H*', '4dd77f4ebf4342d711000000' ) )
481 });
482  
483 =head2 find
484  
485 Just like L<MongoDB::Collection/find>, but returns
486 a L<Mongoose::Cursor> of documents blessed into
487 your package.
488  
489 =head2 query
490  
491 Just like L<MongoDB::Collection/query>, but returns
492 a L<Mongoose::Cursor> of documents blessed into
493 your package.
494  
495 =head2 count
496  
497 Helper and back-compat method to call estimated_document_count() or count_documents()
498 depending on arguments passed.
499  
500 =head2 estimated_document_count
501  
502 Just like L<MongoDB::Collection/estimated_document_count>.
503  
504 =head2 count_documents
505  
506 Just like L<MongoDB::Collection/count_documents>.
507  
508 =head2 delete
509  
510 Deletes the document in the database.
511  
512 =head2 collapse
513  
514 Turns an object into a hash document.
515  
516 =head2 expand
517  
518 Turns a hash document back into an object.
519  
520 =head2 expanded
521  
522 This is an empty method called by expand() after a document was turned into an object. This
523 was added in version 2 to allow old classes to handle dates on nested types that previously
524 were DateTime objects and now are BSON::Time, but it can be used/abused to any kind of fix
525 to the expanded object.
526  
527 It's recommended to use with care and alway using method modifiers (after) to allow subclassing
528 and composition.
529  
530 =head2 collection
531  
532 Returns the L<MongoDB::Collection> object for this class or object.
533  
534 =head2 save
535  
536 Commits the object to the database.
537  
538 =head2 db
539  
540 Returns the object's corresponding L<MongoDB::Database> instance.
541  
542 =head2 fix_integrity
543  
544 Checks all L<Mongoose::Join> fields for invalid references to
545 foreign object ids.
546  
547 =cut
548  
549 1 1;
↑ Summary Mongoose/Class.pm »