Mongoose/Cursor.pm

filter: Statementstmt 33.3% 11 / 33 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 14.2% 1 / 7 Podpod 100% 4 / 4 totaltotal 28.0% 16 / 57 CC 7 SCAR 20.5
CC
7
Cov
24%
each
12.0
hash_on
12.0
__ANON__
6.0
CRAP 7.8
« Mongoose.pm ↑ Summary Mongoose/Document.pm »
1 package Mongoose::Cursor;
2 1 $Mongoose::Cursor::VERSION = '2.02';
3 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 extends 'MongoDB::Cursor';
5  
6 1 has _class => ( is=>'rw', isa=>'Str', required=>1 );
7 1 has _collection_name => ( is=>'rw', isa=>'Str', required=>1 );
8  
9 0 ▶around 'next' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
10 0 my ($orig, $self) = (shift, shift);
11 0 ▶ my $doc = $self->$orig(@_) || return;
Condition0%0/2
$self->$orig(@_) || (return)
l!l
00
MC/DC0%0/1
$self->$orig(@_) || (return)
$self->$orig(@_)
Truth table0%0/2
A
A= $self->$orig(@_)
Aresult
11
00
12 0 $self->_class->expand( $doc );
13 1 };
14  
15 1 for my $arr_method (qw/ all batch /) {
16 0 ▶ around $arr_method => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
17 0 my ($orig, $self) = (shift, shift);
18 0 map { $self->_class->expand( $_ ) } $self->$orig(@_);
19 2 };
20 }
21  
22 # Dumb re-implementation of deprecated count() method
23 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
24 0 my $self = shift;
25 0 $self->_class->count($self->_query->filter);
26 }
27  
28 0 ▶sub each(&) {
Subroutine each: not called CC=3 SCAR=24.8
Pod documented
29 0 my ( $self, $cb ) = @_;
30 0 ▶ while( my $r = $self->next ) { last unless defined $cb->($r) }
Branch: 2 branches
BranchTrueFalseTotal
if (my $r = $self->next) 0 0 0
unless defined &$cb($r) 0 0 0
31 }
32  
33 0 ▶sub hash_on {
Subroutine hash_on: not called CC=3 SCAR=24.8
Pod documented
34 0 my ($self, $key) = @_;
35  
36 0 my %hash;
37 0 ▶ while( my $r = $self->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->next) 0 0 0
38 0 ▶ $hash{ $r->{$key} } = $r unless exists $hash{ $r->{$key} };
Branch: 1 branch
BranchTrueFalseTotal
unless exists $hash{$r->{$key}} 0 0 0
39 }
40 0 return %hash;
41 }
42  
43 0 ▶sub hash_array {
Subroutine hash_array: not called CC=2 SCAR=17.9
Pod documented
44 0 my ($self, $key) = @_;
45  
46 0 my %hash;
47 0 ▶ while( my $r = $self->next ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = $self->next) 0 0 0
48 0 push @{ $hash{ $r->{$key} } }, $r;
49 }
50 0 return %hash;
51 }
52  
53 =head1 NAME
54  
55 Mongoose::Cursor - a Mongoose wrapper for MongoDB::Cursor
56  
57 =head1 DESCRIPTION
58  
59 Extends L<Mongoose::Cursor>.
60  
61 Wraps L<MongoDB::Cursor>'s C<next>, C<all> and C<batch> methods,
62 so that it expands a document into a class.
63  
64 =head1 METHODS
65  
66 For your convenience:
67  
68 =head2 count
69  
70 Same as calling count() on the collection. It will allways return the total
71 counting of the filter ignoring skip() and limit().
72  
73 =head2 each
74  
75 Iterates over a cursor, calling your sub.
76  
77 Person->find->each( sub {
78 my $obj = shift;
79  
80 # do stuff
81  
82 # return undef to break out
83 return undef if $done;
84 });
85  
86 =head2 hash_on
87  
88 Returns all data as a HASH indexed by the key sent as first argument.
89 Rows with duplicate keys are ignored.
90  
91 %tracks = $cd->tracks->find->hash_on('track_name');
92  
93 =head2 hash_array
94  
95 Returns all data as a HASH indexed by the key sent as first argument.
96 Hash values are ARRAYREFs with 1 or more rows.
97  
98 %tracks = $cd->tracks->find->hash_array('track_name');
99  
100 =cut
101  
102 1 __PACKAGE__->meta->make_immutable();
« Mongoose.pm ↑ Summary Mongoose/Document.pm »