1 package SL::Helper::Csv::Dispatcher;
7 use Scalar::Util qw(weaken);
8 use List::MoreUtils qw(all pairwise);
9 use Rose::Object::MakeMethods::Generic scalar => [ qw(
10 _specs _row_class _row_spec _errors
13 use SL::Helper::Csv::Error;
16 my ($class, $parent) = @_;
17 my $self = bless { }, $class;
19 weaken($self->{_csv} = $parent);
26 my ($self, $line) = @_;
28 my $class = $self->_class_by_line($line);
29 croak 'no class given' unless $class;
31 eval "require " . $class;
32 my $obj = $class->new;
34 my $specs = $self->_specs_by_line($line);
35 for my $spec (@{ $specs }) {
36 $self->apply($obj, $spec, $line->{$spec->{key}});
43 my ($self, $line) = @_;
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 } });
50 if ($self->_csv->is_multiplexed) {
51 return $self->_row_class->{$line->{datatype}};
53 return $self->_csv->profile->[0]->{class};
58 my ($self, $line) = @_;
60 # initialize lookup hash if not already done
61 if ($self->_csv->is_multiplexed && ! defined $self->_row_spec ) {
62 $self->_row_spec({ pairwise { no warnings 'once'; $a->{row_ident} => $b } @{ $self->_csv->profile }, @{ $self->_specs } });
65 if ($self->_csv->is_multiplexed) {
66 return $self->_row_spec->{$line->{datatype}};
68 return $self->_specs->[0];
74 my ($self, $obj, $spec, $value) = @_;
77 for my $step (@{ $spec->{steps} }) {
78 my ($acc, $class, $index) = @$step;
83 if (! $obj->$acc || !$obj->$acc->[$index]) {
84 my @objects = $obj->$acc;
85 $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
87 $obj = $obj->$acc->[$index];
90 $obj->$acc($class->new);
102 my ($self, $col) = @_;
103 return grep { $col eq $_->{key} } $self->_specs;
107 my ($self, %params) = @_;
111 my $csv_profile = $self->_csv->profile;
112 my $h_aref = ($self->_csv->is_multiplexed)? $self->_csv->header : [ $self->_csv->header ];
114 foreach my $header (@{ $h_aref }) {
115 my $spec = $self->_parse_profile(profile => $csv_profile->[$i]->{profile},
116 class => $csv_profile->[$i]->{class},
122 $self->_specs(\@specs);
124 $self->_csv->_push_error($self->errors);
126 return ! $self->errors;
130 my ($self, %params) = @_;
132 my $profile = $params{profile};
133 my $class = $params{class};
134 my $header = $params{header};
138 for my $col (@$header) {
140 if ($self->_csv->strict_profile) {
141 if (exists $profile->{$col}) {
142 push @specs, $self->make_spec($col, $profile->{$col}, $class);
144 $self->unknown_column($col, undef);
147 if (exists $profile->{$col}) {
148 push @specs, $self->make_spec($col, $profile->{$col}, $class);
150 push @specs, $self->make_spec($col, $col, $class);
159 my ($self, $col, $path, $cur_class) = @_;
161 my $spec = { key => $col, steps => [] };
165 return unless $cur_class;
167 for my $step_index ( split /\.(?!\d)/, $path ) {
168 my ($step, $index) = split /\./, $step_index;
169 if ($cur_class->can($step)) {
170 if (my $rel = $cur_class->meta->relationship($step)) { #a
171 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
175 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
181 my $next_class = $cur_class->meta->relationship($step)->class;
182 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
183 $cur_class = $next_class;
184 eval "require $cur_class; 1" or die "could not load class '$cur_class'";
186 } else { # simple dispatch
187 push @{ $spec->{steps} }, [ $step ];
191 $self->unknown_column($col, $path);
199 my ($self, $col, $path) = @_;
200 return if $self->_csv->ignore_unknown_columns;
205 "header field '$col' is not recognized",
220 my ($self, @errors) = @_;
221 my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
222 $self->_errors(\@new_errors);