| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Mongoose::Cursor; |
|
2
|
|
|
|
|
|
|
$Mongoose::Cursor::VERSION = '2.02'; |
|
3
|
1
|
|
|
1
|
|
7
|
use Moose; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
5
|
|
|
4
|
|
|
|
|
|
|
extends 'MongoDB::Cursor'; |
|
5
|
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
has _class => ( is=>'rw', isa=>'Str', required=>1 ); |
|
7
|
|
|
|
|
|
|
has _collection_name => ( is=>'rw', isa=>'Str', required=>1 ); |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
around 'next' => sub { |
|
10
|
|
|
|
|
|
|
my ($orig, $self) = (shift, shift); |
|
11
|
|
|
|
|
|
|
my $doc = $self->$orig(@_) || return; |
|
12
|
|
|
|
|
|
|
$self->_class->expand( $doc ); |
|
13
|
|
|
|
|
|
|
}; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
for my $arr_method (qw/ all batch /) { |
|
16
|
|
|
|
|
|
|
around $arr_method => sub { |
|
17
|
|
|
|
|
|
|
my ($orig, $self) = (shift, shift); |
|
18
|
|
|
|
|
|
|
map { $self->_class->expand( $_ ) } $self->$orig(@_); |
|
19
|
|
|
|
|
|
|
}; |
|
20
|
|
|
|
|
|
|
} |
|
21
|
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# Dumb re-implementation of deprecated count() method |
|
23
|
|
|
|
|
|
|
sub count { |
|
24
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
|
25
|
0
|
|
|
|
|
|
$self->_class->count($self->_query->filter); |
|
26
|
|
|
|
|
|
|
} |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
sub each(&) { |
|
29
|
0
|
|
|
0
|
1
|
|
my ( $self, $cb ) = @_; |
|
30
|
0
|
0
|
|
|
|
|
while( my $r = $self->next ) { last unless defined $cb->($r) } |
|
|
0
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
sub hash_on { |
|
34
|
0
|
|
|
0
|
1
|
|
my ($self, $key) = @_; |
|
35
|
|
|
|
|
|
|
|
|
36
|
0
|
|
|
|
|
|
my %hash; |
|
37
|
0
|
|
|
|
|
|
while( my $r = $self->next ) { |
|
38
|
0
|
0
|
|
|
|
|
$hash{ $r->{$key} } = $r unless exists $hash{ $r->{$key} }; |
|
39
|
|
|
|
|
|
|
} |
|
40
|
0
|
|
|
|
|
|
return %hash; |
|
41
|
|
|
|
|
|
|
} |
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
sub hash_array { |
|
44
|
0
|
|
|
0
|
1
|
|
my ($self, $key) = @_; |
|
45
|
|
|
|
|
|
|
|
|
46
|
0
|
|
|
|
|
|
my %hash; |
|
47
|
0
|
|
|
|
|
|
while( my $r = $self->next ) { |
|
48
|
0
|
|
|
|
|
|
push @{ $hash{ $r->{$key} } }, $r; |
|
|
0
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable(); |