| 1 | package Sweet::File::DSV; | ||
| 2 | use latest; | ||
| 3 | use Moose; | ||
| 4 | |||
| 5 | use Carp; | ||
| 6 | use MooseX::AttributeShortcuts; | ||
| 7 | use Try::Tiny; | ||
| 8 | |||
| 9 | use namespace::autoclean; | ||
| 10 | |||
| 11 | extends 'Sweet::File'; | ||
| 12 | |||
| 13 | sub BUILDARGS { | ||
| 14 | my ($class, %attribute) = @_; | ||
| 15 | |||
| 16 | my $fields_arrayref = $attribute{fields}; | ||
| 17 | my $header = $attribute{header}; | ||
| 18 | my $no_header = $attribute{no_header}; | ||
| 19 | |||
| 20 | if ($no_header and $header) { | ||
| 21 | croak "Argument no_header conflicts with header: $header"; | ||
| 22 | } | ||
| 23 | |||
| 24 | # Needed 'cause init_arg does not work with Array trait. | ||
| 25 | if (defined $fields_arrayref) { | ||
| 26 | $attribute{_fields} = $fields_arrayref; | ||
| 27 | delete $attribute{fields}; | ||
| 28 | } | ||
| 29 | |||
| 30 | return \%attribute; | ||
| 31 | } | ||
| 32 | |||
| 33 | sub BUILD { | ||
| 34 | my $self = shift; | ||
| 35 | |||
| 36 | my (@fields, $header); | ||
| 37 | |||
| 38 | my $separator = $self->separator; | ||
| 39 | |||
| 40 | # If file exists and attribute fields is provided, fill header. | ||
| 41 | if ($self->is_a_plain_file) { | ||
| 42 | try { | ||
| 43 | $header = $self->header; | ||
| 44 | } | ||
| 45 | catch { | ||
| 46 | @fields = $self->fields; | ||
| 47 | $header = join($separator, @fields); | ||
| 48 | }; | ||
| 49 | |||
| 50 | $self->_write_header($header); | ||
| 51 | } | ||
| 52 | else { | ||
| 53 | if ($self->has_fields) { | ||
| 54 | @fields = $self->fields; | ||
| 55 | $header = join($separator, @fields); | ||
| 56 | |||
| 57 | # Check if fields and header does not conflict. | ||
| 58 | if ($self->has_header) { | ||
| 59 | croak "Conflict header and fields" unless $header eq $self->header; | ||
| 60 | } | ||
| 61 | else { | ||
| 62 | $self->_write_header($header); | ||
| 63 | } | ||
| 64 | } | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | has _fields => ( | ||
| 70 | builder => '_build_fields', | ||
| 71 | handles => { | ||
| 72 | field => 'get', | ||
| 73 | fields => 'elements', | ||
| 74 | num_fields => 'count', | ||
| 75 | }, | ||
| 76 | is => 'rw', | ||
| 77 | isa => 'ArrayRef[Str]', | ||
| 78 | lazy => 1, | ||
| 79 | predicate => 'has_fields', | ||
| 80 | traits => ['Array'], | ||
| 81 | ); | ||
| 82 | |||
| 83 | sub _build_fields { | ||
| 84 | my $self = shift; | ||
| 85 | |||
| 86 | my ($header, $separator, @fields); | ||
| 87 | |||
| 88 | try { | ||
| 89 | $header = $self->header; | ||
| 90 | $separator = $self->separator; | ||
| 91 | |||
| 92 | @fields = $self->split_line->($separator)->(0); | ||
| 93 | } | ||
| 94 | catch { | ||
| 95 | croak "Cannot compute file fields", $_; | ||
| 96 | }; | ||
| 97 | |||
| 98 | return \@fields; | ||
| 99 | } | ||
| 100 | |||
| 101 | has no_header => ( | ||
| 102 | default => sub { 0 }, | ||
| 103 | is => 'ro', | ||
| 104 | isa => 'Bool', | ||
| 105 | lazy => 1, | ||
| 106 | ); | ||
| 107 | |||
| 108 | has header => ( | ||
| 109 | builder => '_build_header', | ||
| 110 | is => 'rw', | ||
| 111 | isa => 'Maybe[Str]', | ||
| 112 | lazy => 1, | ||
| 113 | predicate => 'has_header', | ||
| 114 | writer => '_write_header', | ||
| 115 | ); | ||
| 116 | |||
| 117 | sub _build_header { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | return if $self->no_header; | ||
| 121 | |||
| 122 | my $header = $self->line(0); | ||
| 123 | |||
| 124 | return $header; | ||
| 125 | } | ||
| 126 | |||
| 127 | has separator => ( | ||
| 128 | is => 'lazy', | ||
| 129 | isa => 'Str', | ||
| 130 | ); | ||
| 131 | |||
| 132 | has _rows => ( | ||
| 133 | builder => '_build_rows', | ||
| 134 | traits => ['Array'], | ||
| 135 | handles => { | ||
| 136 | num_rows => 'count', | ||
| 137 | row => 'get', | ||
| 138 | rows => 'elements', | ||
| 139 | }, | ||
| 140 | is => 'ro', | ||
| 141 | isa => 'ArrayRef[Str]', | ||
| 142 | lazy => 1, | ||
| 143 | ); | ||
| 144 | |||
| 145 | sub _build_rows { | ||
| 146 | my $self = shift; | ||
| 147 | |||
| 148 | my @rows = $self->lines; | ||
| 149 | |||
| 150 | # Remove header, if any. | ||
| 151 | shift @rows unless $self->no_header; | ||
| 152 | |||
| 153 | return \@rows; | ||
| 154 | } | ||
| 155 | |||
| 156 | sub split_row { | ||
| 157 | my $self = shift; | ||
| 158 | |||
| 159 | my $no_header = $self->no_header; | ||
| 160 | my $separator = $self->separator; | ||
| 161 | |||
| 162 | if ($no_header) { | ||
| 163 | return $self->split_line->($separator); | ||
| 164 | } | ||
| 165 | else { | ||
| 166 | return sub { | ||
| 167 | my $num_line = shift; | ||
| 168 | |||
| 169 | return $self->split_line->($separator)->($num_line + 1); | ||
| 170 | } | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | __PACKAGE__->meta->make_immutable; | ||
| 175 | |||
| 176 | 1; | ||
| 177 | |||
| 178 | =head1 NAME | ||
| 179 | |||
| 180 | Sweet::File::DSV | ||
| 181 | |||
| 182 | =head1 SYNOPSIS | ||
| 183 | |||
| 184 | Given a C<file.dat> in your home dir. | ||
| 185 | |||
| 186 | FIELD_A|FIELD_B | ||
| 187 | foo|bar | ||
| 188 | 2|3 | ||
| 189 | |||
| 190 | Create a pipe separated value file instance. | ||
| 191 | |||
| 192 | my $dir = Sweet::HomeDir->new; | ||
| 193 | my $file = Sweet::File::DSV->new( | ||
| 194 | dir => $dir, | ||
| 195 | name => 'file.dat', | ||
| 196 | sep => '|', | ||
| 197 | ); | ||
| 198 | |||
| 199 | =head1 INHERITANCE | ||
| 200 | |||
| 201 | Inherits from C<Sweet::File>. | ||
| 202 | |||
| 203 | =head1 ATTRIBUTES | ||
| 204 | |||
| 205 | =head2 header | ||
| 206 | |||
| 207 | =head2 no_header | ||
| 208 | |||
| 209 | =head2 separator | ||
| 210 | |||
| 211 | Field separator. Must be provided at creation time or in a sub class with C<_build_sep> method. | ||
| 212 | |||
| 213 | =head1 METHODS | ||
| 214 | |||
| 215 | =head2 num_rows | ||
| 216 | |||
| 217 | say $file->num_rows; # 2 | ||
| 218 | |||
| 219 | =head2 field | ||
| 220 | |||
| 221 | say $file->field(0); # FIELD_A | ||
| 222 | say $file->field(1); # FIELD_B | ||
| 223 | |||
| 224 | =head2 fields | ||
| 225 | |||
| 226 | my @fields = $file->fields; # ('FIELD_A', 'FIELD_B') | ||
| 227 | |||
| 228 | =head1 split_row | ||
| 229 | |||
| 230 | my $cells = $self->split_row->(0); | ||
| 231 | |||
| 232 | say $_ for @$cells; | ||
| 233 | # foo | ||
| 234 | # bar | ||
| 235 | |||
| 236 | =head2 rows | ||
| 237 | |||
| 238 | say $_ for $file->rows; | ||
| 239 | # foo|bar | ||
| 240 | # 2|3 | ||
| 241 | |||
| 242 | =head1 SEE ALSO | ||
| 243 | |||
| 244 | L<Delimiter-separated values|https://en.wikipedia.org/wiki/Delimiter-separated_values> Wikipedia page. | ||
| 245 | |||
| 246 | =cut | ||
| 247 |