blib/lib/OpenSky/API/Utils/Iterator.pm untested

filter: Statementstmt 0.0% 0 / 25 Branchbran n/a Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 40 CC 1 SCAR 6.9
CC
1
Cov
0%
first
2.0
next
2.0
reset
2.0
CRAP 2.0
1 # ABSTARCT: Internal iterator class for OpenSky::API
2  
3 package OpenSky::API::Utils::Iterator;
4  
5 our $VERSION = '0.004';
6 use Moose;
7 use OpenSky::API::Types qw(
8 ArrayRef
9 Defined
10 InstanceOf
11 PositiveOrZeroInt
12 );
13 use experimental qw(signatures);
14  
15 has '_rows' => (
16 is => 'ro',
17 isa => ArrayRef [Defined],
18 init_arg => 'rows',
19 );
20  
21 has '_index' => (
22 is => 'rw',
23 writer => '_set_index',
24 isa => PositiveOrZeroInt,
25 default => 0,
26 );
27  
28 sub first ($self) {
29 return $self->_rows->[0];
30 }
31  
32 sub next ($self) {
33 my $i = $self->_index;
34 my $next = $self->_rows->[$i] or return;
35 $self->_set_index( $i + 1 );
36 return $next;
37 }
38  
39 sub reset ($self) {
40 $self->_set_index(0);
41 return 1;
42 }
43  
44 sub all ($self) {
45 return @{ $self->_rows };
46 }
47  
48 sub count ($self) {
49 my @all = $self->all;
50 return scalar @all;
51 }
52  
53 __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