r8980@llin: dpavlin | 2005-11-20 00:49:22 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 19 Nov 2005 23:48:24 +0000 (23:48 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Sat, 19 Nov 2005 23:48:24 +0000 (23:48 +0000)
 implement data_structure that returns HASH and not ARRAY.

 Little explanation for this rationale:

 Array was needed back in WebPAC v1 because order of tags in import_xml was
 important. However, since we are no longer depending on order of tags in
 input/*.xml, hash is much better choice.

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

conf/output/tt/html.tt
lib/WebPAC/DB.pm
lib/WebPAC/Normalize.pm
lib/WebPAC/Output/TT.pm
t/3-normalize-xml.t
t/4-db.t
t/5-output-tt.t
t/6-unit.t
web/browse.cgi

index b70c28b..eb98b75 100644 (file)
@@ -6,7 +6,8 @@
 <body>
 <table>
 [%
-       FOREACH d IN data ;
+       FOREACH key IN data.keys ;
+               d = data.key ;
                IF d.display
 -%]
 <tr class="[% d.tag %]">
index 3e24397..b5cbdb2 100644 (file)
@@ -110,13 +110,13 @@ sub path {
 
 Retrive from disk one data_structure records using field 000 as key
 
-  my @ds = $db->load_ds($rec);
+  my $ds = $db->load_ds($rec);
 
 This function will also perform basic sanity checking on returned
 data and disable caching if data is corrupted (or changed since last
 update).
 
-Returns array or undef if cacheing is disabled or unavailable.
+Returns hash or undef if cacheing is disabled or unavailable.
 
 =cut
 
@@ -154,7 +154,7 @@ sub load_ds {
                                        }
                                };
                                if ($ok && $ds_ref->{'ds'}) {
-                                       return @{ $ds_ref->{'ds'} };
+                                       return $ds_ref->{'ds'};
                                } else {
                                        $log->warn("cache entry $cache_path corrupt. Use rm $cache_path/* to re-create it on next run!");
                                        undef $self->{'path'};
@@ -173,7 +173,7 @@ sub load_ds {
 Store data_structure on disk.
 
   $db->save_ds(
-       ds => \@ds,
+       ds => $ds,
        current_filename => $self->{'current_filename'},
        headline => $self->{'headline'},
   );
index afcf1c1..1e21425 100644 (file)
@@ -11,11 +11,11 @@ WebPAC::Normalize - data mungling for normalisation
 
 =head1 VERSION
 
-Version 0.01
+Version 0.02
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 SYNOPSIS
 
@@ -122,7 +122,7 @@ C<conf/normalize/*.xml>.
 
 This structures are used to produce output.
 
- my @ds = $webpac->data_structure($rec);
+ my $ds = $webpac->data_structure($rec);
 
 B<Note: historical oddity follows>
 
@@ -143,9 +143,9 @@ sub data_structure {
        my $cache_file;
 
        if ($self->{'db'}) {
-               my @ds = $self->{'db'}->load_ds($rec);
-               $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) });
-               return @ds if ($#ds > 0);
+               my $ds = $self->{'db'}->load_ds($rec);
+               $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
+               return $ds if ($ds);
                $log->debug("cache miss, creating");
        }
 
@@ -160,7 +160,7 @@ sub data_structure {
                $self->{tags_by_order} = \@sorted_tags;
        }
 
-       my @ds;
+       my $ds;
 
        $log->debug("tags: ",sub { join(", ",@sorted_tags) });
 
@@ -243,14 +243,14 @@ sub data_structure {
 
                        # TODO: name_sigular, name_plural
                        my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
-                       $row->{'name'} = $name ? $self->_x($name) : $field;
+                       my $row_name = $name ? $self->_x($name) : $field;
 
                        # post-sort all values in field
                        if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {
                                $log->warn("sort at field tag not implemented");
                        }
 
-                       push @ds, $row;
+                       $ds->{$row_name} = $row;
 
                        $log->debug("row $field: ",sub { Dumper($row) });
                }
@@ -260,14 +260,16 @@ sub data_structure {
        $log->logdie("there is no current_filename defined! Do you have filename tag in conf/normalize/?.xml") unless ($self->{'current_filename'});
 
        $self->{'db'}->save_ds(
-               ds => \@ds,
+               ds => $ds,
                current_filename => $self->{'current_filename'},
                headline => $self->{'headline'},
        ) if ($self->{'db'});
 
-       $log->debug("ds: ", sub { Dumper(@ds) });
+       $log->debug("ds: ", sub { Dumper($ds) });
 
-       return @ds;
+       $log->logconfess("data structure returned is not array any more!") if wantarray;
+
+       return $ds;
 
 }
 
index 77eb897..d9bd3a1 100644 (file)
@@ -70,7 +70,7 @@ Create output from in-memory data structure using Template Toolkit template.
 
  my $text = $tt->apply(
        template => 'text.tt',
-       data => \@ds
+       data => $ds
  );
 
 It also has follwing template toolikit filter routies defined:
@@ -90,7 +90,7 @@ sub apply {
 
 =head3 tt_filter_type
 
-filter to return values of specified from @ds
+filter to return values of specified from $ds
 
 =cut
 
@@ -109,13 +109,11 @@ filter to return values of specified from @ds
 
                        my ($name,$join) = @_;
 
-                       die "no data array" unless ($data->{'data'} && ref($data->{'data'}) eq 'ARRAY');
+                       die "no data hash" unless ($data->{'data'} && ref($data->{'data'}) eq 'HASH');
                        # Hm? Should we die here?
                        return unless ($name);
 
-                       my $item = first { $_->{'name'} eq $name } @{ $data->{'data'} };
-
-                       return unless($item);
+                       my $item = $data->{'data'}->{$name} || return;
 
                        my $v = $item->{$type} || return;
 
@@ -153,7 +151,7 @@ to a file.
  $tt->to_file(
         file => 'out.txt',
        template => 'text.tt',
-       data => \@ds
+       data => $ds
  );
 
 =cut
index 2320777..9743bbe 100755 (executable)
@@ -129,6 +129,7 @@ foreach my $fld (keys %$rec) {
        ok(! $found, "not found");
 }
 
-ok(my @ds = $n->data_structure( $rec ), "data_structure");
+ok(my $ds = $n->data_structure( $rec ), "data_structure");
+
+diag Dumper($rec, $ds);
 
-#diag Dumper($rec, \@ds);
index fa21dd8..08f662c 100755 (executable)
--- a/t/4-db.t
+++ b/t/4-db.t
@@ -42,7 +42,7 @@ cmp_ok($db->{'path'}, 'eq', $path, "path");
 
 ok(! $db->path(''), "path - disable caching");
 
-cmp_ok($db->{'path'}, '==', undef, "no path");
+ok(! defined($db->{'path'}), "no path");
 
 ok($db->path( $path ), "path($path)");
 
@@ -53,32 +53,33 @@ ok(! $db->load_ds({ '000' => '000' }), 'load_ds');
 
 ok(! $db->save_ds(), "save_ds");
 
-my @ds = [ {
-       'name' => 'Izvor: ',
-       'tag' => 'Source',
-       'display' => [ 'foo' ]
-       }, {
-       'name' => 'ID',
-       'tag' => 'IDths',
-       'swish' => [ 'bar' ],
-       'lookup_key' => [ 'bar' ]
-       }, {
+my $ds = {
+       'Source' => {
+               'name' => 'Izvor: ',
+               'tag' => 'Source',
+               'display' => [ 'foo' ]
+       },
+       'ID' => {
+               'name' => 'ID',
+               'tag' => 'IDths',
+               'swish' => [ 'bar' ],
+               'lookup_key' => [ 'bar' ]
+       },
        'filename' => [ 'out/thes/001.html' ],
        'name' => 'filename',
        'tag' => 'filename'
-       },
-];
+};
 
 ok(! $db->save_ds(), "empty save_ds");
 throws_ok { $db->save_ds( foo => 1 ) } qr/ds/, "save_ds - ds";
-throws_ok { $db->save_ds( ds => \@ds ) } qr/current_filename/, "save_ds - current_filename";
-throws_ok { $db->save_ds( ds => \@ds, 'current_filename' => 'foo' ) } qr/headline/, "save_ds - headline";
+throws_ok { $db->save_ds( ds => $ds ) } qr/current_filename/, "save_ds - current_filename";
+throws_ok { $db->save_ds( ds => $ds, 'current_filename' => 'foo' ) } qr/headline/, "save_ds - headline";
 
-ok($db->save_ds( ds => \@ds, 'current_filename' => 'foo', 'headline' => 'bar' ), "save_ds");
+ok($db->save_ds( ds => $ds, 'current_filename' => 'foo', 'headline' => 'bar' ), "save_ds");
 
-ok(my @ds2 = $db->load_ds({ '000' => '000' }), "load_ds");
+ok(my $ds2 = $db->load_ds({ '000' => '000' }), "load_ds");
 
-is_deeply(\@ds, \@ds2, "loaded data");
+is_deeply($ds, $ds2, "loaded data");
 
 ok(! $db->load_ds({ '000' => 42 }), "load_ds non-existing");
 
index 4d1177a..b645630 100755 (executable)
@@ -19,25 +19,26 @@ ok(my $tt = new WebPAC::Output::TT(
        no_log => 1,
 ), "new");
 
-my @ds = [ {
-       'name' => 'Izvor: ',
-       'tag' => 'Source',
-       'display' => [ 'foo' ]
-       }, {
-       'name' => 'ID',
-       'tag' => 'IDths',
-       'swish' => [ 'bar' ],
-       'lookup_key' => [ 'bar' ]
-       }, {
+my $ds = {
+       'Source' => {
+               'name' => 'Izvor: ',
+               'tag' => 'Source',
+               'display' => [ 'foo' ]
+               },
+       'ID' => {
+               'name' => 'ID',
+               'tag' => 'IDths',
+               'swish' => [ 'bar' ],
+               'lookup_key' => [ 'bar' ]
+               },
        'filename' => [ 'out/thes/001.html' ],
        'name' => 'filename',
        'tag' => 'filename'
-       },
-];
+};
 
 throws_ok { $tt->apply( template => 'foo', data => [] ) } qr/error.*foo/, "apply without template";
 
-cmp_ok(my $text = $tt->apply( template => 'text.tt', data => @ds ), '=~', qr/Source.*foo/, "apply");
+cmp_ok(my $text = $tt->apply( template => 'text.tt', data => $ds ), '=~', qr/Source.*foo/, "apply");
 
 diag $text;
 
index cac9e66..c02c8d4 100755 (executable)
@@ -65,13 +65,13 @@ ok(my $out = new WebPAC::Output::TT(
 
 while (my $row = $isis->fetch) {
        
-       ok(my @ds = $n->data_structure($row), "data_structure");
+       ok(my $ds = $n->data_structure($row), "data_structure");
 
-#      diag Dumper(\@ds);
+#      diag Dumper($ds);
 
        ok(my $html = $out->apply(
                template => 'html.tt',
-               data => \@ds,
+               data => $ds,
        ), "apply");
 
        $html =~ s#\s*[\n\r]+\s*##gs;
index 916d946..3981a86 100755 (executable)
@@ -76,14 +76,14 @@ sub get_file_in_html($) {
 
 if ($q->path_info =~ m#xml#) {
 
-       my @ds = $db->load_ds($rec);
+       my $ds = $db->load_ds($rec);
 
-       if (@ds && $#ds > 0) {
+       if ($ds) {
                print qq{<response>
 <action type='html' target='div_record' errorCode='' errorMessage='' >
                }, $iconv_utf8->convert( $out->apply(
                        template => $template_file,
-                       data => \@ds,
+                       data => $ds,
                ) ), qq{
 
 </action>