X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/f33995ff79013afbf0543453a73dd745354b129a..c8473408202bb3b821a14cee9f8945405d8eeffc:/SL/Helper/Csv.pm diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 3f4eaaaa3..01312fe8a 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -9,7 +9,7 @@ use IO::File; use Params::Validate qw(:all); use Text::CSV_XS; use Rose::Object::MakeMethods::Generic scalar => [ qw( - file encoding sep_char quote_char escape_char header profile class + file encoding sep_char quote_char escape_char header profile numberformat dateformat ignore_unknown_columns strict_profile _io _csv _objects _parsed _data _errors all_cvar_configs case_insensitive_header ) ]; @@ -29,7 +29,6 @@ sub new { profile => { type => HASHREF, optional => 1 }, file => 1, encoding => 0, - class => 0, numberformat => 0, dateformat => 0, ignore_unknown_columns => 0, @@ -71,7 +70,6 @@ sub get_data { sub get_objects { my ($self, %params) = @_; - croak 'no class given' unless $self->class; croak 'must parse first' unless $self->_parsed; $self->_make_objects unless $self->_objects; @@ -188,13 +186,11 @@ sub _make_objects { my ($self, %params) = @_; my @objs; - eval "require " . $self->class; local $::myconfig{numberformat} = $self->numberformat if $self->numberformat; local $::myconfig{dateformat} = $self->dateformat if $self->dateformat; for my $line (@{ $self->_data }) { - my $tmp_obj = $self->class->new; - $self->dispatcher->dispatch($tmp_obj, $line); + my $tmp_obj = $self->dispatcher->dispatch($line); push @objs, $tmp_obj; } @@ -248,8 +244,7 @@ SL::Helper::Csv - take care of csv file uploads quote_char => '\'', # default '"' escape_char => '"', # default '"' header => [qw(id text sellprice word)], # see later - profile => { sellprice => 'sellprice_as_number' }, - class => 'SL::DB::CsvLine', # if present, map lines to this + profile => { profile => { sellprice => 'sellprice_as_number'}, class => SL::DB::Part }, ); my $status = $csv->parse; @@ -348,11 +343,14 @@ Same as in L Can be an array of columns, in this case the first line is not used as a header. Empty header fields will be ignored in objects. -=item C \%ACCESSORS +=item C {profile => \%ACCESSORS, class => class} -May be used to map header fields to custom accessors. Example: +This is a HASHREF to hash which may contain the keys C and C. - { listprice => listprice_as_number } +The C is a HASHREF which may be used to map header fields to custom +accessors. Example: + + {profile => { listprice => listprice_as_number }} In this case C will be used to read in values from the C column. @@ -360,7 +358,7 @@ C column. In case of a One-To-One relationsship these can also be set over relationsships by sparating the steps with a dot (C<.>). This will work: - { customer => 'customer.name' } + {profile => { customer => 'customer.name' }} And will result in something like this: @@ -373,15 +371,7 @@ these information are unique, and should be connected to preexisting data, you will have to do that for yourself. Since you provided the profile, it is assumed you know what to do in this case. -If no profile is given, any header field found will be taken as is. - -If the path in a profile entry is empty, the field will be subjected to -C and C checking, will be parsed into -C, but will not be attempted to be dispatched into objects. - -=item C - -If present, the line will be handed to the new sub of this class, +If C is present, the line will be handed to the new sub of this class, and the return value used instead of the line itself. =item C @@ -442,18 +432,20 @@ Encoding errors are not dealt with properly. Dispatch to child objects, like this: $csv = SL::Helper::Csv->new( - file => ... - class => SL::DB::Part, - profile => [ - makemodel => { - make_1 => make, - model_1 => model, - }, - makemodel => { - make_2 => make, - model_2 => model, - }, - ] + file => ... + profile => { + profile => [ + makemodel => { + make_1 => make, + model_1 => model, + }, + makemodel => { + make_2 => make, + model_2 => model, + }, + ], + class => SL::DB::Part, + } ); =head1 AUTHOR