File Coverage

blib/lib/TableDataRole/Munge/GroupRows.pm
Criterion Covered Total %
statement 38 38 100.0
branch 8 14 57.1
condition 3 6 50.0
subroutine 7 7 100.0
pod 1 1 100.0
total 57 66 86.3


line stmt bran cond sub pod time code
1             package TableDataRole::Munge::GroupRows;
2              
3 3     3   463104 use 5.010001;
  3         11  
4 3     3   19 use strict;
  3         9  
  3         78  
5 3     3   19 use warnings;
  3         6  
  3         153  
6 3     3   5344 use Log::ger;
  3         161  
  3         18  
7              
8 3     3   1123 use Role::Tiny;
  3         4722  
  3         18  
9              
10             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11             our $DATE = '2024-05-14'; # DATE
12             our $DIST = 'TableDataRoles-Standard'; # DIST
13             our $VERSION = '0.025'; # VERSION
14              
15             with 'TableDataRole::Spec::Basic';
16             with 'TableDataRole::Source::AOA';
17              
18             sub new {
19 1     1 1 498949 require Module::Load::Util;
20              
21 1         2999 my ($class, %args) = @_;
22              
23             my $tabledata = delete $args{tabledata}
24 1 50       7 or die "Please supply 'tabledata' argument";
25 1   50     5 my $key = delete($args{key}) // 'key';
26 1 50       5 length($key) > 1 or die "Argument 'key' cannot be empty";
27 1 50       4 $key ne 'rows' or die "Argument 'key' cannot have the value of 'rows'";
28             my $calc_key = delete $args{calc_key}
29 1 50       5 or die "Please supply 'calc_key' argument";
30 1   50     8 my $load = delete($args{load}) // 1;
31 1 50       4 die "Unknown argument(s): ". join(", ", sort keys %args)
32             if keys %args;
33              
34 1         6 my $td = Module::Load::Util::instantiate_class_with_optional_args(
35             {load=>$load, ns_prefix=>"TableData"}, $tabledata);
36              
37             # group the rows now into aoa
38 1         18 my $aoa = [];
39             {
40 1         2 my %rownum; # key=calculated key, val=rownum
  1         3  
41             $td->each_row_arrayref(
42             sub {
43 99     99   395 my $row_arrayref = shift;
44 99         264 my $row_hashref = $td->convert_row_arrayref_to_hashref($row_arrayref);
45 99         1601 my $key = $calc_key->($row_hashref, $aoa);
46 99 50       1172 defined $key or die "BUG: calc_key produced undef key!";
47 99 100       289 unless (defined $rownum{$key}) {
48 49         155 $rownum{$key} = @$aoa;
49 49   50     288 $aoa->[ $rownum{$key} ] //= [$key, []];
50             }
51 99         161 push @{ $aoa->[ $rownum{$key} ][1] }, $row_arrayref;
  99         455  
52 1         14 });
53             }
54              
55             bless {
56 1         46 tabledata => $tabledata,
57             _tabledata => $td,
58             column_names => [$key, 'rows'],
59             column_idxs => {$key=>0, rows=>1},
60             aoa => $aoa,
61             pos => 0,
62             }, $class;
63             }
64              
65             1;
66             # ABSTRACT: Role to group rows from another tabledata
67              
68             __END__