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) = @$step;
37 eval "require $class; 1" or die "could not load class '$class'";
38 $obj->$acc($class->new) if ! $$obj->$acc;
47 my ($self, $col) = @_;
48 return grep { $col eq $_->{key} } $self->_specs;
52 my ($self, %params) = @_;
54 my $header = $self->_csv->header;
55 my $profile = $self->_csv->profile;
58 for my $col (@$header) {
60 push @specs, $self->make_spec($col, $profile->{$col} || $col);
63 $self->_specs(\@specs);
64 $self->_csv->_push_error($self->errors);
65 return ! $self->errors;
69 my ($self, $col, $path) = @_;
71 my $spec = { key => $col, steps => [] };
72 my $cur_class = $self->_csv->class;
74 for my $step ( split /\./, $path ) {
75 if ($cur_class->can($step)) {
76 if ($cur_class->meta->relationship($step)) { #a
77 my $next_class = $cur_class->meta->relationsship($step)->class;
78 push @{ $spec->{steps} }, [ $step, $next_class ];
79 $cur_class = $next_class;
80 } else { # simple dispatch
81 push @{ $spec->{steps} }, [ $step ];
85 $self->unknown_column($col, $path);
93 my ($self, $col, $path) = @_;
94 return if $self->_csv->ignore_unknown_columns;
99 "header field '$col' is not recognized",
114 my ($self, @errors) = @_;
115 my @new_errors = ($self->errors, @errors);
116 $self->_errors(\@new_errors);