AutoDeref/DBRef.pm

filter: Statementstmt 54.8% 17 / 31 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 66.6% 4 / 6 Podpod 100% 2 / 2 totaltotal 40.3% 23 / 57 CC 7 SCAR 35.0
CC
7
Cov
38%
fetch
56.0
revert
2.0
BEGIN
1.0
CRAP 33.2
↑ Summary AutoDeref/LookMeUp.pm »
1 package MongoDBx::AutoDeref::DBRef;
2 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 $MongoDBx::AutoDeref::DBRef::VERSION = '1.110560';
4 }
5  
6 #ABSTRACT: DBRef representation in Perl
7  
8 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use MooseX::Types::Moose(':all');
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12  
13 2 has mongo_connection =>
14 (
15 is => 'ro',
16 isa => 'MongoDB::Connection',
17 required => 1,
18 );
19  
20  
21 2 has '$id' =>
22 (
23 is => 'ro',
24 isa => 'MongoDB::OID',
25 reader => 'id',
26 required => 1,
27 );
28  
29  
30 2 has '$ref' =>
31 (
32 is => 'ro',
33 isa => Str,
34 reader => 'ref',
35 required => 1,
36 );
37  
38  
39 2 has '$db' =>
40 (
41 is => 'ro',
42 isa => Str,
43 reader => 'db',
44 required => 1,
45 );
46  
47  
48 2 has lookmeup =>
49 (
50 is => 'ro',
51 isa => 'MongoDBx::AutoDeref::LookMeUp',
52 required => 1,
53 weak_ref => 1,
54 );
55  
56  
57 sub revert
58 0 ▶{
Subroutine revert: not called CC=1 SCAR=6.9
Pod documented
59 0 my ($self) = @_;
60 0 return +{ '$db' => $self->db, '$ref' => $self->ref, '$id' => $self->id };
61 }
62  
63  
64 sub fetch
65 0 ▶{
Subroutine fetch: not called CC=7 SCAR=40.3
Pod documented
66 0 my ($self, $fields) = @_;
67 0 my %hash = %{$self->revert()};
68 0 my @dbs = $self->mongo_connection->database_names();
69 die "Database '$hash{'$db'}' doesn't exist"
70 0 ▶ unless (scalar(@dbs) > 0 || any(@dbs) eq $hash{'$db'});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @dbs > 0 or any(@dbs) eq $hash{"\$db"} 0 0 0
Condition0%0/3
scalar @dbs > 0 or any(@dbs) eq $hash{"\$db"}
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @dbs > 0 or any(@dbs) eq $hash{"\$db"}
scalar @dbs > 0 any(@dbs) eq $hash{"\$db"}
Truth table0%0/3
A or B
A= scalar @dbs > 0B= any(@dbs) eq $hash{"\$db"}
ABresult
1X1
011
000
71  
72 0 my $db = $self->mongo_connection->get_database($hash{'$db'});
73 0 my @cols = $db->collection_names;
74  
75 die "Collection '$hash{'$ref'}' doesn't exist in $hash{'$db'}"
76 0 ▶ unless (scalar(@cols) > 0 || any(@cols) eq $hash{'$ref'});
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @cols > 0 or any(@cols) eq $hash{"\$ref"} 0 0 0
Condition0%0/3
scalar @cols > 0 or any(@cols) eq $hash{"\$ref"}
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @cols > 0 or any(@cols) eq $hash{"\$ref"}
scalar @cols > 0 any(@cols) eq $hash{"\$ref"}
Truth table0%0/3
A or B
A= scalar @cols > 0B= any(@cols) eq $hash{"\$ref"}
ABresult
1X1
011
000
77  
78 0 my $collection = $db->get_collection($hash{'$ref'});
79  
80 my $doc = $collection->find_one
81 (
82 {
83 0 ▶ _id => $hash{'$id'}
Branch: 2 branches
BranchTrueFalseTotal
my $doc = $collection->find_one({"_id", $hash{"\$id"}}, defined $fields ? $fields : ()) or die qq[Unable to find document with _id: '$hash{"\$id"}'] 0 0 0
defined $fields ? : 0 0 0
84 },
85 (
86 defined($fields)
87 ? $fields
88 : ()
89 )
90 ) or die "Unable to find document with _id: '$hash{'$id'}'";
91  
92 0 $self->lookmeup->sieve($doc);
93 0 return $doc;
94 }
95  
96 2 __PACKAGE__->meta->make_immutable();
97 2 1;
98  
99  
100 =pod
101  
102 =head1 NAME
103  
104 MongoDBx::AutoDeref::DBRef - DBRef representation in Perl
105  
106 =head1 VERSION
107  
108 version 1.110560
109  
110 =head1 DESCRIPTION
111  
112 MongoDBx::AutoDeref::DBRef is the Perl space representation of Mongo database
113 references. These ideally shouldn't be constructed manually, but instead should
114 be constructed by the internal L<MongoDBx::AutoDeref::LookMeUp> class.
115  
116 =head1 PUBLIC_ATTRIBUTES
117  
118 =head2 mongo_connection
119  
120 is: ro, isa: MongoDB::Connection, required: 1
121  
122 In order to defer fetching the referenced document, a connection object needs to
123 be accessible. This is required for construction of the object.
124  
125 =head2 $id
126  
127 is: ro, isa: MongoDB::OID, reader: id, required: 1
128  
129 This is the OID of the object.
130  
131 =head2 $ref
132  
133 is: ro, isa: Str, reader: ref, required: 1
134  
135 This is the collection in which this item resides.
136  
137 =head2 $db
138  
139 is: ro, isa: Str, reader: db, required: 1
140  
141 This is the database in which this item resides.
142  
143 =head2 lookmeup
144  
145 is: ro, isa: MongoDBx::AutoDeref::LookMeUp, weak_ref: 1, required: 1
146  
147 When fetching referenced documents, those documents may in turn reference other
148 documents. By providing a LookMeUp object, those other references can also be
149 travered as DBRefs.
150  
151 =head1 PUBLIC_METHODS
152  
153 =head2 revert
154  
155 This method returns a hash reference in the DBRef format suitable for MongoDB
156 serialization.
157  
158 =head2 fetch
159  
160 (HashRef?)
161  
162 fetch takes the information contained in the L</$db>, L</$ref>, L</$id>
163 attributes and applies them via the L</mongo_connection> to retrieve the
164 document that is referenced.
165  
166 fetch also accepts a hashref of fields-as-keys that will be passed unaltered
167 directly to the MongoDB driver as a way to limit the fields pulled back.
168  
169 =head1 AUTHOR
170  
171 Nicholas R. Perez <nperez@cpan.org>
172  
173 =head1 COPYRIGHT AND LICENSE
174  
175 This software is copyright (c) 2010 by Nicholas R. Perez <nperez@cpan.org>.
176  
177 This is free software; you can redistribute it and/or modify it under
178 the same terms as the Perl 5 programming language system itself.
179  
180 =cut
181  
182  
183 __END__
↑ Summary AutoDeref/LookMeUp.pm »