| 1 | package TableDataRole::Munge::Filter; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use Log::ger; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 3 | ▶ | use Role::Tiny; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 3 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 11 | 3 | our $DATE = '2024-05-14'; # DATE | |||||||||||||||||||||||||||
| 12 | 3 | our $DIST = 'TableDataRoles-Standard'; # DIST | |||||||||||||||||||||||||||
| 13 | 3 | our $VERSION = '0.025'; # VERSION | |||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 3 | with 'TableDataRole::Spec::Basic'; | |||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | 2 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=26.0
Pod
documented
| |||||||||||||||||||||||||||||
| 18 | 2 | require Module::Load::Util; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 2 | my ($class, %args) = @_; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | my $tabledata = delete $args{tabledata} | ||||||||||||||||||||||||||||
| 23 | 2 | ▶ | or die "Please supply 'tabledata' argument"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 24 | 2 | my $filter = delete $args{filter}; | |||||||||||||||||||||||||||
| 25 | 2 | my $filter_hashref = delete $args{filter_hashref}; | |||||||||||||||||||||||||||
| 26 | 2 | ▶ | ($filter || $filter_hashref) or die "Please supply 'filter' or 'filter_hashref' argument"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$filter or $filter_hashref
MC/DC0%0/2
$filter or $filter_hashref
$filter
$filter_hashref
Truth table66%2/3
A or B
A= $filterB= $filter_hashref
| |||||||||||||||||||||||||||||
| 27 | 2 | for ($filter, $filter_hashref) { | |||||||||||||||||||||||||||
| 28 | 4 | ▶ | next unless defined; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | 2 | ▶ | unless (ref $_ eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 0 | my $code = "package main; sub { no strict; no warnings; $_ }"; | |||||||||||||||||||||||||||
| 31 | 0 | log_trace "Eval-ing: $code"; | |||||||||||||||||||||||||||
| 32 | 0 | $_ = eval $code; ## no critic: BuiltinFunctions::ProhibitStringyEval | |||||||||||||||||||||||||||
| 33 | 0 | ▶ | die if $@; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||
| 35 | } | ||||||||||||||||||||||||||||
| 36 | 2 | ▶ | my $load = delete($args{load}) // 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $args{"load"} // 1
MC/DC0%0/1
delete $args{"load"} // 1
delete $args{"load"}
Truth table50%1/2
A
A= delete $args{"load"}
| |||||||||||||||||||||||||||||
| 37 | 2 | ▶ | die "Unknown argument(s): ". join(", ", sort keys %args) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | if keys %args; | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 2 | $tabledata = Module::Load::Util::instantiate_class_with_optional_args({load=>$load, ns_prefix=>"TableData"}, $tabledata); | |||||||||||||||||||||||||||
| 41 | 2 | my $column_names = $tabledata->get_column_names; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | bless { | ||||||||||||||||||||||||||||
| 44 | tabledata => $tabledata, | ||||||||||||||||||||||||||||
| 45 | column_names => $column_names, | ||||||||||||||||||||||||||||
| 46 | 2 | column_idxs => {map {$column_names->[$_] => $_} 0..$#{$column_names}}, | |||||||||||||||||||||||||||
| 47 | filter => $filter, | ||||||||||||||||||||||||||||
| 48 | filter_hashref => $filter_hashref, | ||||||||||||||||||||||||||||
| 49 | pos => 0, # iterator | ||||||||||||||||||||||||||||
| 50 | # buffer => undef, | ||||||||||||||||||||||||||||
| 51 | }, $class; | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 2 | ▶ | sub get_column_count { | ||||||||||||||||||||||||||
|
Subroutine
get_column_count: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 55 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 2 | scalar @{ $self->{column_names} }; | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | sub get_column_names { | ||||||||||||||||||||||||||
|
Subroutine
get_column_names: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 61 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 62 | 2 | ▶ | wantarray ? @{ $self->{column_names} } : $self->{column_names}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 52 | ▶ | sub _fill_buffer { | ||||||||||||||||||||||||||
|
Subroutine
_fill_buffer: called CC=6 SCAR=17.9
| |||||||||||||||||||||||||||||
| 66 | 52 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 52 | ▶ | return if $self->{buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | 30 | while (1) { | |||||||||||||||||||||||||||
| 69 | 215 | ▶ | return unless $self->{tabledata}->has_next_item; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 213 | ▶ | if ($self->{filter}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 102 | my $row = $self->{tabledata}->get_next_row_arrayref; | |||||||||||||||||||||||||||
| 72 | 102 | ▶ | if ($self->{filter}->($row)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | 15 | $self->{buffer} = $row; | |||||||||||||||||||||||||||
| 74 | 15 | return; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } else { | ||||||||||||||||||||||||||||
| 77 | 111 | my $row = $self->{tabledata}->get_next_item; | |||||||||||||||||||||||||||
| 78 | 111 | my $row_hashref = { map {$self->{column_names}[$_] => $row->[$_]} 0..$#{$row} }; | |||||||||||||||||||||||||||
| 79 | 111 | ▶ | if ($self->{filter_hashref}->($row_hashref)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | 13 | $self->{buffer} = $row; | |||||||||||||||||||||||||||
| 81 | 13 | return; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 24 | ▶ | sub has_next_item { | ||||||||||||||||||||||||||
|
Subroutine
has_next_item: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 88 | 24 | my $self = shift; | |||||||||||||||||||||||||||
| 89 | 24 | ▶ | return 1 if $self->{buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 24 | $self->_fill_buffer; | |||||||||||||||||||||||||||
| 91 | 24 | ▶ | return $self->{buffer} ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 28 | ▶ | sub get_next_item { | ||||||||||||||||||||||||||
|
Subroutine
get_next_item: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 95 | 28 | my $self = shift; | |||||||||||||||||||||||||||
| 96 | 28 | $self->_fill_buffer; | |||||||||||||||||||||||||||
| 97 | 28 | ▶ | die "StopIteration" unless $self->{buffer}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 98 | 28 | $self->{pos}++; | |||||||||||||||||||||||||||
| 99 | 28 | return delete $self->{buffer}; | |||||||||||||||||||||||||||
| 100 | } | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | 4 | ▶ | sub get_next_row_hashref { | ||||||||||||||||||||||||||
|
Subroutine
get_next_row_hashref: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 103 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 104 | 4 | my $row = $self->get_next_item; | |||||||||||||||||||||||||||
| 105 | 4 | +{ map {($self->{column_names}->[$_] => $row->[$_])} 0..$#{$row} }; | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | 0 | ▶ | sub get_iterator_pos { | ||||||||||||||||||||||||||
|
Subroutine
get_iterator_pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 110 | 0 | $self->{pos}; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | 6 | ▶ | sub reset_iterator { | ||||||||||||||||||||||||||
|
Subroutine
reset_iterator: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 114 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 115 | 6 | $self->{tabledata}->reset_iterator; | |||||||||||||||||||||||||||
| 116 | 6 | $self->{pos} = 0; | |||||||||||||||||||||||||||
| 117 | } | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 3 | 1; | |||||||||||||||||||||||||||
| 120 | # ABSTRACT: Role to filter rows from another tabledata | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | __END__ | ||||||||||||||||||||||||||||