new syntax for row $update in test
[MojoFacets.git] / lib / MojoFacets / Data.pm
index 17cd670..81a5b9b 100644 (file)
@@ -16,6 +16,7 @@ use File::Path qw(mkpath);
 
 use MojoFacets::Import::File;
 use MojoFacets::Import::HTMLTable;
+use MojoFacets::Import::CSV;
 
 our $loaded;
 our $filters;
@@ -38,6 +39,9 @@ sub index {
                } elsif ( -d $file && $file =~ m/\.html$/ ) {
                        $file =~ s/$data_dir\/*//;
                        push @files, $file;
+               } elsif ( -f $file && $file =~ m/\.csv$/i ) {
+                       $file =~ s/$data_dir\/*//;
+                       push @files, $file;
                } else {
                        #warn "IGNORE: $file\n";
                }
@@ -178,7 +182,11 @@ sub _load_path {
 
        my $data;
        if ( -f $full_path ) {
-               $data = MojoFacets::Import::File->new( full_path => $full_path, path => $path )->data;
+               if ( $full_path =~ m/.csv/i ) {
+                       $data = MojoFacets::Import::CSV->new( full_path => $full_path )->data;
+               } else {
+                       $data = MojoFacets::Import::File->new( full_path => $full_path, path => $path )->data;
+               }
        } elsif ( -d $full_path && $full_path =~ m/.html/ ) {
                $data = MojoFacets::Import::HTMLTable->new( dir => $full_path )->data;
        } else {
@@ -702,17 +710,18 @@ sub items {
                $i = $from_end - $i if $from_end;
                my $id = $filtered->[$i];
                my $row = Storable::dclone $data->{items}->[ $id ];
-               my $old = { map { $_ => 1 } keys %$row };
                if ( $code && $test ) {
+                       my $update;
                        eval $code;
                        if ( $@ ) {
                                warn "ERROR evaling\n$code\n$@";
                                $self->stash('eval_error', $@) if $@;
                        } else {
-                               warn "EVAL ",dump($row);
-                               $old->{$_}-- foreach keys %$row;
-                               warn "columns changed ",dump($old);
-                               $cols_changed->{$_}++ foreach grep { $old->{$_} == -1 } keys %$old;
+                               warn "EVAL ",dump($update);
+                               foreach ( keys %$update ) {
+                                       $cols_changed->{$_}++;
+                                       $row->{$_} = $update->{$_};
+                               }
                        }
                }
                $row->{_row_id} ||= $id;