File Coverage

blib/lib/TableDataRole/Spec/Basic.pm
Criterion Covered Total %
statement 41 41 100.0
branch 1 2 50.0
condition n/a
subroutine 10 10 100.0
pod 9 9 100.0
total 61 62 98.3


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