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
) ];
profile => { type => HASHREF, optional => 1 },
file => 1,
encoding => 0,
- class => 0,
numberformat => 0,
dateformat => 0,
ignore_unknown_columns => 0,
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;
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;
}
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;
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<profile> \%ACCESSORS
+=item C<profile> {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<profile> and C<class>.
- { listprice => listprice_as_number }
+The C<profile> is a HASHREF which may be used to map header fields to custom
+accessors. Example:
+
+ {profile => { listprice => listprice_as_number }}
In this case C<listprice_as_number> will be used to read in values from the
C<listprice> 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:
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<strict_profile> and C<case_insensitive_header> checking, will be parsed into
-C<get_data>, but will not be attempted to be dispatched into objects.
-
-=item C<class>
-
-If present, the line will be handed to the new sub of this class,
+If C<class> 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<ignore_unknown_columns>
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
Support::TestSetup::login();
my $csv = SL::Helper::Csv->new(
- file => \"Kaffee\n",
- header => [ 'description' ],
- class => 'SL::DB::Part',
+ file => \"Kaffee\n",
+ header => [ 'description' ],
+ profile => { class => 'SL::DB::Part', },
);
isa_ok $csv->_csv, 'Text::CSV_XS';
$::myconfig{dateformat} = 'dd.mm.yyyy';
$csv = SL::Helper::Csv->new(
- file => \"Kaffee;0.12;12,2;1,5234\n",
- header => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ],
- profile => { listprice => 'listprice_as_number' },
- class => 'SL::DB::Part',
+ file => \"Kaffee;0.12;12,2;1,5234\n",
+ header => [ 'description', 'sellprice', 'lastcost_as_number', 'listprice' ],
+ profile => {profile => { listprice => 'listprice_as_number' },
+ class => 'SL::DB::Part',},
);
$csv->parse;
EOL
sep_char => ',',
quote_char => "'",
- profile => { listprice => 'listprice_as_number' },
- class => 'SL::DB::Part',
+ profile => {profile => { listprice => 'listprice_as_number' },
+ class => 'SL::DB::Part',}
);
$csv->parse;
is scalar @{ $csv->get_objects }, 1, 'auto header works';
;;description;sellprice;lastcost_as_number;
#####;Puppy;Kaffee;0.12;12,2;1,5234
EOL
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
$csv->parse;
is scalar @{ $csv->get_objects }, 1, 'bozo header doesn\'t blow things up';
Kaffee;;0.12;12,2;1,5234
Beer;1123245;0.12;12,2;1,5234
EOL
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
$csv->parse;
is scalar @{ $csv->get_objects }, 2, 'multiple objects work';
Beer;1123245;0.12;1.5234
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
$csv->parse;
is $csv->get_objects->[0]->lastcost, '1221.52', 'formatnumber';
Beer;1123245;0.12;1.5234
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
is $csv->parse, undef, 'broken csv header won\'t get parsed';
Beer;1123245;0.12;1.5234
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
is $csv->parse, undef, 'broken csv content won\'t get parsed';
is_deeply $csv->errors, [ '"Kaf"fee";;0.12;1,221.52'."\n", 2023, 'EIQ - QUO character not allowed', 5, 2 ], 'error';
EOL
numberformat => '1,000.00',
ignore_unknown_columns => 1,
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
$csv->parse;
is $csv->get_objects->[0]->lastcost, '1221.52', 'ignore_unkown_columns works';
Beer;1123245;0.12;1.5234;16 %
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
profile => {
- buchungsgruppe => "buchungsgruppen.description",
+ profile => {buchungsgruppe => "buchungsgruppen.description"},
+ class => 'SL::DB::Part',
}
);
$csv->parse;
Beer;1123245;0.12;1.5234;
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
profile => {
- make_1 => "makemodels.0.make",
- model_1 => "makemodels.0.model",
- }
+ profile => {
+ make_1 => "makemodels.0.make",
+ model_1 => "makemodels.0.model",
+ },
+ class => 'SL::DB::Part',
+ },
);
$csv->parse;
my @mm = $csv->get_objects->[0]->makemodel;
Kaffee;;0.12;1,221.52;213;Chair 0815;523;Table 15
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
profile => {
- make_1 => "makemodels.0.make",
- model_1 => "makemodels.0.model",
- make_2 => "makemodels.1.make",
- model_2 => "makemodels.1.model",
+ profile => {
+ make_1 => "makemodels.0.make",
+ model_1 => "makemodels.0.model",
+ make_2 => "makemodels.1.make",
+ model_2 => "makemodels.1.model",
+ },
+ class => 'SL::DB::Part',
}
);
$csv->parse;
description;partnumber;sellprice;lastcost_as_number;buchungsgruppe;
EOL
numberformat => '1,000.00',
- class => 'SL::DB::Part',
profile => {
- buchungsgruppe => "buchungsgruppen.1.description",
+ profile => {buchungsgruppe => "buchungsgruppen.1.description"},
+ class => 'SL::DB::Part',
}
);
is $csv->parse, undef, 'wrong profile gets rejected';
numberformat => '1,000.00',
ignore_unknown_columns => 1,
strict_profile => 1,
- class => 'SL::DB::Part',
profile => {
- lastcost => 'lastcost_as_number',
+ profile => {lastcost => 'lastcost_as_number'},
+ class => 'SL::DB::Part',
}
);
$csv->parse;
EOL
numberformat => '1,000.00',
strict_profile => 1,
- class => 'SL::DB::Part',
profile => {
- lastcost => 'lastcost_as_number',
+ profile => {lastcost => 'lastcost_as_number'},
+ class => 'SL::DB::Part',
}
);
$csv->parse;
$csv = SL::Helper::Csv->new(
file => \"Kaffee",
header => [ 'description' ],
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
);
$csv->parse;
is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'eol bug at the end of files';
$csv = SL::Helper::Csv->new(
file => \"Description\nKaffee",
- class => 'SL::DB::Part',
case_insensitive_header => 1,
- profile => { description => 'description' },
+ profile => {profile => { description => 'description' }, class => 'SL::DB::Part'},
);
$csv->parse;
is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header from csv works';
$csv = SL::Helper::Csv->new(
file => \"Kaffee",
header => [ 'Description' ],
- class => 'SL::DB::Part',
case_insensitive_header => 1,
- profile => { description => 'description' },
+ profile => {profile => { description => 'description' }, class => 'SL::DB::Part'},
);
$csv->parse;
is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header as param works';
$csv = SL::Helper::Csv->new(
file => \"\x{EF}\x{BB}\x{BF}description\nKaffee",
- class => 'SL::DB::Part',
+ profile => {class => 'SL::DB::Part'},
encoding => 'utf8',
);
$csv->parse;
is_deeply $csv->get_data, [ { cvar_Groundhog => 'Phil' } ], 'using empty path to get cvars working';
ok $csv->get_objects->[0], '...and not destorying the objects';
+$csv = SL::Helper::Csv->new(
+ file => \"description\nKaffee",
+);
+$csv->parse;
+is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'without profile and class works';
+
# vim: ft=perl