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