File Coverage

blib/lib/Iterator/Flex/Chunk.pm
Criterion Covered Total %
statement 55 55 100.0
branch 8 10 80.0
condition 4 8 50.0
subroutine 13 13 100.0
pod 1 2 50.0
total 81 88 92.0


line stmt bran cond sub pod time code
1             package Iterator::Flex::Chunk;
2              
3             # ABSTRACT: Chunk Iterator Class
4              
5 3     3   244137 use v5.28;
  3         14  
6 3     3   22 use strict;
  3         8  
  3         94  
7 3     3   19 use warnings;
  3         8  
  3         251  
8 3     3   696 use experimental 'signatures';
  3         2116  
  3         22  
9              
10             our $VERSION = '0.33';
11              
12 3     3   1396 use Iterator::Flex::Factory 'to_iterator';
  3         9  
  3         361  
13 3     3   23 use Iterator::Flex::Utils qw[ THROW STATE EXHAUSTION :IterAttrs :IterStates ];
  3         8  
  3         705  
14 3     3   25 use Ref::Util;
  3         67  
  3         186  
15 3     3   19 use parent 'Iterator::Flex::Base';
  3         7  
  3         30  
16              
17 3     3   315 use namespace::clean;
  3         8  
  3         65  
18              
19              
20              
21              
22              
23              
24              
25              
26              
27              
28              
29              
30              
31              
32              
33              
34              
35              
36              
37              
38              
39              
40              
41              
42              
43              
44              
45              
46              
47              
48              
49              
50              
51              
52              
53              
54 3     3 1 7 sub new ( $class, $iterable, $pars = {} ) {
  3         8  
  3         6  
  3         7  
  3         6  
55              
56 3         13 my %pars = $pars->%*;
57 3   50     14 my $capacity = delete $pars{capacity} // 1;
58 3         24 $class->SUPER::new( {
59             capacity => $capacity,
60             src => $iterable,
61             },
62             \%pars,
63             );
64             }
65              
66 3     3 0 7 sub construct ( $class, $state ) {
  3         6  
  3         6  
  3         5  
67              
68 3 50       14 throw_failure( parameter => q{'state' parameter must be a HASH reference} )
69             unless Ref::Util::is_hashref( $state );
70              
71 3         7 my ( $src, $capacity ) = @{$state}{qw[ src capacity ]};
  3         9  
72              
73 3         24 $src
74             = to_iterator( $src, { ( +EXHAUSTION ) => THROW } );
75              
76 3         11 my $self;
77             my $iterator_state;
78              
79             # This iterator may have to delay signalling exhaustion for one
80             # cycle if the input iterator is exhausted and the current chunk
81             # is not empty
82 3         8 my $next_is_exhausted = !!0;
83              
84             return {
85             ( +_NAME ) => 'ichunk',
86              
87             ( +_SELF ) => \$self,
88              
89             ( +STATE ) => \$iterator_state,
90              
91             ( +NEXT ) => sub {
92 16 100 66 16   228 return $self->signal_exhaustion
93             if $iterator_state == IterState_EXHAUSTED || $next_is_exhausted;
94              
95 14         31 my @chunked;
96             eval {
97 14         115 push @chunked, $src->() while @chunked < $capacity;
98 10         67 1;
99 14 100       24 } or do {
100 4 50 33     364 die $@
101             unless Ref::Util::is_blessed_ref( $@ )
102             && $@->isa( 'Iterator::Flex::Failure::Exhausted' );
103              
104 4 100       19 return $self->signal_exhaustion if !@chunked;
105 2         5 $next_is_exhausted = !!1;
106             };
107 12         131 return \@chunked;
108             },
109             ( +RESET ) => sub {
110 4     4   30 $next_is_exhausted = !!0;
111             },
112 3         62 ( +_DEPENDS ) => $src,
113             };
114             }
115              
116             __PACKAGE__->_add_roles( qw[
117             State::Closure
118             Next::ClosedSelf
119             Reset::Closure
120             ] );
121              
122             1;
123              
124             #
125             # This file is part of Iterator-Flex
126             #
127             # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
128             #
129             # This is free software, licensed under:
130             #
131             # The GNU General Public License, Version 3, June 2007
132             #
133              
134             __END__