1 package SL::Helper::Csv::Dispatcher;
7 use Scalar::Util qw(weaken);
8 use Rose::Object::MakeMethods::Generic scalar => [ qw(
12 use SL::Helper::Csv::Error;
15 my ($class, $parent) = @_;
16 my $self = bless { }, $class;
18 weaken($self->{_csv} = $parent);
25 my ($self, $obj, $line) = @_;
27 for my $spec (@{ $self->_specs }) {
28 $self->apply($obj, $spec, $line->{$spec->{key}});
33 my ($self, $obj, $spec, $value) = @_;
36 for my $step (@{ $spec->{steps} }) {
37 my ($acc, $class, $index) = @$step;
42 if (! $obj->$acc || !$obj->$acc->[$index]) {
43 my @objects = $obj->$acc;
44 $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
46 $obj = $obj->$acc->[$index];
49 $obj->$acc($class->new);
61 my ($self, $col) = @_;
62 return grep { $col eq $_->{key} } $self->_specs;
66 my ($self, %params) = @_;
68 my $header = $self->_csv->header;
69 my $profile = $self->_csv->profile;
72 for my $col (@$header) {
74 push @specs, $self->make_spec($col, $profile->{$col} || $col);
77 $self->_specs(\@specs);
78 $self->_csv->_push_error($self->errors);
79 return ! $self->errors;
83 my ($self, $col, $path) = @_;
85 my $spec = { key => $col, steps => [] };
86 my $cur_class = $self->_csv->class;
88 for my $step_index ( split /\.(?!\d)/, $path ) {
89 my ($step, $index) = split /\./, $step_index;
90 if ($cur_class->can($step)) {
91 if (my $rel = $cur_class->meta->relationship($step)) { #a
92 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
96 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
102 my $next_class = $cur_class->meta->relationship($step)->class;
103 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
104 $cur_class = $next_class;
105 eval "require $cur_class; 1" or die "could not load class '$cur_class'";
107 } else { # simple dispatch
108 push @{ $spec->{steps} }, [ $step ];
112 $self->unknown_column($col, $path);
120 my ($self, $col, $path) = @_;
121 return if $self->_csv->ignore_unknown_columns;
126 "header field '$col' is not recognized",
141 my ($self, @errors) = @_;
142 my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
143 $self->_errors(\@new_errors);