File Coverage

blib/lib/TableDataRole/Source/AOH.pm
Criterion Covered Total %
statement 56 65 86.1
branch 9 18 50.0
condition n/a
subroutine 11 13 84.6
pod 0 9 0.0
total 76 105 72.3


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