| 1 | ## no critic: TestingAndDebugging::RequireUseStrict | ||||||||||
| 2 | package TableDataRole::Spec::Basic; | ||||||||||
| 3 | |||||||||||
| 4 | 3 | ▶ | use Role::Tiny; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 3 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 7 | 3 | our $DATE = '2023-11-25'; # DATE | |||||||||
| 8 | 3 | our $DIST = 'TableData'; # DIST | |||||||||
| 9 | 3 | our $VERSION = '0.2.6'; # VERSION | |||||||||
| 10 | |||||||||||
| 11 | ### constructor | ||||||||||
| 12 | |||||||||||
| 13 | 3 | requires 'new'; | |||||||||
| 14 | |||||||||||
| 15 | ### mixins | ||||||||||
| 16 | |||||||||||
| 17 | 3 | with 'Role::TinyCommons::Iterator::Resettable'; | |||||||||
| 18 | |||||||||||
| 19 | ### additional method names to return hashref | ||||||||||
| 20 | |||||||||||
| 21 | 3 | requires 'get_next_row_hashref'; | |||||||||
| 22 | |||||||||||
| 23 | ### column information | ||||||||||
| 24 | |||||||||||
| 25 | 3 | requires 'get_column_count'; | |||||||||
| 26 | 3 | requires 'get_column_names'; | |||||||||
| 27 | |||||||||||
| 28 | ### aliases, for convenience and clarity | ||||||||||
| 29 | |||||||||||
| 30 | 3 | ▶ | sub has_next_row { | ||||||||
|
Subroutine
has_next_row: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 31 | 3 | my $self = shift; | |||||||||
| 32 | 3 | $self->has_next_item(@_); | |||||||||
| 33 | } | ||||||||||
| 34 | |||||||||||
| 35 | 2 | ▶ | sub get_next_row_arrayref { | ||||||||
|
Subroutine
get_next_row_arrayref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 36 | 2 | my $self = shift; | |||||||||
| 37 | 2 | $self->get_next_item(@_); | |||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | ▶ | sub get_row_count { | ||||||||
|
Subroutine
get_row_count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 41 | 1 | my $self = shift; | |||||||||
| 42 | 1 | $self->get_item_count(@_); | |||||||||
| 43 | } | ||||||||||
| 44 | |||||||||||
| 45 | 1 | ▶ | sub get_all_rows_arrayref { | ||||||||
|
Subroutine
get_all_rows_arrayref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 46 | 1 | my $self = shift; | |||||||||
| 47 | 1 | $self->get_all_items(@_); | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | 1 | ▶ | sub get_all_rows_hashref { | ||||||||
|
Subroutine
get_all_rows_hashref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 51 | 1 | my $self = shift; | |||||||||
| 52 | |||||||||||
| 53 | 1 | my @items; | |||||||||
| 54 | 1 | $self->reset_iterator; | |||||||||
| 55 | 1 | ▶ | while ($self->has_next_item) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | 3 | my $row = $self->get_next_row_hashref; | |||||||||
| 57 | 3 | push @items, $row; | |||||||||
| 58 | } | ||||||||||
| 59 | 1 | @items; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub each_row_arrayref { | ||||||||
|
Subroutine
each_row_arrayref: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 63 | 1 | my $self = shift; | |||||||||
| 64 | 1 | $self->each_item(@_); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 1 | ▶ | sub each_row_hashref { | ||||||||
|
Subroutine
each_row_hashref: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 68 | 1 | my ($self, $coderef) = @_; | |||||||||
| 69 | |||||||||||
| 70 | 1 | $self->reset_iterator; | |||||||||
| 71 | 1 | my $index = 0; | |||||||||
| 72 | 1 | ▶ | while ($self->has_next_item) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 73 | 3 | my $row = $self->get_next_row_hashref; | |||||||||
| 74 | 3 | my $res = $coderef->($row, $self, $index); | |||||||||
| 75 | 3 | ▶ | return 0 unless $res; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 76 | 3 | $index++; | |||||||||
| 77 | } | ||||||||||
| 78 | 1 | return 1; | |||||||||
| 79 | } | ||||||||||
| 80 | |||||||||||
| 81 | 1 | ▶ | sub convert_row_arrayref_to_hashref { | ||||||||
|
Subroutine
convert_row_arrayref_to_hashref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 1 | my ($self, $row_arrayref) = @_; | |||||||||
| 83 | |||||||||||
| 84 | 1 | my $row_hashref = {}; | |||||||||
| 85 | 1 | my @column_names = $self->get_column_names; | |||||||||
| 86 | 1 | for my $i (0 .. $#column_names) { | |||||||||
| 87 | 2 | $row_hashref->{ $column_names[$i] } = $row_arrayref->[$i]; | |||||||||
| 88 | } | ||||||||||
| 89 | 1 | $row_hashref; | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 1 | ▶ | sub convert_row_hashref_to_arrayref { | ||||||||
|
Subroutine
convert_row_hashref_to_arrayref: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 1 | my ($self, $row_hashref) = @_; | |||||||||
| 94 | |||||||||||
| 95 | 1 | my $row_arrayref = []; | |||||||||
| 96 | 1 | my @column_names = $self->get_column_names; | |||||||||
| 97 | 1 | for my $i (0 .. $#column_names) { | |||||||||
| 98 | 2 | $row_arrayref->[$i] = $row_hashref->{ $column_names[$i] }; | |||||||||
| 99 | } | ||||||||||
| 100 | 1 | $row_arrayref; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | 3 | 1; | |||||||||
| 104 | # ABSTRACT: Basic interface for all TableData::* modules | ||||||||||
| 105 | |||||||||||
| 106 | __END__ | ||||||||||