File Coverage

blib/lib/Attean/ListIterator.pm
Criterion Covered Total %
statement 32 32 100.0
branch 2 2 100.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 47 47 100.0


line stmt bran cond sub pod time code
1 50     50   525 use v5.14;
  50         198  
2 50     50   229 use warnings;
  50         84  
  50         2129  
3              
4             =head1 NAME
5              
6             Attean::ListIterator - Iterator implementation backed by a list/array of values
7              
8             =head1 VERSION
9              
10             This document describes Attean::ListIterator version 0.032
11              
12             =head1 SYNOPSIS
13              
14             use v5.14;
15             use Attean;
16             my @values = map { Attean::Literal->new($_) } (1,2,3);
17             my $iter = Attean::ListIterator->new(
18             values => \@values,
19             item_type => 'Attean::API::Term',
20             );
21            
22             say $iter->next->value; # 1
23             say $iter->next->value; # 2
24             say $iter->next->value; # 3
25              
26             =head1 DESCRIPTION
27              
28             The Attean::ListIterator class represents a typed iterator.
29             It conforms to the L<Attean::API::RepeatableIterator|Attean::API::Iterator> role.
30              
31             The Attean::ListIterator constructor requires two named arguments:
32              
33             =over 4
34              
35             =item values
36              
37             An array reference containing the items to iterate over.
38              
39             =item item_type
40              
41             A string representing the type of the items that will be returned from the
42             iterator.
43              
44             =back
45              
46             =head1 METHODS
47              
48             =over 4
49              
50             =cut
51              
52             use Moo;
53 50     50   274 use Scalar::Util qw(blessed);
  50         135  
  50         308  
54 50     50   14707 use Type::Tiny::Role;
  50         117  
  50         2182  
55 50     50   302 use Types::Standard qw(ArrayRef Int);
  50         93  
  50         1858  
56 50     50   314 use namespace::clean;
  50         141  
  50         446  
57 50     50   39030
  50         105  
  50         341  
58             has values => (is => 'ro', isa => ArrayRef, required => 1);
59             has current => (is => 'rw', isa => Int, init_arg => undef, default => 0);
60            
61             my $self = shift;
62             my $role = $self->item_type;
63             foreach my $item (@{ $self->values }) {
64             if (Role::Tiny->is_role($role)) {
65             die "ListIterator item <$item> is not a $role" unless (blessed($item) and $item->does($role));
66             }
67             }
68             }
69              
70             =item C<< reset >>
71              
72             Resets the iterator's internal state so that iteration begins again at the
73             beginning of the values array.
74              
75             =cut
76              
77             my $self = shift;
78             $self->current(0);
79             }
80 807     807 1 973
81 807         10074 =item C<< next >>
82              
83             Returns the iterator's next item, or undef upon reaching the end of iteration.
84              
85             =cut
86              
87             my $self = shift;
88             my $list = $self->values;
89             my $index = $self->current;
90             my $item = $list->[$index];
91 4704     4704 1 230996 return unless defined($item);
92 4704         7488 $self->current(1+$index);
93 4704         72101 return $item;
94 4704         22888 }
95 4704 100       10099  
96 3603         48668 =item C<< size >>
97 3603         77924  
98             Returns the number of elements still remaining in the iterator until it is
99             fully consumed or until C<< reset >> is called.
100              
101             =cut
102              
103             my $self = shift;
104             return scalar(@{ $self->values }) - $self->current;
105             }
106            
107             with 'Attean::API::RepeatableIterator';
108 1     1 1 3 }
109 1         2  
  1         26  
110             1;
111              
112              
113             =back
114              
115             =head1 BUGS
116              
117             Please report any bugs or feature requests to through the GitHub web interface
118             at L<https://github.com/kasei/attean/issues>.
119              
120             =head1 SEE ALSO
121              
122              
123              
124             =head1 AUTHOR
125              
126             Gregory Todd Williams C<< <gwilliams@cpan.org> >>
127              
128             =head1 COPYRIGHT
129              
130             Copyright (c) 2014--2022 Gregory Todd Williams.
131             This program is free software; you can redistribute it and/or modify it under
132             the same terms as Perl itself.
133              
134             =cut