Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision f74b0dac

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

  • ID f74b0dac7a8e55cc7a67c4a03a3ee229415b3fbd
  • Vorgänger 621248de
  • Nachfolger a0d9f09a

Csv: mapping support

Unterschiede anzeigen:

SL/Helper/Csv.pm
204 204
      foreach my $h (@{ $h_aref }) {
205 205
        my @names = (
206 206
          keys %{ $self->profile->[$p_num]->{profile} || {} },
207
          keys %{ $self->profile->[$p_num]->{mapping} || {} },
207 208
        );
208 209
        for my $name (@names) {
209 210
          for my $i (0..$#$h) {
......
527 528

  
528 529
  PROFILE       := [ CLASS_PROFILE, CLASS_PROFILE* ]
529 530
  CLASS_PROFILE := {
530
                      profile   => { ACCESSORS },
531
                      profile   => { ACCESSORS+ },
531 532
                      class     => $classname,
532 533
                      row_ident => $row_ident,
534
                      mapping   => { MAPPINGS* },
533 535
                   }
534
  ACCESSORS     := $field => $accessor, ACCESSORS*
536
  ACCESSORS     := $field => $accessor
537
  MAPPINGS      := $alias => $field
535 538

  
536
The C<profile> is a HASHREF which may be used to map header fields to custom
539
The C<ACCESSORS> may be used to map header fields to custom
537 540
accessors. Example:
538 541

  
539
  [
540
    {
541
      profile => {
542
        listprice => 'listprice_as_number',
543
      }
544
    }
545
  ]
542
  profile => {
543
    listprice => 'listprice_as_number',
544
  }
546 545

  
547 546
In this case C<listprice_as_number> will be used to store the values from the
548 547
C<listprice> column.
......
575 574
C<row_ident> is used to determine the correct profile in multiplexed data and
576 575
must be given there. It's not used in non-multiplexed data.
577 576

  
578
Example:
577
If C<mappings> is present, it must contain a hashref that maps strings to known
578
fields. This can be used to add custom profiles for known sources, that don't
579
comply with the expected header identities.
580

  
581
Without strict profiles, mappings can also directly map header fields that
582
should end up in the same accessor.
583

  
584
Mappings can be identical to known fields and will be prefered during lookup,
585
but will not replace the field, meaning that:
586

  
587
  profile => {
588
    name        => 'name',
589
    description => 'description',
590
  }
591
  mapping => {
592
    name        => 'description',
593
    shortname   => 'name',
594
  }
595

  
596
will work as expected, and shortname will not end up in description. This also
597
works with the case insensitive option. Note however that the case insensitive
598
option will not enable true unicode collating.
599

  
600

  
601
Here's a full example:
602

  
579 603
  [
580 604
    {
581 605
      class     => 'SL::DB::Order',
......
584 608
    {
585 609
      class     => 'SL::DB::OrderItem',
586 610
      row_ident => 'I',
587
      profile   => { sellprice => 'sellprice_as_number' }
611
      profile   => { sellprice => 'sellprice_as_number' },
612
      mapping   => { 'Verkaufspreis' => 'sellprice' }
588 613
    },
589 614
  ]
590 615

  
......
601 626

  
602 627
Note that the last entry can be off, but will give an estimate.
603 628

  
629
Error handling is also known to break on new Perl versions and need to be
630
adjusted from time to time due to changes in Text::CSV_XS.
631

  
604 632
=head1 CAVEATS
605 633

  
606 634
=over 4

Auch abrufbar als: Unified diff