package WebPAC::Common;
+use Exporter 'import';
+@EXPORT = qw/
+ force_array
+/;
use warnings;
use strict;
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
return $hash;
}
+
=head1 AUTHOR
Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
$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 );
use Sort::External;
use File::Path;
use Data::Dump qw/dump/;
+use WebPAC::Common qw/force_array/;
=head1 NAME
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 ) {
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 );
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?" );
} );
#
- 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");
} );
#!/usr/bin/perl -w
-use Test::More tests => 20;
+use Test::More tests => 15;
use Test::Exception;
use Cwd qw/abs_path/;
use KinoSearch;