| 1 | # ABSTARCT: Internal iterator class for OpenSky::API | ||||||||||
| 2 | |||||||||||
| 3 | package OpenSky::API::Utils::Iterator; | ||||||||||
| 4 | |||||||||||
| 5 | 6 | our $VERSION = '0.004'; | |||||||||
| 6 | 6 | ▶ | use Moose; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 6 | use OpenSky::API::Types qw( | |||||||||
| 8 | ArrayRef | ||||||||||
| 9 | Defined | ||||||||||
| 10 | InstanceOf | ||||||||||
| 11 | PositiveOrZeroInt | ||||||||||
| 12 | 6 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 6 | ▶ | use experimental qw(signatures); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | |||||||||||
| 15 | 6 | has '_rows' => ( | |||||||||
| 16 | is => 'ro', | ||||||||||
| 17 | isa => ArrayRef [Defined], | ||||||||||
| 18 | init_arg => 'rows', | ||||||||||
| 19 | ); | ||||||||||
| 20 | |||||||||||
| 21 | 6 | has '_index' => ( | |||||||||
| 22 | is => 'rw', | ||||||||||
| 23 | writer => '_set_index', | ||||||||||
| 24 | isa => PositiveOrZeroInt, | ||||||||||
| 25 | default => 0, | ||||||||||
| 26 | ); | ||||||||||
| 27 | |||||||||||
| 28 | ▶ | sub first ($self) { | |||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 29 | 6 | return $self->_rows->[0]; | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | ▶ | sub next ($self) { | |||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 33 | 8 | my $i = $self->_index; | |||||||||
| 34 | 8 | ▶ | my $next = $self->_rows->[$i] or return; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 35 | 6 | $self->_set_index( $i + 1 ); | |||||||||
| 36 | 6 | return $next; | |||||||||
| 37 | } | ||||||||||
| 38 | |||||||||||
| 39 | ▶ | sub reset ($self) { | |||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 40 | 1 | $self->_set_index(0); | |||||||||
| 41 | 1 | return 1; | |||||||||
| 42 | } | ||||||||||
| 43 | |||||||||||
| 44 | ▶ | sub all ($self) { | |||||||||
|
Subroutine
all: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 2 | return @{ $self->_rows }; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | ▶ | sub count ($self) { | |||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 49 | 1 | my @all = $self->all; | |||||||||
| 50 | 1 | return scalar @all; | |||||||||
| 51 | } | ||||||||||
| 52 | |||||||||||
| 53 | 6 | __PACKAGE__->meta->make_immutable; | |||||||||
| 54 | |||||||||||
| 55 | __END__ | ||||||||||
| 56 | |||||||||||
| 57 | =pod | ||||||||||
| 58 | |||||||||||
| 59 | =encoding UTF-8 | ||||||||||
| 60 | |||||||||||
| 61 | =head1 NAME | ||||||||||
| 62 | |||||||||||
| 63 | OpenSky::API::Utils::Iterator | ||||||||||
| 64 | |||||||||||
| 65 | =head1 VERSION | ||||||||||
| 66 | |||||||||||
| 67 | version 0.004 | ||||||||||
| 68 | |||||||||||
| 69 | =head1 SYNOPSIS | ||||||||||
| 70 | |||||||||||
| 71 | use OpenSky::API::Utils::Iterator; | ||||||||||
| 72 | |||||||||||
| 73 | my $results = OpenSky::API::Utils::Iterator->new( rows => [ 1, 2, 3 ] ); | ||||||||||
| 74 | |||||||||||
| 75 | while ( my $result = $results->next ) { | ||||||||||
| 76 | ... | ||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | =head1 DESCRIPTION | ||||||||||
| 80 | |||||||||||
| 81 | A simple iterator class. To keep it dead simple, it only allows defined values | ||||||||||
| 82 | to be passed in. | ||||||||||
| 83 | |||||||||||
| 84 | =head1 METHODS | ||||||||||
| 85 | |||||||||||
| 86 | =head2 C<next> | ||||||||||
| 87 | |||||||||||
| 88 | while ( my $result = $results->next ) { | ||||||||||
| 89 | ... | ||||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | Returns the next member in the iterator. Returns C<undef> if the iterator is | ||||||||||
| 93 | exhausted. | ||||||||||
| 94 | |||||||||||
| 95 | =head2 C<count> | ||||||||||
| 96 | |||||||||||
| 97 | if ( $results->count ) { | ||||||||||
| 98 | ... | ||||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | Returns the number of members in the iterator. | ||||||||||
| 102 | |||||||||||
| 103 | =head2 C<first> | ||||||||||
| 104 | |||||||||||
| 105 | my $object = $results->first; | ||||||||||
| 106 | |||||||||||
| 107 | Returns the first object in the results. | ||||||||||
| 108 | |||||||||||
| 109 | =head2 C<reset> | ||||||||||
| 110 | |||||||||||
| 111 | $results->reset; | ||||||||||
| 112 | |||||||||||
| 113 | Resets the iterator to point to the first member. | ||||||||||
| 114 | |||||||||||
| 115 | =head2 C<all> | ||||||||||
| 116 | |||||||||||
| 117 | my @objects = $results->all; | ||||||||||
| 118 | |||||||||||
| 119 | Returns a list of all members in the iterator. | ||||||||||
| 120 | |||||||||||
| 121 | =head1 AUTHOR | ||||||||||
| 122 | |||||||||||
| 123 | Curtis "Ovid" Poe <curtis.poe@gmail.com> | ||||||||||
| 124 | |||||||||||
| 125 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 126 | |||||||||||
| 127 | This software is Copyright (c) 2023 by Curtis "Ovid" Poe. | ||||||||||
| 128 | |||||||||||
| 129 | This is free software, licensed under: | ||||||||||
| 130 | |||||||||||
| 131 | The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 132 | |||||||||||
| 133 | =cut | ||||||||||