| 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
| |||||||||||||||||||||||||||||
| 57 | unless Ref::Util::is_coderef( $code ); | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 4 | ▶ | throw_failure( parameter => "iterator (@{[ $iterator->_name ]}) must provide a freeze method" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | unless can_meth( $iterator, FREEZE ); | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 3 | ▶ | throw_failure( | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
can_meth($iterator, "set_exhausted") and can_meth($iterator, "is_exhausted")
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")
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 117 | 12 | ▶ | next if $@ ne q{}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 118 | 12 | ▶ | last if $src->does( $role ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | undef $role; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 12 | ▶ | throw_failure( class => "unable to find role for '$meth' capability for @{[ $src->_name ]}" ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||