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;
39 eval "require $class; 1" or die "could not load class '$class'";
41 if (! $obj->$acc || !$obj->$acc->[$index]) {
42 my @objects = $obj->$acc;
43 $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
45 $obj = $obj->$acc->[$index];
48 $obj->$acc($class->new);
60 my ($self, $col) = @_;
61 return grep { $col eq $_->{key} } $self->_specs;
65 my ($self, %params) = @_;
67 my $header = $self->_csv->header;
68 my $profile = $self->_csv->profile;
71 for my $col (@$header) {
73 push @specs, $self->make_spec($col, $profile->{$col} || $col);
76 $self->_specs(\@specs);
77 $self->_csv->_push_error($self->errors);
78 return ! $self->errors;
82 my ($self, $col, $path) = @_;
84 my $spec = { key => $col, steps => [] };
85 my $cur_class = $self->_csv->class;
87 for my $step_index ( split /\.(?!\d)/, $path ) {
88 my ($step, $index) = split /\./, $step_index;
89 if ($cur_class->can($step)) {
90 if (my $rel = $cur_class->meta->relationship($step)) { #a
91 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
95 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
101 my $next_class = $cur_class->meta->relationship($step)->class;
102 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
103 $cur_class = $next_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);