r1394@llin: dpavlin | 2007-10-31 01:26:46 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 31 Oct 2007 00:26:45 +0000 (00:26 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Wed, 31 Oct 2007 00:26:45 +0000 (00:26 +0000)
 add new (exported by default) function force_array
 used all over the place

git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@924 07558da8-63fa-0310-ba24-9fe276d99e06

lib/WebPAC/Common.pm
lib/WebPAC/Output.pm
lib/WebPAC/Output/KinoSearch.pm
lib/WebPAC/Output/Sorted.pm
run.pl
t/5-output-kinosearch.t

index e427cd3..5f42ba7 100644 (file)
@@ -1,4 +1,8 @@
 package WebPAC::Common;
+use Exporter 'import';
+@EXPORT = qw/
+       force_array
+/;
 
 use warnings;
 use strict;
@@ -169,6 +173,29 @@ sub var_path {
        return File::Spec->catfile('var', @_);
 }
 
+=head1 EXPORTED NETHODS
+
+=head2 force_array
+
+  my @array = force_array( $ref, sub {
+       warn "reference is undefined!";
+  });
+
+=cut
+
+sub force_array {
+       my ( $what, $error ) = @_;
+       my @result;
+       if ( ref( $what ) eq 'ARRAY' ) {
+               @result = @{ $what };
+       } elsif ( defined $what ) {
+               @result =  ( $what );
+       } else {
+               $error->() if ref($error) eq 'CODE';
+       }
+       return @result;
+}
+
 
 =head1 INTERNAL METHODS
 
index 1ec5d8d..ee35e7d 100644 (file)
@@ -54,6 +54,7 @@ sub ds_to_hash {
        return $hash;
 }
 
+
 =head1 AUTHOR
 
 Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
index e194f4e..8349550 100644 (file)
@@ -139,6 +139,12 @@ sub add {
        $hash->{database} ||= $self->database;
        $hash->{id} ||= $id;
 
+       foreach my $f ( keys %$hash ) {
+               if ( ref($hash->{$f}) eq 'ARRAY' ) {
+                       $hash->{$f} = join(' <*> ', @{ $hash->{$f} });
+               }
+       }
+
        $log->debug("add( $id, ", sub { dump($ds) }," ) => ", sub { dump( $hash ) });
 
        $self->index->add_doc( $hash );
index a5cc924..63d90b2 100644 (file)
@@ -14,6 +14,7 @@ __PACKAGE__->mk_accessors(qw(
 use Sort::External;
 use File::Path;
 use Data::Dump qw/dump/;
+use WebPAC::Common qw/force_array/;
 
 =head1 NAME
 
@@ -84,7 +85,7 @@ sub add {
 
        my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return;
 
-       warn "add( $id, ",dump($ds)," ) => ", dump( $hash );
+       $log->debug("add( $id, ", sub { dump($ds) }," ) => ", sub { dump( $hash ) });
 
        foreach my $f ( keys %$hash ) {
 
@@ -110,14 +111,8 @@ sub add {
 
                my @v;
 
-               if ( ref( $hash->{$f} ) eq 'ARRAY' ) {
-                       @v = @{ $hash->{$f} };
-               } else {
-                       @v =    $hash->{$f}  ;
-               }
-
                # we want LF in output file :-)
-               @v = map { "$_\n" } @v;
+               @v = map { "$_\n" } force_array( $hash->{$f} );
 
                $self->{sortex}->{$f}->feed( @v );
 
diff --git a/run.pl b/run.pl
index 03de8b3..07f8eeb 100755 (executable)
--- a/run.pl
+++ b/run.pl
@@ -299,23 +299,11 @@ foreach my $database ( sort keys %{ $config->databases } ) {
                debug => $debug,
        });
 
-       sub iterate_over {
-               my ( $what, $error ) = @_;
-               my @result;
-               if ( ref( $what ) eq 'ARRAY' ) {
-                       @result = @{ $what };
-               } elsif ($db_config->{input}) {
-                       @result =  ( $what );
-               } else {
-                       $error->() if ref($error) eq 'CODE';
-               }
-               return @result;
-       }
 
        #
        # prepare output
        #
-       my @outputs = iterate_over( $db_config->{output}, sub {
+       my @outputs = force_array( $db_config->{output}, sub {
                $log->error("Database $database doesn't have any outputs defined. Do you want to remove it from configuration?" );
        } );
 
@@ -346,7 +334,7 @@ warn '## output = ',dump( $output );
        #
 
 
-       my @inputs = iterate_over( $db_config->{input}, sub {
+       my @inputs = force_array( $db_config->{input}, sub {
                $log->info("database $database doesn't have inputs defined");
        } );
 
index a613ebc..64e4792 100755 (executable)
@@ -1,6 +1,6 @@
 #!/usr/bin/perl -w
 
-use Test::More tests => 20;
+use Test::More tests => 15;
 use Test::Exception;
 use Cwd qw/abs_path/;
 use KinoSearch;