File Coverage

blib/lib/TableDataRole/Source/AOA.pm
Criterion Covered Total %
statement 46 59 77.9
branch 6 14 42.8
condition 3 18 16.6
subroutine 13 16 81.2
pod 0 12 0.0
total 68 119 57.1


line stmt bran cond sub pod time code
1             package TableDataRole::Source::AOA;
2              
3 10     10   353660 use 5.010001;
  10         63  
4 10     10   52 use strict;
  10         26  
  10         250  
5 10     10   46 use warnings;
  10         16  
  10         521  
6              
7 10     10   535 use Role::Tiny;
  10         5264  
  10         58  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2024-05-14'; # DATE
11             our $DIST = 'TableDataRoles-Standard'; # DIST
12             our $VERSION = '0.025'; # VERSION
13              
14             with 'TableDataRole::Spec::Basic';
15             with 'TableDataRole::Spec::GetRowByPos';
16              
17             sub new {
18 2     2 0 630803 my ($class, %args) = @_;
19              
20 2 50       32 my $column_names = delete $args{column_names} or die "Please specify 'column_names' argument";
21 2 50       11 my $aoa = delete $args{aoa} or die "Please specify 'aoa' argument";
22 2 50       11 die "Unknown argument(s): ". join(", ", sort keys %args)
23             if keys %args;
24              
25             bless {
26             aoa => $aoa,
27             pos => 0,
28             column_names => $column_names,
29 2         6 column_idxs => {map {$column_names->[$_] => $_} 0..$#{$column_names}},
  5         55  
  2         9  
30             }, $class;
31             }
32              
33             sub get_column_count {
34 3     3 0 34 my $self = shift;
35 3         8 scalar @{ $self->{column_names} };
  3         39  
36             }
37              
38             sub get_column_names {
39 3     3 0 9 my $self = shift;
40 3 50       15 wantarray ? @{ $self->{column_names} } : $self->{column_names};
  3         44  
41             }
42              
43             sub has_next_item {
44 57     57 0 111 my $self = shift;
45 57         49 $self->{pos} < @{$self->{aoa}};
  57         74  
46             }
47              
48             sub get_next_item {
49 62     62 0 126 my $self = shift;
50 62         70 my $aoa = $self->{aoa};
51 62 50       64 die "StopIteration" unless $self->{pos} < @{$aoa};
  62         103  
52 62         88 $aoa->[ $self->{pos}++ ];
53             }
54              
55             sub get_next_row_hashref {
56 4     4 0 8 my $self = shift;
57 4         12 my $row_aryref = $self->get_next_item;
58 4         9 +{ map { $self->{column_names}[$_] => $row_aryref->[$_] } 0..$#{$self->{column_names}} };
  10         64  
  4         16  
59             }
60              
61             sub get_row_count {
62 2     2 0 6 my $self = shift;
63 2         6 scalar(@{ $self->{aoa} });
  2         11  
64             }
65              
66             sub reset_iterator {
67 6     6 0 25 my $self = shift;
68 6         19 $self->{pos} = 0;
69             }
70              
71             sub get_iterator_pos {
72 0     0 0 0 my $self = shift;
73 0         0 $self->{pos};
74             }
75              
76             sub get_item_at_pos {
77 3     3 0 7 my ($self, $index) = @_;
78              
79             die "OutOfBounds" if
80 0         0 $index < 0 && -$index > @{ $self->{aoa} } ||
81 3 50 33     16 $index >= 0 && $index >= @{ $self->{aoa} };
  3   33     11  
      33        
82 3         16 $self->{aoa}->[$index];
83             }
84              
85             sub get_row_at_pos_hashref {
86 0     0 0   my ($self, $index) = @_;
87 0           my $row_aryref = $self->get_item_at_pos($index);
88 0           +{ map { $self->{column_names}[$_] => $row_aryref->[$_] } 0..$#{$self->{column_names}} };
  0            
  0            
89             }
90              
91             sub has_item_at_pos {
92 0     0 0   my ($self, $index) = @_;
93              
94             return 0 if
95 0           $index < 0 && -$index > @{ $self->{aoa} } ||
96 0 0 0       $index >= 0 && $index >= @{ $self->{aoa} };
  0   0        
      0        
97 0           1;
98             }
99              
100             1;
101             # ABSTRACT: Get table data from an array of arrays
102              
103             __END__