MongoDB/GridFS/File.pm untested

filter: Statementstmt 0.0% 0 / 36 Branchbran 0.0% 0 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 4 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 53 CC 7 SCAR 36.8
CC
7
Cov
0%
read
56.0
BEGIN
2.0
BEGIN
2.0
CRAP 39.8
1 ## Copyright (C) Graham Barr
2 ## vim: ts=8:sw=2:expandtab:shiftround
3  
4 ## WARNING!!!!
5 ## WARNING!!!! This code is still very much a work in progress,
6 ## WARNING!!!! do not depend on anything not changing
7 ## WARNING!!!!
8  
9 package AnyEvent::MongoDB::GridFS::File;
10  
11 use Moose;
12  
13 has doc => ( is => 'ro');
14 has gridfs => ( is => 'ro' );
15  
16 use Scalar::Util qw(weaken);
17  
18 use namespace::autoclean;
19  
20 sub read {
21 my $self = shift;
22  
23 my %arg;
24 $arg{cb} = shift if ref($_[0]);
25  
26 my %options = (@_, %arg);
27  
28 # TODO, support fetching less than just the whole file
29  
30 my $files_id = $self->doc->{_id};
31 my $cb = $options{cb};
32 my $chunks = $self->gridfs->chunks;
33 my $buffer;
34 my $n = 0;
35  
36 my $iter;
37 $iter = sub {
38 my $doc = shift;
39 if (!$doc) {
40 $cb->($options{chunked} ? () : ($buffer)); # done
41 }
42 elsif ($doc->{'$err'}) {
43 $cb->($doc); # done
44 }
45 else {
46 my $more = 1;
47 if ($options{chunked}) {
48 $more = $cb->($doc->{data});
49 }
50 else {
51 $buffer .= $doc->{data};
52 }
53 if ($more) {
54 $chunks->find(
55 query => {
56 files_id => $files_id,
57 n => ++$n,
58 },
59 cb => $iter,
60 limit => -1,
61 );
62 }
63 }
64 return;
65 };
66  
67 $chunks->find(
68 query => {
69 files_id => $files_id,
70 n => $n,
71 },
72 cb => $iter,
73 );
74  
75 weaken($iter); # avoid loops
76 }
77  
78 __PACKAGE__->meta->make_immutable;