Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 90af0ce7

Von Sven Schöling vor mehr als 13 Jahren hinzugefügt

  • ID 90af0ce7f2f0a8b4155d65a89e612bea1631db38
  • Vorgänger 8a635325
  • Nachfolger efb48636

Multiple Dispatch - one-to-many.

Unterschiede anzeigen:

SL/Helper/Csv/Dispatcher.pm
32 32
  return unless $value;
33 33

  
34 34
  for my $step (@{ $spec->{steps} }) {
35
    my ($acc, $class) = @$step;
35
    my ($acc, $class, $index) = @$step;
36 36
    if ($class) {
37

  
38
      # autovifify
37 39
      eval "require $class; 1" or die "could not load class '$class'";
38
      $obj->$acc($class->new) if ! $obj->$acc;
39
      $obj = $obj->$acc;
40
      if (defined $index) {
41
        if (! $obj->$acc || !$obj->$acc->[$index]) {
42
          my @objects = $obj->$acc;
43
          $obj->$acc(@objects, map { $class->new } 0 .. $index - @objects);
44
        }
45
        $obj = $obj->$acc->[$index];
46
      } else {
47
        if (! $obj->$acc) {
48
          $obj->$acc($class->new);
49
        }
50
        $obj = $obj->$acc;
51
      }
52

  
40 53
    } else {
41 54
      $obj->$acc($value);
42 55
    }
......
71 84
  my $spec = { key => $col, steps => [] };
72 85
  my $cur_class = $self->_csv->class;
73 86

  
74
  for my $step ( split /\./, $path ) {
87
  for my $step_index ( split /\.(?!\d)/, $path ) {
88
    my ($step, $index) = split /\./, $step_index;
75 89
    if ($cur_class->can($step)) {
76 90
      if ($cur_class->meta->relationship($step)) { #a
77 91
        my $next_class = $cur_class->meta->relationship($step)->class;
78
        push @{ $spec->{steps} }, [ $step, $next_class ];
92
        push @{ $spec->{steps} }, [ $step, $next_class, $index ];
79 93
        $cur_class = $next_class;
80 94
      } else { # simple dispatch
81 95
        push @{ $spec->{steps} }, [ $step ];
t/helper/csv.t
1
use Test::More;
1
use Test::More tests => 29;
2 2
use SL::Dispatcher;
3
use Data::Dumper;
3 4
use utf8;
4 5

  
5 6
use_ok 'SL::Helper::Csv';
......
159 160
is $csv->get_objects->[0]->buchungsgruppe->description, 'Standard 7%', '...and gets set correctly';
160 161

  
161 162

  
162
done_testing();
163
#####
164

  
165
$csv = SL::Helper::Csv->new(
166
  file   => \<<EOL,
167
description;partnumber;sellprice;lastcost_as_number;make_1;model_1;
168
  Kaffee;;0.12;1,221.52;213;Chair 0815
169
Beer;1123245;0.12;1.5234;
170
EOL
171
  numberformat => '1,000.00',
172
  class  => 'SL::DB::Part',
173
  profile => {
174
    make_1 => "makemodels.0.make",
175
    model_1 => "makemodels.0.model",
176
  }
177
);
178
$csv->parse;
179
my @mm = $csv->get_objects->[0]->makemodel;
180
is scalar @mm,  1, 'one-to-many dispatch';
181
is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '... and works';
182

  
183
#####
184

  
185

  
186
$csv = SL::Helper::Csv->new(
187
  file   => \<<EOL,
188
description;partnumber;sellprice;lastcost_as_number;make_1;model_1;make_2;model_2;
189
 Kaffee;;0.12;1,221.52;213;Chair 0815;523;Table 15
190
EOL
191
  numberformat => '1,000.00',
192
  class  => 'SL::DB::Part',
193
  profile => {
194
    make_1 => "makemodels.0.make",
195
    model_1 => "makemodels.0.model",
196
    make_2 => "makemodels.1.make",
197
    model_2 => "makemodels.1.model",
198
  }
199
);
200
$csv->parse;
201

  
202
print Dumper($csv->errors);
203

  
204
my @mm = $csv->get_objects->[0]->makemodel;
205
is scalar @mm,  1, 'multiple one-to-many dispatch';
206
is $csv->get_objects->[0]->makemodels->[0]->model, 'Chair 0815', '...check 1';
207
is $csv->get_objects->[0]->makemodels->[0]->make, '213', '...check 2';
208
is $csv->get_objects->[0]->makemodels->[1]->model, 'Table 15', '...check 3';
209
is $csv->get_objects->[0]->makemodels->[1]->make, '523', '...check 4';
210

  
163 211
# vim: ft=perl

Auch abrufbar als: Unified diff