1 package SL::Helper::Csv::Dispatcher;
7 use Scalar::Util qw(weaken);
8 use Rose::Object::MakeMethods::Generic scalar => [ qw(
13 my ($class, $parent) = @_;
14 my $self = bless { }, $class;
16 weaken($self->{_csv} = $parent);
23 my ($self, $obj, $line) = @_;
25 for my $spec (@{ $self->_specs }) {
26 $self->apply($obj, $spec, $line->{$spec->{key}});
31 my ($self, $obj, $spec, $value) = @_;
34 for my $step (@{ $spec->{steps} }) {
35 my ($acc, $class, $index) = @$step;
40 if (! $obj->$acc || !$obj->$acc->[$index]) {
41 my @objects = $obj->$acc;
42 $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
44 $obj = $obj->$acc->[$index];
47 $obj->$acc($class->new);
59 my ($self, $col) = @_;
60 return grep { $col eq $_->{key} } $self->_specs;
64 my ($self, %params) = @_;
66 my $header = $self->_csv->header;
67 my $profile = $self->_csv->profile;
70 for my $col (@$header) {
72 push @specs, $self->make_spec($col, $profile->{$col} || $col);
75 $self->_specs(\@specs);
76 $self->_csv->_push_error($self->errors);
77 return ! $self->errors;
81 my ($self, $col, $path) = @_;
83 my $spec = { key => $col, steps => [] };
84 my $cur_class = $self->_csv->class;
86 for my $step_index ( split /\.(?!\d)/, $path ) {
87 my ($step, $index) = split /\./, $step_index;
88 if ($cur_class->can($step)) {
89 if (my $rel = $cur_class->meta->relationship($step)) { #a
90 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
94 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
100 my $next_class = $cur_class->meta->relationship($step)->class;
101 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
102 $cur_class = $next_class;
103 eval "require $cur_class; 1" or die "could not load class '$cur_class'";
105 } else { # simple dispatch
106 push @{ $spec->{steps} }, [ $step ];
110 $self->unknown_column($col, $path);
118 my ($self, $col, $path) = @_;
119 return if $self->_csv->ignore_unknown_columns;
124 "header field '$col' is not recognized",
139 my ($self, @errors) = @_;
140 my @new_errors = ($self->errors, @errors);
141 $self->_errors(\@new_errors);