File Coverage

blib/lib/MongoDBx/AutoDeref/LookMeUp.pm
Criterion Covered Total %
statement 31 41 75.6
branch 0 4 0.0
condition n/a
subroutine 11 14 78.5
pod n/a
total 42 59 71.1


line stmt bran cond sub pod time code
1             package MongoDBx::AutoDeref::LookMeUp;
2             BEGIN {
3 2     2   45 $MongoDBx::AutoDeref::LookMeUp::VERSION = '1.110560';
4             }
5              
6             #ABSTRACT: Provides the sieve that replaces DBRefs with deferred scalars.
7              
8 2     2   916 use Moose;
  2         528090  
  2         20  
9 2     2   15520 use namespace::autoclean;
  2         3115  
  2         11  
10              
11 2     2   114 use Scalar::Util('weaken');
  2         3  
  2         104  
12 2     2   2214 use MooseX::Types::Structured(':all');
  2         924996  
  2         18  
13 2     2   3387 use MooseX::Types::Moose(':all');
  2         32681  
  2         26  
14 2     2   18304 use Data::Visitor::Callback;
  2         135014  
  2         89  
15 2     2   19 use Moose::Util::TypeConstraints();
  2         6  
  2         45  
16 2     2   1370 use MongoDBx::AutoDeref::Types(':all');
  2         9  
  2         13  
17 2     2   3294 use MongoDBx::AutoDeref::DBRef;
  2         7  
  2         87  
18 2     2   1825 use Perl6::Junction('any');
  2         24539  
  2         876  
19              
20              
21             has mongo_connection =>
22             (
23             is => 'ro',
24             isa => 'MongoDB::Connection',
25             required => 1,
26             );
27              
28              
29              
30             has visitor =>
31             (
32             is => 'ro',
33             isa => 'Data::Visitor::Callback',
34             lazy => 1,
35             builder => '_build_visitor',
36             handles => { 'sieve' => 'visit' },
37             );
38              
39             sub _build_visitor
40             {
41 0     0     my ($self) = @_;
42 0           weaken($self);
43 0 0         if($self->sieve_type eq 'output')
44             {
45             return Data::Visitor::Callback->new
46             (
47             hash => sub
48             {
49 0     0     my ($visitor, $data) = @_;
50 0 0         return unless is_DBRef($data);
51 0           $_ = MongoDBx::AutoDeref::DBRef->new
52             (
53             %$data,
54             mongo_connection => $self->mongo_connection,
55             lookmeup => $self,
56             );
57             },
58 0           ignore_return_values => 1,
59             );
60             }
61             else
62             {
63             return Data::Visitor::Callback->new
64             (
65             'MongoDBx::AutoDeref::DBRef' => sub
66             {
67 0     0     my ($visitor, $obj) = @_;
68 0           $_ = $obj->revert()
69             },
70 0           ignore_return_values => 1,
71             );
72             }
73             }
74              
75              
76             has sieve_type =>
77             (
78             is => 'ro',
79             isa => Moose::Util::TypeConstraints::enum([qw/input output/]),
80             required => 1,
81             );
82              
83             1;
84              
85              
86             =pod
87              
88             =head1 NAME
89              
90             MongoDBx::AutoDeref::LookMeUp - Provides the sieve that replaces DBRefs with deferred scalars.
91              
92             =head1 VERSION
93              
94             version 1.110560
95              
96             =head1 DESCRIPTION
97              
98             This module provides the guts for L<MongoDBx::AutoDeref>. It modifies documents
99             in place to replace DBRefs with actual objects that implement the deferred
100             fetch. This class also will deflate those same objects back into plain hashes.
101              
102             =head1 PUBLIC_ATTRIBUTES
103              
104             =head2 mongo_connection
105              
106             is: ro, isa: MongoDB::Connection, required: 1
107              
108             In order to defer fetching the referenced document, a connection object needs to
109             be accessible. This is required for construction of the object.
110              
111             =head2 visitor
112              
113             is: ro, isa: Data::Visitor::Callback
114             lazy: 1, builder => _build_visitor
115             handles: sieve => visit
116              
117             In order to find the DBRefs within the returned document, Data::Visitor is used
118             to traverse the structure. The raw hashes are replaced with proper objects that
119             implement the lookup via L<MongoDBx::AutoDeref::DBRef/fetch>. Upon
120             insert/update, these objects are then deflated back to their raw hash
121             references.
122              
123             =head2 sieve_type
124              
125             is: ro, isa: enum(input,output), required: 1
126              
127             The LookMeUp object can operate in two modes. In the input mode,
128             L<MongoDBx::AutoDeref::DBRef> objects will be deflated to plain hashes. In
129             output mode, plain hashes that pass the DBRef type constraint will be inflated.
130              
131             =head1 PUBLIC_METHODS
132              
133             =head2 sieve
134              
135             (HashRef)
136              
137             This method takes the returned document from MongoDB and traverses it to replace
138             DBRefs with defered lookups of the actual document. It does this IN PLACE on the
139             document.
140              
141             The obverse is true as well. If storing a document the document will be
142             traversed and the DBRef objects will be deflated into plain hashes
143              
144             =head1 AUTHOR
145              
146             Nicholas R. Perez <nperez@cpan.org>
147              
148             =head1 COPYRIGHT AND LICENSE
149              
150             This software is copyright (c) 2010 by Nicholas R. Perez <nperez@cpan.org>.
151              
152             This is free software; you can redistribute it and/or modify it under
153             the same terms as the Perl 5 programming language system itself.
154              
155             =cut
156              
157              
158             __END__
159