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 if ($self->_csv->strict_profile) {
75 if (exists $profile->{$col}) {
76 push @specs, $self->make_spec($col, $profile->{$col});
78 $self->unknown_column($col, undef);
81 push @specs, $self->make_spec($col, $profile->{$col} || $col);
85 $self->_specs(\@specs);
86 $self->_csv->_push_error($self->errors);
87 return ! $self->errors;
91 my ($self, $col, $path) = @_;
93 my $spec = { key => $col, steps => [] };
94 my $cur_class = $self->_csv->class;
96 return unless $cur_class;
98 for my $step_index ( split /\.(?!\d)/, $path ) {
99 my ($step, $index) = split /\./, $step_index;
100 if ($cur_class->can($step)) {
101 if (my $rel = $cur_class->meta->relationship($step)) { #a
102 if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
106 "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
112 my $next_class = $cur_class->meta->relationship($step)->class;
113 push @{ $spec->{steps} }, [ $step, $next_class, $index ];
114 $cur_class = $next_class;
115 eval "require $cur_class; 1" or die "could not load class '$cur_class'";
117 } else { # simple dispatch
118 push @{ $spec->{steps} }, [ $step ];
122 $self->unknown_column($col, $path);
130 my ($self, $col, $path) = @_;
131 return if $self->_csv->ignore_unknown_columns;
136 "header field '$col' is not recognized",
151 my ($self, @errors) = @_;
152 my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
153 $self->_errors(\@new_errors);