Interact/Role/Iterator.pm

filter: Statementstmt 31.1% 14 / 45 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 21.4% 3 / 14 Podpod 100% 11 / 11 totaltotal 32.1% 28 / 87 CC 8 SCAR 20.1
CC
8
Cov
23%
idx
20.0
next
12.0
item_at
6.0
CRAP 7.5
« Interact/Action.pm ↑ Summary Interact/Transport/Serial.pm »
1 package Net::CLI::Interact::Role::Iterator;
2 {
3 1 $Net::CLI::Interact::Role::Iterator::VERSION = '2.143070';
4 }
5  
6 1 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Sub::Quote;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use MooX::Types::MooseLike::Base qw(ArrayRef Any Int);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 has '_sequence' => (
11 is => 'rw',
12 isa => ArrayRef[Any],
13 required => 1,
14 );
15  
16 # fiddly only in case of auto_deref
17 0 ▶sub count { return scalar @{ (shift)->_sequence } }
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
18  
19 0 ▶sub first { return (shift)->_sequence->[0] }
Subroutine first: not called CC=1 SCAR=6.9
Pod documented
20 0 ▶sub last { return (shift)->_sequence->[-1] }
Subroutine last: not called CC=1 SCAR=6.9
Pod documented
21  
22 sub item_at {
23 0 ▶ my ($self, $pos) = @_;
Subroutine item_at: not called CC=2 SCAR=17.9
Pod documented
24 0 ▶ die "position is past the end of sequence\n"
Branch: 1 branch
BranchTrueFalseTotal
if $pos >= $self->count 0 0 0
25 if $pos >= $self->count;
26 0 return $self->_sequence->[$pos];
27 }
28  
29 sub insert_at {
30 0 ▶ my ($self, $pos, @rest) = @_;
Subroutine insert_at: not called CC=1 SCAR=6.9
Pod documented
31 0 my @seq = @{ $self->_sequence };
32 0 splice @seq, $pos, 0, @rest;
33 0 $self->_sequence( \@seq );
34 }
35  
36 sub append {
37 0 ▶ my $self = shift;
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
38 0 $self->insert_at( $self->count, @{ (shift)->_sequence } );
39 }
40  
41 1 has '_position' => (
42 is => 'rw',
43 isa => Int,
44 default => quote_sub('-1'),
45 );
46  
47 sub idx {
48 0 ▶ my $self = shift;
Subroutine idx: not called CC=4 SCAR=30.0
Pod documented
49 0 my $pos = $self->_position;
50 0 ▶ die "attempt to read iter index before pulling a value\n"
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ == 0 and $pos == -1 0 0 0
Condition0%0/3
scalar @_ == 0 and $pos == -1
!ll&&!rl&&r
000
MC/DC0%0/2
scalar @_ == 0 and $pos == -1
scalar @_ == 0 $pos == -1
Truth table0%0/3
A and B
A= scalar @_ == 0B= $pos == -1
ABresult
0X0
100
111
51 if scalar @_ == 0 and $pos == -1;
52 0 ▶ $self->_position(shift) if scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @_ 0 0 0
53 0 return $pos;
54 }
55  
56 sub next {
57 0 ▶ my $self = shift;
Subroutine next: not called CC=3 SCAR=24.8
Pod documented
58 0 ▶ die "er, please check has_next before next\n"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->has_next 0 0 0
59 if not $self->has_next;
60  
61 0 my $position = $self->_position;
62 0 ▶ die "fell off end of iterator\n"
Branch: 1 branch
BranchTrueFalseTotal
if ++$position == $self->count 0 0 0
63 if ++$position == $self->count;
64  
65 0 $self->_position($position);
66 0 return $self->_sequence->[ $position ];
67 }
68  
69 sub has_next {
70 0 ▶ my $self = shift;
Subroutine has_next: not called CC=1 SCAR=6.9
Pod documented
71 0 return ($self->_position < ($self->count - 1));
72 }
73  
74 sub peek {
75 0 ▶ my $self = shift;
Subroutine peek: not called CC=2 SCAR=17.9
Pod documented
76 0 ▶ return $self->_sequence->[ $self->_position + 1 ]
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_next 0 0 0
77 if $self->has_next;
78 }
79  
80 0 ▶sub reset { (shift)->_position(-1) }
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
81  
82 1 1;
83  
84 # ABSTRACT: Array-based Iterator
85  
86  
87 __END__
88 =pod
89  
90 =head1 NAME
91  
92 Net::CLI::Interact::Role::Iterator - Array-based Iterator
93  
94 =head1 VERSION
95  
96 version 2.143070
97  
98 =head1 SYNOPSIS
99  
100 my $count = $iter->count;
101
102 $iter->reset;
103 while ( $iter->has_next ) {
104 print $iter->next;
105 }
106  
107 =head1 DESCRIPTION
108  
109 This module implements an array-based iterator which may be mixed-in to add
110 management of a sequence of elements and processing of that sequence.
111  
112 The iterator is inspired by L<MooseX::Iterator> but limited to arrays and adds
113 many other facilities. The following section describes the methods provided by
114 this class.
115  
116 =head1 USAGE
117  
118 The slot used for storing iterator elements is named C<_sequence> and you
119 should write your consuming class to marshall data into this slot, perhaps via
120 C<BUILD> or C<init_arg>. For example:
121  
122 has '+_sequence' => (
123 isa => 'ArrayRef[Thingy]',
124 init_arg => 'things',
125 );
126  
127 =head1 INTERFACE
128  
129 =head2 count
130  
131 The number of elements currently stored in the iterator. Note that this is of
132 course not the same as the index of the last item in the iterator (which is
133 0-based)
134  
135 =head2 first
136  
137 Returns the first item in the iterator.
138  
139 =head2 last
140  
141 Returns the last item in the iterator.
142  
143 =head2 item_at( $pos )
144  
145 Returns the item at the given position in the iterator, or throws an exception
146 if C<$pos> is past the end of the iterator. The position is 0-based.
147  
148 =head2 insert_at( $pos, $iter )
149  
150 Inserts the contents of the passed iterator starting I<at> (not I<after>) the
151 position given. The passed iterator must also be a consumer of this role. The
152 position is 0-based.
153  
154 =head2 append( $iter )
155  
156 Shorthand for C<insert_at> when you want to add the contents of the passed
157 iterator I<after> the end of the sequence.
158  
159 =head2 idx( $pos? )
160  
161 Returns the index (0-based) of the current iterator cursor, or sets the
162 cursor if a position (again, 0-based) is passed.
163  
164 An exception is thrown if you attempt to read the cursor position before
165 having read any elements from the iterator, or if the iterator is empty.
166  
167 =head2 next
168  
169 Returns the next item in the iterator sequence, and advances the cursor.
170 Throws an exception if you have already reached the end of the sequence.
171  
172 =head2 has_next
173  
174 Returns true if there are further elements to be read from the iterator.
175  
176 =head2 peek
177  
178 Returns the next item in the sequence without advancing the position of the
179 cursor. It returns C<undef> if you are already at the end of the sequence.
180  
181 =head2 reset
182  
183 Resets the cursor so you can iterate through the sequence of elements again.
184  
185 =head1 AUTHOR
186  
187 Oliver Gorwits <oliver@cpan.org>
188  
189 =head1 COPYRIGHT AND LICENSE
190  
191 This software is copyright (c) 2014 by Oliver Gorwits.
192  
193 This is free software; you can redistribute it and/or modify it under
194 the same terms as the Perl 5 programming language system itself.
195  
196 =cut
197  
« Interact/Action.pm ↑ Summary Interact/Transport/Serial.pm »