| 1 | |||||||||||
| 2 | package Class::DBI::Lite::Iterator; | ||||||||||
| 3 | |||||||||||
| 4 | 17 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 17 | ▶ | use warnings 'all'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | |||||||||||
| 8 | sub new | ||||||||||
| 9 | 76 | ▶ | { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 10 | 76 | my ($class, $data) = @_; | |||||||||
| 11 | |||||||||||
| 12 | 76 | my $s = bless { | |||||||||
| 13 | data => $data, | ||||||||||
| 14 | count => scalar(@$data), | ||||||||||
| 15 | idx => 0 | ||||||||||
| 16 | }, $class; | ||||||||||
| 17 | 76 | $s->init(); | |||||||||
| 18 | |||||||||||
| 19 | 76 | return $s; | |||||||||
| 20 | }# end new() | ||||||||||
| 21 | |||||||||||
| 22 | |||||||||||
| 23 | 76 | ▶ | sub init { } | ||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 24 | |||||||||||
| 25 | |||||||||||
| 26 | sub first | ||||||||||
| 27 | 14 | ▶ | { | ||||||||
|
Subroutine
first: called CC=2 SCAR=7.2
Pod
undocumented
| |||||||||||
| 28 | 14 | ▶ | return unless $_[0]->{data}->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 14 | $_[0]->{data}->[0]; | |||||||||
| 30 | }# end first() | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | sub next | ||||||||||
| 34 | 119 | ▶ | { | ||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 119 | my $s = shift; | |||||||||
| 36 | 119 | ▶ | return unless $s->{idx} < $s->{count}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | 114 | $s->{data}->[ $s->{idx}++ ]; | |||||||||
| 38 | }# end next() | ||||||||||
| 39 | |||||||||||
| 40 | |||||||||||
| 41 | sub count | ||||||||||
| 42 | 17 | ▶ | { | ||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 43 | 17 | $_[0]->{count}; | |||||||||
| 44 | }# end count() | ||||||||||
| 45 | |||||||||||
| 46 | |||||||||||
| 47 | sub reset | ||||||||||
| 48 | 1 | ▶ | { | ||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | 1 | $_[0]->{idx} = 0; | |||||||||
| 50 | }# end reset() | ||||||||||
| 51 | |||||||||||
| 52 | 17 | 1;# return true: | |||||||||
| 53 | |||||||||||
| 54 | |||||||||||
| 55 | =pod | ||||||||||
| 56 | |||||||||||
| 57 | =head1 NAME | ||||||||||
| 58 | |||||||||||
| 59 | Class::DBI::Lite::Iterator - Simple iterator for Class::DBI::Lite | ||||||||||
| 60 | |||||||||||
| 61 | =head1 SYNOPSIS | ||||||||||
| 62 | |||||||||||
| 63 | # Get an iterator somehow: | ||||||||||
| 64 | my $iter = app::artist->retrieve_all; | ||||||||||
| 65 | |||||||||||
| 66 | my $artist = $iter->first; | ||||||||||
| 67 | |||||||||||
| 68 | my $record_count = $iter->count; | ||||||||||
| 69 | |||||||||||
| 70 | while( my $artist = $iter->next ) | ||||||||||
| 71 | { | ||||||||||
| 72 | ... | ||||||||||
| 73 | }# end while() | ||||||||||
| 74 | |||||||||||
| 75 | # We can reset the iterator to go back to the beginning: | ||||||||||
| 76 | $iter->reset; | ||||||||||
| 77 | print $_->id . "\n" while $_ = $iter->next; | ||||||||||
| 78 | |||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||
| 80 | |||||||||||
| 81 | Provides a simple iterator-based approach to Class::DBI::Lite resultsets. | ||||||||||
| 82 | |||||||||||
| 83 | =head1 PUBLIC PROPERTIES | ||||||||||
| 84 | |||||||||||
| 85 | =head2 count | ||||||||||
| 86 | |||||||||||
| 87 | Returns the number of records in the Iterator. | ||||||||||
| 88 | |||||||||||
| 89 | =head1 PUBLIC METHODS | ||||||||||
| 90 | |||||||||||
| 91 | =head2 next | ||||||||||
| 92 | |||||||||||
| 93 | Returns the next object in the series, or undef. | ||||||||||
| 94 | |||||||||||
| 95 | Moves the internal cursor to the next object if one exists. | ||||||||||
| 96 | |||||||||||
| 97 | =head2 reset | ||||||||||
| 98 | |||||||||||
| 99 | Resets the internal cursor to the first object if one exists. | ||||||||||
| 100 | |||||||||||
| 101 | =head1 SEE ALSO | ||||||||||
| 102 | |||||||||||
| 103 | L<Class::DBI:Lite> | ||||||||||
| 104 | |||||||||||
| 105 | =head1 AUTHOR | ||||||||||
| 106 | |||||||||||
| 107 | John Drago <jdrago_999@yahoo.com>. | ||||||||||
| 108 | |||||||||||
| 109 | =head1 LICENSE AND COPYRIGHT | ||||||||||
| 110 | |||||||||||
| 111 | Copyright 2008 John Drago <jdrago_999@yahoo.com>. All rights reserved. | ||||||||||
| 112 | |||||||||||
| 113 | This software is Free software and may be used and distributed under the same | ||||||||||
| 114 | terms as perl itself. | ||||||||||
| 115 | |||||||||||
| 116 | =cut | ||||||||||
| 117 | |||||||||||