Revision c8473408
Von Bernd Blessmann vor fast 11 Jahren hinzugefügt
SL/Helper/Csv.pm | ||
---|---|---|
9 | 9 |
use Params::Validate qw(:all); |
10 | 10 |
use Text::CSV_XS; |
11 | 11 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
12 |
file encoding sep_char quote_char escape_char header profile class
|
|
12 |
file encoding sep_char quote_char escape_char header profile |
|
13 | 13 |
numberformat dateformat ignore_unknown_columns strict_profile _io _csv |
14 | 14 |
_objects _parsed _data _errors all_cvar_configs case_insensitive_header |
15 | 15 |
) ]; |
... | ... | |
29 | 29 |
profile => { type => HASHREF, optional => 1 }, |
30 | 30 |
file => 1, |
31 | 31 |
encoding => 0, |
32 |
class => 0, |
|
33 | 32 |
numberformat => 0, |
34 | 33 |
dateformat => 0, |
35 | 34 |
ignore_unknown_columns => 0, |
... | ... | |
71 | 70 |
|
72 | 71 |
sub get_objects { |
73 | 72 |
my ($self, %params) = @_; |
74 |
croak 'no class given' unless $self->class; |
|
75 | 73 |
croak 'must parse first' unless $self->_parsed; |
76 | 74 |
|
77 | 75 |
$self->_make_objects unless $self->_objects; |
... | ... | |
188 | 186 |
my ($self, %params) = @_; |
189 | 187 |
my @objs; |
190 | 188 |
|
191 |
eval "require " . $self->class; |
|
192 | 189 |
local $::myconfig{numberformat} = $self->numberformat if $self->numberformat; |
193 | 190 |
local $::myconfig{dateformat} = $self->dateformat if $self->dateformat; |
194 | 191 |
|
195 | 192 |
for my $line (@{ $self->_data }) { |
196 |
my $tmp_obj = $self->class->new; |
|
197 |
$self->dispatcher->dispatch($tmp_obj, $line); |
|
193 |
my $tmp_obj = $self->dispatcher->dispatch($line); |
|
198 | 194 |
push @objs, $tmp_obj; |
199 | 195 |
} |
200 | 196 |
|
... | ... | |
248 | 244 |
quote_char => '\'', # default '"' |
249 | 245 |
escape_char => '"', # default '"' |
250 | 246 |
header => [qw(id text sellprice word)], # see later |
251 |
profile => { sellprice => 'sellprice_as_number' }, |
|
252 |
class => 'SL::DB::CsvLine', # if present, map lines to this |
|
247 |
profile => { profile => { sellprice => 'sellprice_as_number'}, class => SL::DB::Part }, |
|
253 | 248 |
); |
254 | 249 |
|
255 | 250 |
my $status = $csv->parse; |
... | ... | |
348 | 343 |
Can be an array of columns, in this case the first line is not used as a |
349 | 344 |
header. Empty header fields will be ignored in objects. |
350 | 345 |
|
351 |
=item C<profile> \%ACCESSORS
|
|
346 |
=item C<profile> {profile => \%ACCESSORS, class => class}
|
|
352 | 347 |
|
353 |
May be used to map header fields to custom accessors. Example:
|
|
348 |
This is a HASHREF to hash which may contain the keys C<profile> and C<class>.
|
|
354 | 349 |
|
355 |
{ listprice => listprice_as_number } |
|
350 |
The C<profile> is a HASHREF which may be used to map header fields to custom |
|
351 |
accessors. Example: |
|
352 |
|
|
353 |
{profile => { listprice => listprice_as_number }} |
|
356 | 354 |
|
357 | 355 |
In this case C<listprice_as_number> will be used to read in values from the |
358 | 356 |
C<listprice> column. |
... | ... | |
360 | 358 |
In case of a One-To-One relationsship these can also be set over |
361 | 359 |
relationsships by sparating the steps with a dot (C<.>). This will work: |
362 | 360 |
|
363 |
{ customer => 'customer.name' }
|
|
361 |
{profile => { customer => 'customer.name' }}
|
|
364 | 362 |
|
365 | 363 |
And will result in something like this: |
366 | 364 |
|
... | ... | |
373 | 371 |
will have to do that for yourself. Since you provided the profile, it is |
374 | 372 |
assumed you know what to do in this case. |
375 | 373 |
|
376 |
If no profile is given, any header field found will be taken as is. |
|
377 |
|
|
378 |
If the path in a profile entry is empty, the field will be subjected to |
|
379 |
C<strict_profile> and C<case_insensitive_header> checking, will be parsed into |
|
380 |
C<get_data>, but will not be attempted to be dispatched into objects. |
|
381 |
|
|
382 |
=item C<class> |
|
383 |
|
|
384 |
If present, the line will be handed to the new sub of this class, |
|
374 |
If C<class> is present, the line will be handed to the new sub of this class, |
|
385 | 375 |
and the return value used instead of the line itself. |
386 | 376 |
|
387 | 377 |
=item C<ignore_unknown_columns> |
... | ... | |
442 | 432 |
Dispatch to child objects, like this: |
443 | 433 |
|
444 | 434 |
$csv = SL::Helper::Csv->new( |
445 |
file => ... |
|
446 |
class => SL::DB::Part, |
|
447 |
profile => [ |
|
448 |
makemodel => { |
|
449 |
make_1 => make, |
|
450 |
model_1 => model, |
|
451 |
}, |
|
452 |
makemodel => { |
|
453 |
make_2 => make, |
|
454 |
model_2 => model, |
|
455 |
}, |
|
456 |
] |
|
435 |
file => ... |
|
436 |
profile => { |
|
437 |
profile => [ |
|
438 |
makemodel => { |
|
439 |
make_1 => make, |
|
440 |
model_1 => model, |
|
441 |
}, |
|
442 |
makemodel => { |
|
443 |
make_2 => make, |
|
444 |
model_2 => model, |
|
445 |
}, |
|
446 |
], |
|
447 |
class => SL::DB::Part, |
|
448 |
} |
|
457 | 449 |
); |
458 | 450 |
|
459 | 451 |
=head1 AUTHOR |
Auch abrufbar als: Unified diff
SL::Helper::Csv bekommt Klasse im Profil mitgeteilt.