File Coverage

blib/lib/Iterator/Flex/Chunk.pm
Criterion Covered Total %
statement 56 56 100.0
branch 10 12 83.3
condition 10 14 71.4
subroutine 13 13 100.0
pod 1 2 50.0
total 90 97 92.7


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