blib/lib/Iterator/Flex/Freeze.pm

filter: Statementstmt 95.7% 68 / 71 Branchbran 59.0% 13 / 22 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 14 / 14 Podpod 50.0% 1 / 2 totaltotal 85.0% 97 / 114 CC 16 SCAR 17.3
CC
16
Cov
85%
construct
10.7
new
7.1
__ANON__
2.0
CRAP 5.7
« blib/lib/Iterator/Flex/Take.pm ↑ Summary blib/lib/Iterator/Flex/Sequence.pm »
1 package Iterator::Flex::Freeze;
2  
3 # ABSTRACT: Freeze an iterator after every next
4  
5 2 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 our $VERSION = '0.33';
11  
12 2 ▶use Iterator::Flex::Factory 'to_iterator';
Subroutine BEGIN: called CC=1 SCAR=0.0
13 use Iterator::Flex::Utils
14 2 ▶ qw( RETURN EXHAUSTION :IterAttrs :ExhaustedMethods can_meth load_role throw_failure );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use parent 'Iterator::Flex::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use Ref::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 2 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 4 ▶sub new ( $class, $code, $iterator, $pars = {} ) {
Subroutine new: called CC=6 SCAR=19.6
Pod undocumented
55  
56 4 ▶ throw_failure( parameter => q{'serialize' parameter is not a coderef} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($code) 0 4 4
57 unless Ref::Util::is_coderef( $code );
58  
59 4 ▶ throw_failure( parameter => "iterator (@{[ $iterator->_name ]}) must provide a freeze method" )
Branch: 1 branch
BranchTrueFalseTotal
unless can_meth($iterator, "freeze") 1 3 4
60 unless can_meth( $iterator, FREEZE );
61  
62 3 ▶ throw_failure(
Branch: 1 branch
BranchTrueFalseTotal
unless can_meth($iterator, "set_exhausted") and can_meth($iterator, "is_exhausted") 0 3 3
Condition33%1/3
can_meth($iterator, "set_exhausted") and can_meth($iterator, "is_exhausted")
!ll&&!rl&&r
003
MC/DC0%0/2
can_meth($iterator, "set_exhausted") and can_meth($iterator, "is_exhausted")
can_meth($iterator, "set_exhausted") can_meth($iterator, "is_exhausted")
Truth table33%1/3
A and B
A= can_meth($iterator, "set_exhausted")B= can_meth($iterator, "is_exhausted")
ABresult
0X0
100
111
63 0 parameter => "iterator (@{[ $iterator->_name ]}) must provide set_exhausted/is_exhausted methods" )
64 unless can_meth( $iterator, SET_EXHAUSTED )
65 && can_meth( $iterator, IS_EXHAUSTED );
66  
67 3 $class->SUPER::new( { serialize => $code, src => $iterator }, $pars );
68 }
69  
70  
71 ▶sub construct ( $class, $state ) {
Subroutine construct: called CC=10 SCAR=23.7
Pod documented
72  
73 3 ▶ throw_failure( parameter => q{'state' parameter must be a HASH reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_hashref($state) 0 3 3
74 unless Ref::Util::is_hashref( $state );
75  
76 3 my ( $serialize, $src ) = @{$state}{qw( serialize src )};
77  
78 3 ▶ throw_failure( parameter => q{'serialize' must be a CODE reference} )
Branch: 1 branch
BranchTrueFalseTotal
unless Ref::Util::XS::is_coderef($serialize) 0 3 3
79 unless Ref::Util::is_coderef( $serialize );
80  
81 # wrap the source iterator so that it returns undef on exhaustion.
82 3 $src
83 = to_iterator( $src, { ( +EXHAUSTION ) => RETURN } );
84  
85 3 my $self;
86 my %params = (
87 ( +_NAME ) => 'freeze',
88  
89 ( +_SELF ) => \$self,
90  
91 ( +_DEPENDS ) => $src,
92 ( +NEXT ) => sub {
93 24 ▶ my $value = $src->();
Subroutine __ANON__: called CC=2 SCAR=6.9
94 24 local $_ = $src->freeze;
95 24 &$serialize();
96 24 ▶ $value = $self->signal_exhaustion if $src->is_exhausted;
Branch: 1 branch
BranchTrueFalseTotal
if $src->is_exhausted 8 16 24
97 24 return $value;
98 },
99 3 );
100  
101 3 Scalar::Util::weaken $src;
102 3 $params{ +_ROLES } = [];
103 3 for my $meth ( PREV, CURRENT, REWIND, RESET ) {
104 12 ▶ next unless $src->may( $meth );
Branch: 1 branch
BranchTrueFalseTotal
unless $src->may($meth) 0 12 12
105 12 my $sub = $src->can( $meth );
106 12 Scalar::Util::weaken $sub;
107 $params{$meth} = sub {
108 25 ▶ $src->$sub();
Subroutine __ANON__: called CC=1 SCAR=0.0
109 12 };
110  
111 # figure out which role was used to describe the capability
112 12 my $Umeth = ucfirst $meth;
113 12 my $role;
114 12 for my $suffix ( 'Closure', 'Method' ) {
115 $role
116 12 ▶ = eval { load_role( $suffix ? $Umeth . q{::} . $suffix : $Umeth, $class->_role_namespaces ); };
Branch: 1 branch
BranchTrueFalseTotal
$suffix ? : 12 0 12
117 12 ▶ next if $@ ne q{};
Branch: 1 branch
BranchTrueFalseTotal
if $@ ne "" 0 12 12
118 12 ▶ last if $src->does( $role );
Branch: 1 branch
BranchTrueFalseTotal
if $src->does($role) 12 0 12
119 0 undef $role;
120 }
121  
122 12 ▶ throw_failure( class => "unable to find role for '$meth' capability for @{[ $src->_name ]}" )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $role 0 12 12
123 unless defined $role;
124  
125  
126 # need '+' as role names are fully qualified
127 12 push $params{ +_ROLES }->@*, q{+} . $role;
128 }
129  
130  
131 3 return \%params;
132 }
133  
134 2 __PACKAGE__->_add_roles( qw[
135 State::Registry
136 Next::ClosedSelf
137 ] );
138  
139 2 1;
140  
141 #
142 # This file is part of Iterator-Flex
143 #
144 # This software is Copyright (c) 2018 by Smithsonian Astrophysical Observatory.
145 #
146 # This is free software, licensed under:
147 #
148 # The GNU General Public License, Version 3, June 2007
149 #
150  
151 __END__
« blib/lib/Iterator/Flex/Take.pm ↑ Summary blib/lib/Iterator/Flex/Sequence.pm »