File Coverage

blib/lib/MongoDBx/AutoDeref.pm
Criterion Covered Total %
statement 29 43 67.4
branch 0 2 0.0
condition n/a
subroutine 7 10 70.0
pod n/a
total 36 55 65.4


line stmt bran cond sub pod time code
1             package MongoDBx::AutoDeref;
2             BEGIN {
3 2     2   2269931 $MongoDBx::AutoDeref::VERSION = '1.110560';
4             }
5              
6             #ABSTRACT: Automagically dereference MongoDB DBRefs lazily
7              
8 2     2   20 use warnings;
  2         4  
  2         64  
9 2     2   11 use strict;
  2         5  
  2         72  
10 2     2   14 use feature qw/state/;
  2         3  
  2         216  
11 2     2   1081 use Class::Load('load_class');
  2         38236  
  2         174  
12 2     2   1239 use MongoDBx::AutoDeref::LookMeUp;
  2         39  
  2         1002  
13              
14              
15              
16             sub import
17             {
18 2     2   23 my ($class) = @_;
19              
20 2         11 load_class('MongoDB');
21 2         1405162 load_class('MongoDB::Cursor');
22 2         63 load_class('MongoDB::Collection');
23 2         59 my $cur = 'MongoDB::Cursor'->meta();
24 2         62 $cur->make_mutable();
25             $cur->add_around_method_modifier
26             (
27             'next',
28             sub
29             {
30 0     0   0 my ($orig, $self, @args) = @_;
31 0         0 state $lookmeup = MongoDBx::AutoDeref::LookMeUp->new(
32             mongo_connection => $self->_connection,
33             sieve_type => 'output',
34             );
35              
36 0         0 my $ret = $self->$orig(@args);
37 0 0       0 if(defined($ret))
38             {
39 0         0 $lookmeup->sieve($ret);
40             }
41              
42 0         0 return $ret;
43             }
44 2         1281 );
45 2         708 $cur->make_immutable(inline_destructor => 0);
46              
47 2         919 my $col = 'MongoDB::Collection'->meta();
48 2         32 $col->make_mutable();
49             $col->add_around_method_modifier
50             (
51             'batch_insert',
52             sub
53             {
54 0     0   0 my ($orig, $self, $object, $options) = @_;
55 0         0 state $lookmeup = MongoDBx::AutoDeref::LookMeUp->new(
56             mongo_connection => $self->_database->_connection,
57             sieve_type => 'input',
58             );
59              
60 0         0 $lookmeup->sieve($object);
61 0         0 return $self->$orig($object, $options);
62             }
63 2         665 );
64             $col->add_around_method_modifier
65             (
66             'update',
67             sub
68             {
69 0     0   0 my ($orig, $self, $query, $object, $options) = @_;
70 0         0 state $lookmeup = MongoDBx::AutoDeref::LookMeUp->new(
71             mongo_connection => $self->_database->_connection,
72             sieve_type => 'input',
73             );
74              
75 0         0 $lookmeup->sieve($object);
76 0         0 return $self->$orig($query, $object, $options);
77             }
78 2         607 );
79              
80 2         443 $col->make_immutable(inline_destructor => 0);
81             }
82              
83             1;
84              
85              
86              
87             =pod
88              
89             =head1 NAME
90              
91             MongoDBx::AutoDeref - Automagically dereference MongoDB DBRefs lazily
92              
93             =head1 VERSION
94              
95             version 1.110560
96              
97             =head1 SYNOPSIS
98              
99             use MongoDB; #or omit this
100             use MongoDBx::AutoDeref;
101              
102             my $connection = MongoDB::Connection->new();
103             my $database = $connection->get_database('foo');
104             my $collection = $database->get_collection('bar');
105              
106             my $doc1 = { baz => 'flarg' };
107             my $doc2 = { yarp => 'floop' };
108              
109             my $id = $collection->insert($doc1);
110             $doc2->{dbref} = {'$db' => 'foo', '$ref' => 'bar', '$id' => $id };
111             my $id2 = $collection->insert($doc2);
112              
113             my $fetched_doc2 = $collection->find_one({_id => $id2 });
114             my $fetched_doc1 = $fetched_doc2->{dbref}->fetch;
115            
116             # $fetched_doc1 == $doc1
117              
118             =head1 DESCRIPTION
119              
120             Using Mongo drivers from other languages and miss driver support for expanding
121             DBRefs? Then this module is for you. Simple 'use' it to have this ability added
122             to the core MongoDB driver.
123              
124             Please read more about DBRefs:
125             http://www.mongodb.org/display/DOCS/Database+References
126              
127             If more information is necessary on the guts, please see
128             L<MongoDBx::AutoDeref::LookMeUp>
129              
130             =head1 CLASS_METHODS
131              
132             =head2 import
133              
134             Upon use (or require+import), this class method will load MongoDB (if it isn't
135             already loaded), and alter the metaclasses for MongoDB::Cursor and
136             MongoDB::Collection. Internally, everything is cursor driven so the result
137             returned is ultimately from the L<MongoDB::Cursor/next> method. So this method
138             is advised to apply the L<MongoDBx::AutoDeref::LookMeUp> sieve to the returned
139             result which replaces all DBRefs with L<MongoDBx::AutoDeref::DBRef> objects.
140             When doing updates and inserts back using MongoDB::Collection, the inflated
141             objects will be deflated back into DBRefs.
142              
143             =head1 AUTHOR
144              
145             Nicholas R. Perez <nperez@cpan.org>
146              
147             =head1 COPYRIGHT AND LICENSE
148              
149             This software is copyright (c) 2010 by Nicholas R. Perez <nperez@cpan.org>.
150              
151             This is free software; you can redistribute it and/or modify it under
152             the same terms as the Perl 5 programming language system itself.
153              
154             =cut
155              
156              
157             __END__
158