| 1 | package Data::Grid::Row; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.014; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings FATAL => 'all'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use Moo; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | ▶ | use overload '@{}' => 'cells'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | # lol this doesn't work with moo | ||||||||||||||||||||||||||||
| 12 | #use overload '%{}' => 'as_hash'; | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 1 | extends 'Data::Grid::Container'; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | =head1 NAME | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | Data::Grid::Row - Row implementation for Data::Grid::Table | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 VERSION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | Version 0.06 | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | =cut | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 1 | our $VERSION = '0.06'; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | # CSV files are themselves only a single table, so the list | ||||||||||||||||||||||||||||
| 31 | # context assignment here takes the first and only one. | ||||||||||||||||||||||||||||
| 32 | my ($table) = Data::Grid->parse('foo.csv')->tables; | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | while (my $row = $table->next) { | ||||||||||||||||||||||||||||
| 35 | my @cells = $row->cells; | ||||||||||||||||||||||||||||
| 36 | # or | ||||||||||||||||||||||||||||
| 37 | @cells = @$row; | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # or, if column names were supplied somehow: | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | my %cells = $row->as_hash; | ||||||||||||||||||||||||||||
| 42 | # or | ||||||||||||||||||||||||||||
| 43 | %cells = %$row; | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | =head1 METHODS | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =head2 table | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | Retrieve the L<Data::Grid::Table> object to which this row | ||||||||||||||||||||||||||||
| 51 | belongs. Alias for L<Data::Grid::Container/parent>. | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =cut | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 0 | ▶ | sub table { | ||||||||||||||||||||||||||
|
Subroutine
table: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 0 | $_[0]->parent; | |||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | =head2 cells [$FLATTEN] | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | Retrieve the cells from the row, as an array in list context or | ||||||||||||||||||||||||||||
| 62 | arrayref in scalar context. The array dereferencing operator C<@{}> is | ||||||||||||||||||||||||||||
| 63 | also overloaded and works like this: | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | my @cells = @$row; | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | =cut | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | sub cells { | ||||||||||||||||||||||||||
|
Subroutine
cells: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 0 | Carp::croak("This method is a stub; it must be overridden!"); | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | =head2 width | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | Returns the width of the row in columns. This is the same as C<scalar | ||||||||||||||||||||||||||||
| 76 | @{$row-E<gt>cells}>. | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | =cut | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | sub width { | ||||||||||||||||||||||||||
|
Subroutine
width: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 81 | # flatten the cells, because no sense in spending any effort | ||||||||||||||||||||||||||||
| 82 | # computing results that get immediately thrown away | ||||||||||||||||||||||||||||
| 83 | 0 | scalar @{$_[0]->cells(1)}; | |||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | =head2 as_hash [$FLATTEN] | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | If the table has a heading or its columns were designated in the | ||||||||||||||||||||||||||||
| 89 | constructor or with L<Data::Grid::Table/columns>, this method will | ||||||||||||||||||||||||||||
| 90 | return the row as key-value pairs in list context and a HASH reference | ||||||||||||||||||||||||||||
| 91 | in scalar context. If there is no column spec, this method will | ||||||||||||||||||||||||||||
| 92 | generate dummy column names starting from 1, like C<col1>, C<col2>, | ||||||||||||||||||||||||||||
| 93 | etc. It will also fill in the blanks if the column spec is shorter | ||||||||||||||||||||||||||||
| 94 | than the actual row. If the column spec is longer, the overhang will | ||||||||||||||||||||||||||||
| 95 | be populated with C<undef>s. As well it is worth noting that duplicate | ||||||||||||||||||||||||||||
| 96 | keys will be clobbered with the rightmost value at this time, though | ||||||||||||||||||||||||||||
| 97 | that behaviour may change. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub as_hash { | ||||||||||||||||||||||||||
|
Subroutine
as_hash: not called CC=6 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||||||||
| 102 | 0 | my ($self, $flatten) = @_; | |||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | my @cells = $self->cells; | |||||||||||||||||||||||||||
| 105 | 0 | my @cols = $self->parent->columns; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | # XXX this should probably be the global width but whatev | ||||||||||||||||||||||||||||
| 108 | 0 | ▶ | @cols = map { "col$_" } (1..scalar @cells) unless @cols; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | my %out; | |||||||||||||||||||||||||||
| 111 | 0 | for my $i (0..$#cols) { | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | $out{$cols[$i]} = ($flatten && ref $cells[$i]) ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$flatten and ref $cells[$i]
MC/DC0%0/2
$flatten and ref $cells[$i]
$flatten
ref $cells[$i]
Truth table0%0/3
A and B
A= $flattenB= ref $cells[$i]
| |||||||||||||||||||||||||||||
| 113 | $cells[$i]->value : $cells[$i]; | ||||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | ▶ | wantarray ? %out : \%out; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | =head2 as_string | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | Returns the row as CSV, quoted where necessary, minus the newline. | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | sub as_string { | ||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 125 | 0 | join ',', map { $_->quoted } $_[0]->cells; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Dorian Taylor, C<< <dorian at cpan.org> >> | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | =over 4 | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | =item | ||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | L<Data::Grid> | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | L<Data::Grid::Container> | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =item | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | L<Data::Grid::Table> | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =item | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | L<Data::Grid::Cell> | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =back | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | Copyright 2010-2018 Dorian Taylor. | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Licensed under the Apache License, Version 2.0 (the "License"); you | ||||||||||||||||||||||||||||
| 159 | may not use this file except in compliance with the License. You may | ||||||||||||||||||||||||||||
| 160 | obtain a copy of the License at | ||||||||||||||||||||||||||||
| 161 | L<http://www.apache.org/licenses/LICENSE-2.0>. | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 164 | distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 165 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or | ||||||||||||||||||||||||||||
| 166 | implied. See the License for the specific language governing | ||||||||||||||||||||||||||||
| 167 | permissions and limitations under the License. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 1 | 1; # End of Data::Grid::Row | |||||||||||||||||||||||||||