File Coverage

blib/lib/MongoDBx/AutoDeref/DBRef.pm
Criterion Covered Total %
statement 10 24 41.6
branch 0 8 0.0
condition 0 6 0.0
subroutine 4 6 66.6
pod 2 2 100.0
total 16 46 34.7


line stmt bran cond sub pod time code
1             package MongoDBx::AutoDeref::DBRef;
2             BEGIN {
3 2     2   47 $MongoDBx::AutoDeref::DBRef::VERSION = '1.110560';
4             }
5              
6             #ABSTRACT: DBRef representation in Perl
7              
8 2     2   11 use Moose;
  2         4  
  2         16  
9 2     2   12787 use namespace::autoclean;
  2         6  
  2         19  
10 2     2   155 use MooseX::Types::Moose(':all');
  2         6  
  2         20  
11              
12              
13             has mongo_connection =>
14             (
15             is => 'ro',
16             isa => 'MongoDB::Connection',
17             required => 1,
18             );
19              
20              
21             has '$id' =>
22             (
23             is => 'ro',
24             isa => 'MongoDB::OID',
25             reader => 'id',
26             required => 1,
27             );
28              
29              
30             has '$ref' =>
31             (
32             is => 'ro',
33             isa => Str,
34             reader => 'ref',
35             required => 1,
36             );
37              
38              
39             has '$db' =>
40             (
41             is => 'ro',
42             isa => Str,
43             reader => 'db',
44             required => 1,
45             );
46              
47              
48             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             {
59 0     0 1   my ($self) = @_;
60 0           return +{ '$db' => $self->db, '$ref' => $self->ref, '$id' => $self->id };
61             }
62              
63              
64             sub fetch
65             {
66 0     0 1   my ($self, $fields) = @_;
67 0           my %hash = %{$self->revert()};
  0            
68 0           my @dbs = $self->mongo_connection->database_names();
69 0 0 0       die "Database '$hash{'$db'}' doesn't exist"
70             unless (scalar(@dbs) > 0 || any(@dbs) eq $hash{'$db'});
71              
72 0           my $db = $self->mongo_connection->get_database($hash{'$db'});
73 0           my @cols = $db->collection_names;
74              
75 0 0 0       die "Collection '$hash{'$ref'}' doesn't exist in $hash{'$db'}"
76             unless (scalar(@cols) > 0 || any(@cols) eq $hash{'$ref'});
77              
78 0           my $collection = $db->get_collection($hash{'$ref'});
79              
80 0 0         my $doc = $collection->find_one
    0          
81             (
82             {
83             _id => $hash{'$id'}
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             __PACKAGE__->meta->make_immutable();
97             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__
184