| 1 | # Copyright (c) 2024-2026 Philipp Schafft | ||||||||||||
| 2 | |||||||||||||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||||||||||||
| 4 | |||||||||||||
| 5 | # ABSTRACT: Work with Tag databases | ||||||||||||
| 6 | |||||||||||||
| 7 | package Data::TagDB::Iterator; | ||||||||||||
| 8 | |||||||||||||
| 9 | 4 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | |||||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | |||||||||||||
| 15 | 4 | our $VERSION = v0.16; | |||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 20 | 0 | my ($pkg, %opts) = @_; | |||||||||||
| 21 | |||||||||||||
| 22 | 0 | ▶ | croak 'Missing required member: db' unless defined $opts{db}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 23 | |||||||||||||
| 24 | 0 | return bless \%opts, $pkg; | |||||||||||
| 25 | } | ||||||||||||
| 26 | |||||||||||||
| 27 | |||||||||||||
| 28 | 0 | ▶ | sub from_array { | ||||||||||
|
Subroutine
from_array: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 29 | 0 | my ($pkg, $array, %opts) = @_; | |||||||||||
| 30 | 0 | return Data::TagDB::Iterator::_Array->new(%opts, array => $array); | |||||||||||
| 31 | } | ||||||||||||
| 32 | |||||||||||||
| 33 | |||||||||||||
| 34 | 0 | ▶ | sub db { | ||||||||||
|
Subroutine
db: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 35 | 0 | my ($self) = @_; | |||||||||||
| 36 | 0 | return $self->{db}; | |||||||||||
| 37 | } | ||||||||||||
| 38 | |||||||||||||
| 39 | |||||||||||||
| 40 | 0 | ▶ | sub next { | ||||||||||
|
Subroutine
next: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 41 | 0 | my ($self) = @_; | |||||||||||
| 42 | 0 | confess 'Not implemented'; | |||||||||||
| 43 | } | ||||||||||||
| 44 | |||||||||||||
| 45 | |||||||||||||
| 46 | 0 | ▶ | sub finish { | ||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 47 | 0 | my ($self) = @_; | |||||||||||
| 48 | 0 | confess 'Not implemented'; | |||||||||||
| 49 | } | ||||||||||||
| 50 | |||||||||||||
| 51 | |||||||||||||
| 52 | |||||||||||||
| 53 | 0 | ▶ | sub foreach { | ||||||||||
|
Subroutine
foreach: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 54 | 0 | my ($self, $code) = @_; | |||||||||||
| 55 | |||||||||||||
| 56 | 0 | ▶ | while (defined(my $ent = $self->next)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 0 | $code->($ent); | |||||||||||
| 58 | } | ||||||||||||
| 59 | |||||||||||||
| 60 | 0 | $self->finish; | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | |||||||||||||
| 64 | 0 | ▶ | sub one { | ||||||||||
|
Subroutine
one: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 65 | 0 | my ($self) = @_; | |||||||||||
| 66 | 0 | my $ent = $self->next; | |||||||||||
| 67 | 0 | $self->finish; | |||||||||||
| 68 | |||||||||||||
| 69 | 0 | ▶ | return $ent // croak 'No entry'; | ||||||||||
|
Condition0%0/2
$ent // croak("No entry")
MC/DC0%0/1
$ent // croak("No entry")
$ent
Truth table0%0/2
A
A= $ent
| |||||||||||||
| 70 | } | ||||||||||||
| 71 | |||||||||||||
| 72 | |||||||||||||
| 73 | 0 | ▶ | sub none { | ||||||||||
|
Subroutine
none: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 74 | 0 | my ($self) = @_; | |||||||||||
| 75 | 0 | my $ent = $self->next; | |||||||||||
| 76 | |||||||||||||
| 77 | 0 | $self->finish; | |||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | croak 'Iterator non-empty' if defined $ent; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | } | ||||||||||||
| 81 | |||||||||||||
| 82 | |||||||||||||
| 83 | 0 | ▶ | sub map { | ||||||||||
|
Subroutine
map: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 84 | 0 | my ($self, $apply, %opts) = @_; | |||||||||||
| 85 | |||||||||||||
| 86 | 0 | return Data::TagDB::Iterator::_Mapped->new($self, $apply); | |||||||||||
| 87 | } | ||||||||||||
| 88 | |||||||||||||
| 89 | |||||||||||||
| 90 | 0 | ▶ | sub collect { | ||||||||||
|
Subroutine
collect: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||
| 91 | 0 | my ($self, $apply, %opts) = @_; | |||||||||||
| 92 | 0 | my @ret; | |||||||||||
| 93 | |||||||||||||
| 94 | 0 | ▶ | if (defined $apply) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 0 | ▶ | unless (ref $apply) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 0 | my $funcname = $apply; | |||||||||||
| 97 | 0 | ▶ | $apply = sub { $_[0]->can($funcname)->(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 98 | } | ||||||||||||
| 99 | } | ||||||||||||
| 100 | |||||||||||||
| 101 | 0 | ▶ | if (defined($apply)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 102 | 0 | ▶ | if ($opts{skip_died}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 103 | 0 | ▶ | while (defined(my $ent = $self->next)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 104 | 0 | $ent = eval { $ent->$apply() }; | |||||||||||
| 105 | 0 | ▶ | push(@ret, $ent) unless $@; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 106 | } | ||||||||||||
| 107 | } else { | ||||||||||||
| 108 | 0 | ▶ | while (defined(my $ent = $self->next)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 109 | 0 | push(@ret, $ent->$apply()); | |||||||||||
| 110 | } | ||||||||||||
| 111 | } | ||||||||||||
| 112 | } else { | ||||||||||||
| 113 | 0 | ▶ | while (defined(my $ent = $self->next)) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | 0 | push(@ret, $ent); | |||||||||||
| 115 | } | ||||||||||||
| 116 | } | ||||||||||||
| 117 | |||||||||||||
| 118 | 0 | ▶ | return \@ret if $opts{return_ref}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 119 | |||||||||||||
| 120 | 0 | return @ret; | |||||||||||
| 121 | } | ||||||||||||
| 122 | |||||||||||||
| 123 | 4 | package Data::TagDB::Iterator::_Mapped { | |||||||||||
| 124 | 4 | ▶ | use parent -norequire, 'Data::TagDB::Iterator'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 125 | |||||||||||||
| 126 | 0 | ▶ | sub new { | ||||||||||
|
Subroutine
new: not called CC=2 SCAR=17.9
| |||||||||||||
| 127 | 0 | my ($pkg, $parent, $apply) = @_; | |||||||||||
| 128 | |||||||||||||
| 129 | 0 | ▶ | unless (ref $apply) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 130 | 0 | my $funcname = $apply; | |||||||||||
| 131 | 0 | ▶ | $apply = sub { $_[0]->can($funcname)->(@_) }; | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 132 | } | ||||||||||||
| 133 | |||||||||||||
| 134 | 0 | return $pkg->SUPER::new(db => $parent->db, parent => $parent, apply => $apply); | |||||||||||
| 135 | } | ||||||||||||
| 136 | |||||||||||||
| 137 | 0 | ▶ | sub next { | ||||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
| |||||||||||||
| 138 | 0 | my ($self, @args) = @_; | |||||||||||
| 139 | 0 | my $ent = $self->{parent}->next(@args); | |||||||||||
| 140 | 0 | my $apply = $self->{apply}; | |||||||||||
| 141 | |||||||||||||
| 142 | 0 | ▶ | return undef unless defined $ent; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 143 | |||||||||||||
| 144 | 0 | return $ent->$apply(); | |||||||||||
| 145 | } | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | ▶ | sub finish { | ||||||||||
|
Subroutine
finish: not called CC=1 SCAR=6.9
| |||||||||||||
| 148 | 0 | my ($self, @args) = @_; | |||||||||||
| 149 | 0 | return $self->{parent}->finish(@args); | |||||||||||
| 150 | } | ||||||||||||
| 151 | }; | ||||||||||||
| 152 | |||||||||||||
| 153 | 4 | package Data::TagDB::Iterator::_Array { | |||||||||||
| 154 | 4 | ▶ | use parent -norequire, 'Data::TagDB::Iterator'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 155 | |||||||||||||
| 156 | 0 | ▶ | sub next { | ||||||||||
|
Subroutine
next: not called CC=2 SCAR=17.9
| |||||||||||||
| 157 | 0 | my ($self) = @_; | |||||||||||
| 158 | 0 | ▶ | $self->{index} //= 0; | ||||||||||
|
Condition0%0/2
$self->{"index"} //= 0
MC/DC0%0/1
$self->{"index"} //= 0
$self->{"index"}
Truth table0%0/2
A
A= $self->{"index"}
| |||||||||||||
| 159 | |||||||||||||
| 160 | 0 | return $self->{array}[$self->{index}++]; | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | 0 | ▶ | sub finish {} | ||||||||||
|
Subroutine
finish: not called CC=1 SCAR=0.0
| |||||||||||||
| 164 | }; | ||||||||||||
| 165 | |||||||||||||
| 166 | 4 | 1; | |||||||||||
| 167 | |||||||||||||
| 168 | __END__ | ||||||||||||