Revision 7ec8b55e
Von Bernd Blessmann vor fast 11 Jahren hinzugefügt
SL/Helper/Csv/Dispatcher.pm | ||
---|---|---|
5 | 5 |
use Data::Dumper; |
6 | 6 |
use Carp; |
7 | 7 |
use Scalar::Util qw(weaken); |
8 |
use List::MoreUtils qw(all pairwise); |
|
8 | 9 |
use Rose::Object::MakeMethods::Generic scalar => [ qw( |
9 |
_specs _errors |
|
10 |
_specs _row_class _row_spec _errors
|
|
10 | 11 |
) ]; |
11 | 12 |
|
12 | 13 |
use SL::Helper::Csv::Error; |
... | ... | |
38 | 39 |
return $obj; |
39 | 40 |
} |
40 | 41 |
|
41 |
# return class for given line |
|
42 |
# if only one profile is given, return this profiles class |
|
43 |
# if more than one profile is given, identify class by first |
|
44 |
# column??? |
|
45 | 42 |
sub _class_by_line { |
46 | 43 |
my ($self, $line) = @_; |
47 | 44 |
|
48 |
my $class; |
|
45 |
# initialize lookup hash if not already done |
|
46 |
if ($self->_csv->is_multiplexed && ! defined $self->_row_class ) { |
|
47 |
$self->_row_class({ map { $_->{row_ident} => $_->{class} } @{ $self->_csv->profile } }); |
|
48 |
} |
|
49 |
|
|
49 | 50 |
if ($self->_csv->is_multiplexed) { |
50 |
foreach my $p (@{ $self->_csv->profile }) { |
|
51 |
my $row_ident = $p->{row_ident}; |
|
52 |
if ($line->{datatype} eq $row_ident) { |
|
53 |
$class = $p->{class}; |
|
54 |
last; |
|
55 |
} |
|
56 |
} |
|
51 |
return $self->_row_class->{$line->{datatype}}; |
|
57 | 52 |
} else { |
58 |
$class = $self->_csv->profile->[0]->{class};
|
|
53 |
return $self->_csv->profile->[0]->{class};
|
|
59 | 54 |
} |
60 |
|
|
61 |
return $class; |
|
62 | 55 |
} |
63 | 56 |
|
64 | 57 |
sub _specs_by_line { |
65 | 58 |
my ($self, $line) = @_; |
66 | 59 |
|
67 |
my $spec; |
|
68 |
my $i = 0; |
|
60 |
# initialize lookup hash if not already done |
|
61 |
if ($self->_csv->is_multiplexed && ! defined $self->_row_spec ) { |
|
62 |
$self->_row_spec({ pairwise { $a->{row_ident} => $b } @{ $self->_csv->profile }, @{ $self->_specs } }); |
|
63 |
} |
|
64 |
|
|
69 | 65 |
if ($self->_csv->is_multiplexed) { |
70 |
foreach my $p (@{ $self->_csv->profile }) { |
|
71 |
my $row_ident = $p->{row_ident}; |
|
72 |
if ($line->{datatype} eq $row_ident) { |
|
73 |
$spec = $self->_specs->[$i]; |
|
74 |
last; |
|
75 |
} |
|
76 |
$i++; |
|
77 |
} |
|
66 |
return $self->_row_spec->{$line->{datatype}}; |
|
78 | 67 |
} else { |
79 |
$spec = $self->_specs->[0];
|
|
68 |
return $self->_specs->[0];
|
|
80 | 69 |
} |
81 |
|
|
82 |
return $spec; |
|
83 | 70 |
} |
84 | 71 |
|
85 | 72 |
|
Auch abrufbar als: Unified diff
Lookup-Hashes für Header, Klassen und Specs bei Multiplex-Daten benutzen.