File Coverage

blib/lib/TableDataRole/Source/AOH.pm
Criterion Covered Total %
statement 55 61 90.1
branch 9 18 50.0
condition n/a
subroutine 11 13 84.6
pod 0 9 0.0
total 75 101 74.2


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