Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 17d58914

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

  • ID 17d58914b5dbaf021dcea3375ee943bb6e798916
  • Vorgänger 1dcc096b
  • Nachfolger 417cc3a7

Csv Dispatcher implementiert.

Unterschiede anzeigen:

SL/Helper/Csv.pm
5 5

  
6 6
use Carp;
7 7
use IO::File;
8
use Text::CSV;
9 8
use Params::Validate qw(:all);
9
use Text::CSV;
10 10
use Rose::Object::MakeMethods::Generic scalar => [ qw(
11 11
  file encoding sep_char quote_char escape_char header profile class
12 12
  numberformat dateformat ignore_unknown_columns _io _csv _objects _parsed
13 13
  _data _errors
14 14
) ];
15 15

  
16
use SL::Helper::Csv::Dispatcher;
16 17

  
17 18
# public interface
18 19

  
......
53 54

  
54 55
  $self->_open_file;
55 56
  return if ! $self->_check_header;
56
  return if $self->class && ! $self->_check_header_for_class;
57
  return if ! $self->dispatcher->parse_profile;
58
#  return if $self->class && ! $self->_check_header_for_class;
57 59
  return if ! $self->_parse_data;
58 60

  
59 61
  $self->_parsed(1);
......
109 111
  $self->header($header);
110 112
}
111 113

  
112
sub _check_header_for_class {
113
  my ($self, %params) = @_;
114
  my @errors;
115

  
116
  carp 'this should never be called without' unless $self->class;
117

  
118
  if ($self->ignore_unknown_columns) {
119
    my @new_header;
120
    for my $method (@{ $self->header }) {
121
      push @new_header, $self->class->can($self->_real_method($method))
122
         ? $method : undef;
123
    }
124

  
125
    $self->header(\@new_header);
126

  
127
    return 1;
128
  } else {
129
    for my $method (@{ $self->header }) {
130
      next if ! $method;
131
      next if $self->class->can($self->_real_method($method));
132

  
133
      push @errors, [
134
        $method,
135
        undef,
136
        "header field $method is not recognized",
137
        undef,
138
        0,
139
      ];
140
    }
141

  
142
    $self->_push_error(@errors);
143
    return ! @errors;
144
  }
145
}
146

  
147 114
sub _parse_data {
148 115
  my ($self, %params) = @_;
149 116
  my (@data, @errors);
......
153 120
  while (1) {
154 121
    my $row = $self->_csv->getline($self->_io);
155 122
    last if $self->_csv->eof;
156

  
157 123
    if ($row) {
158 124
      my %hr;
159 125
      @hr{@{ $self->header }} = @$row;
......
186 152
  local $::myconfig{dateformat}   = $self->dateformat   if $self->dateformat;
187 153

  
188 154
  for my $line (@{ $self->_data }) {
189
    push @objs, $self->class->new(
190
      map {
191
        $self->_real_method($_) => $line->{$_}
192
      } grep { $_ } keys %$line
193
    );
155
    my $tmp_obj = $self->class->new;
156
    $self->dispatcher->dispatch($tmp_obj, $line);
157
    push @objs, $tmp_obj;
194 158
  }
195 159

  
196 160
  $self->_objects(\@objs);
197 161
}
198 162

  
199
sub _real_method {
200
  my ($self, $arg) = @_;
201
  ($self->profile && $self->profile->{$arg}) || $arg;
163
sub dispatcher {
164
  my ($self, %params) = @_;
165

  
166
  $self->{_dispatcher} ||= $self->_make_dispatcher;
167
}
168

  
169
sub _make_dispatcher {
170
  my ($self, %params) = @_;
171

  
172
  die 'need a header to make a dispatcher' unless $self->header;
173

  
174
  return SL::Helper::Csv::Dispatcher->new($self);
202 175
}
203 176

  
204 177
sub _guess_encoding {

Auch abrufbar als: Unified diff