From cefcf9a059c6fce852263ee58ac3ed932a2853a2 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sun, 4 Nov 2007 14:43:47 +0000 Subject: [PATCH] r1525@llin: dpavlin | 2007-11-04 15:34:13 +0100 added consume_outputs which returns all output types this plugin supports. This will eventually come in handy for main loop to decide which outputs to call... git-svn-id: svn+ssh://mjesec/home/dpavlin/svn/webpac2/trunk@993 07558da8-63fa-0310-ba24-9fe276d99e06 --- lib/WebPAC/Output/Webpacus.pm | 70 ++++++++++++++++++++++++++--------- t/5-output-webpacus.t | 8 +++- 2 files changed, 59 insertions(+), 19 deletions(-) diff --git a/lib/WebPAC/Output/Webpacus.pm b/lib/WebPAC/Output/Webpacus.pm index bdf5b1b..1a2bffe 100644 --- a/lib/WebPAC/Output/Webpacus.pm +++ b/lib/WebPAC/Output/Webpacus.pm @@ -23,13 +23,9 @@ use Jifty; WebPAC::Output::Webpacus - integrate WebPAC front-end with Jifty back-end -=head1 VERSION - -Version 0.01 - =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -92,12 +88,22 @@ sub add { $log->debug("id: $id ds = ",sub { dump($ds) }); - my $hash = $self->ds_to_hash( $ds, 'sorted' ) || return; + my $stat; + + foreach my $type ( $self->consume_outputs ) { + + my $hash = $self->ds_to_hash( $ds, $type ) || next; + + $log->debug("$type has following data: ", sub { dump( $hash ) }); - foreach my $f ( keys %$hash ) { - $self->{fields}->{$f}++; + foreach my $f ( keys %$hash ) { + $self->{fields}->{$type}->{$f}++; + $stat->{$type}->{$f}++; + } } + $log->debug("this record added following fields: ", sub { dump( $stat ) }); + return 1; } @@ -105,7 +111,9 @@ sub add { Close index - $index->finish; + my $affected = $index->finish; + +Returns of records saved in total =cut @@ -115,9 +123,34 @@ sub finish { my $log = $self->_get_logger(); my $fields = $self->{fields} || confess "no fields?"; + + $log->debug("fields = ", sub { dump $fields }); + my $path = $self->path || confess "no path?"; + my $webpac_dir = getcwd(); + chdir $path || $log->logdie("can't chdir($path) $!"); + + my $affected = 0; - my @field_names = %$fields; + foreach my $type ( $self->consume_outputs ) { + next unless defined $fields->{$type}; + $affected += $self->_sync_field( $fields->{$type} ); + } + + return $affected; +}; + +sub _sync_field { + my $self = shift; + + my $field_hash = shift || confess "no field?"; + my $path = $self->path || confess "no path?"; + + my $log = $self->_get_logger(); + + $log->debug("field_hash = ",sub { dump($field_hash) }); + + my @field_names = keys %$field_hash; if ( ! @field_names ) { $log->warn("normalization rules don't produce any data for search!"); @@ -126,12 +159,6 @@ sub finish { $log->info("syncing search fields: ", join(", ", @field_names)); - $log->debug("fields = ", sub { dump $fields }); - - my $webpac_dir = getcwd(); - - chdir $path || $log->logdie("can't chdir($path) $!"); - # push @INC, $path; Jifty->new(); my $system_user = Webpacus::CurrentUser->superuser; @@ -140,7 +167,7 @@ sub finish { my ( $count, $new, $updated ) = ( 0, 0, 0 ); foreach my $field ( @field_names ) { - my $items = $fields->{$field} || confess "no field?"; + my $items = $field_hash->{$field} || confess "no field?"; my ( $id, $msg ) = $o->load_by_cols( name => $field ); @@ -191,6 +218,15 @@ _END_OF_GLUE_ } +=head2 consume_outputs + +Returns array with names of supported output types for this module + +=cut + +sub consume_outputs { + return qw/search sorted/; +} =head1 AUTHOR diff --git a/t/5-output-webpacus.t b/t/5-output-webpacus.t index 74aa7d2..bc12cc5 100755 --- a/t/5-output-webpacus.t +++ b/t/5-output-webpacus.t @@ -3,7 +3,7 @@ use strict; use blib; -use Test::More tests => 10; +use Test::More tests => 11; BEGIN { use_ok( 'WebPAC::Test' ); @@ -17,6 +17,10 @@ ok(my $out = new WebPAC::Output::Webpacus({ %LOG }), "new"); +ok( $out->consume_outputs, 'consume_outputs' ); + +diag "consume_outputs = ",dump( $out->consume_outputs ) if $debug; + ok( $out->init, 'init' ); my $ds = { @@ -43,5 +47,5 @@ ok( $out->add( 100, { foo => { sorted => [ qw/foo bar baz/ ] } } ), 'add 100' ); diag "path: ",$out->path if $debug; -ok( $out->finish, 'finish' ); +cmp_ok( $out->finish, '==', 4, 'finish' ); -- 2.20.1