Revision 62add698
Von Sven Schöling vor mehr als 13 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
8 | 8 |
use Text::CSV; |
9 | 9 |
use Params::Validate qw(:all); |
10 | 10 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
11 |
file encoding sep_char quote_char escape_char header dispatch class
|
|
11 |
file encoding sep_char quote_char escape_char header profile class
|
|
12 | 12 |
numberformat dateformat _io _csv _objects _parsed _data _errors |
13 | 13 |
) ]; |
14 | 14 |
|
... | ... | |
22 | 22 |
quote_char => { default => '"' }, |
23 | 23 |
escape_char => { default => '"' }, |
24 | 24 |
header => { type => ARRAYREF, optional => 1 }, |
25 |
dispatch => { type => HASHREF, optional => 1 },
|
|
25 |
profile => { type => HASHREF, optional => 1 },
|
|
26 | 26 |
file => 1, |
27 | 27 |
encoding => 0, |
28 | 28 |
class => 0, |
... | ... | |
97 | 97 |
|
98 | 98 |
my $header = $self->_csv->getline($self->_io); |
99 | 99 |
|
100 |
$self->_push_error([ |
|
101 |
$self->_csv->error_input, |
|
102 |
$self->_csv->error_diag, |
|
103 |
0, |
|
104 |
]) unless $header; |
|
105 |
|
|
100 | 106 |
$self->header($header); |
101 | 107 |
} |
102 | 108 |
|
... | ... | |
178 | 184 |
|
179 | 185 |
sub _real_method { |
180 | 186 |
my ($self, $arg) = @_; |
181 |
($self->dispatch && $self->dispatch->{$arg}) || $arg;
|
|
187 |
($self->profile && $self->profile->{$arg}) || $arg;
|
|
182 | 188 |
} |
183 | 189 |
|
184 | 190 |
sub _guess_encoding { |
... | ... | |
213 | 219 |
sep_char => ',', # default ';' |
214 | 220 |
quote_char => ''', # default '"' |
215 | 221 |
header => [qw(id text sellprice word)] # see later |
216 |
dispatch => { sellprice => 'sellprice_as_number' }
|
|
222 |
profile => { sellprice => 'sellprice_as_number' }
|
|
217 | 223 |
class => 'SL::DB::CsvLine', # if present, map lines to this |
218 | 224 |
) |
219 | 225 |
|
... | ... | |
288 | 294 |
Can be an array of columns, in this case the first line is not used as a |
289 | 295 |
header. Empty header fields will be ignored in objects. |
290 | 296 |
|
291 |
=item C<dispatch> \%ACCESSORS
|
|
297 |
=item C<profile> \%ACCESSORS
|
|
292 | 298 |
|
293 | 299 |
May be used to map header fields to custom accessors. Example: |
294 | 300 |
|
... | ... | |
342 | 348 |
$csv = SL::Helper::Csv->new( |
343 | 349 |
file => ... |
344 | 350 |
class => SL::DB::Part, |
345 |
dispatch => [
|
|
351 |
profile => [
|
|
346 | 352 |
makemodel => { |
347 | 353 |
make_1 => make, |
348 | 354 |
model_1 => model, |
Auch abrufbar als: Unified diff
dispatch -> profile. ausserdem mehr errorchecking.