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       if (exists $profile->{$col}) {
 
  82         push @specs, $self->make_spec($col, $profile->{$col});
 
  84         push @specs, $self->make_spec($col, $col);
 
  89   $self->_specs(\@specs);
 
  90   $self->_csv->_push_error($self->errors);
 
  91   return ! $self->errors;
 
  95   my ($self, $col, $path) = @_;
 
  97   my $spec = { key => $col, steps => [] };
 
 101   my $cur_class = $self->_csv->class;
 
 103   return unless $cur_class;
 
 105   for my $step_index ( split /\.(?!\d)/, $path ) {
 
 106     my ($step, $index) = split /\./, $step_index;
 
 107     if ($cur_class->can($step)) {
 
 108       if (my $rel = $cur_class->meta->relationship($step)) { #a
 
 109         if ($index && ! $rel->isa('Rose::DB::Object::Metadata::Relationship::OneToMany')) {
 
 113             "Profile path error. Indexed relationship is not OneToMany around here: '$step_index'",
 
 119           my $next_class = $cur_class->meta->relationship($step)->class;
 
 120           push @{ $spec->{steps} }, [ $step, $next_class, $index ];
 
 121           $cur_class = $next_class;
 
 122           eval "require $cur_class; 1" or die "could not load class '$cur_class'";
 
 124       } else { # simple dispatch
 
 125         push @{ $spec->{steps} }, [ $step ];
 
 129       $self->unknown_column($col, $path);
 
 137   my ($self, $col, $path) = @_;
 
 138   return if $self->_csv->ignore_unknown_columns;
 
 143     "header field '$col' is not recognized",
 
 158   my ($self, @errors) = @_;
 
 159   my @new_errors = ($self->errors, map { SL::Helper::Csv::Error->new(@$_) } @errors);
 
 160   $self->_errors(\@new_errors);