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