r1372@llin: dpavlin | 2007-10-30 20:26:52 +0100
authorDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 30 Oct 2007 20:11:02 +0000 (20:11 +0000)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 30 Oct 2007 20:11:02 +0000 (20:11 +0000)
 first real method in common WebPAC::Output as a move towards pluggable
 outputs

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

lib/WebPAC/Output.pm
lib/WebPAC/Output/JSON.pm

index c12b7a1..1ec5d8d 100644 (file)
@@ -3,6 +3,8 @@ package WebPAC::Output;
 use warnings;
 use strict;
 
+use Carp qw/confess/;
+
 =head1 NAME
 
 WebPAC::Output - The great new WebPAC::Output!
@@ -17,34 +19,39 @@ our $VERSION = '0.01';
 
 =head1 SYNOPSIS
 
-Quick summary of what the module does.
+Common routines for output formats
 
-Perhaps a little code snippet.
+=head1 FUNCTIONS
 
-    use WebPAC::Output;
+=head2 ds_to_hash
 
-    my $foo = WebPAC::Output->new();
-    ...
+  my $hash = $self->ds_to_hash( $ds, 'display' );
 
-=head1 EXPORT
+=cut
 
-A list of functions that can be exported.  You can delete this section
-if you don't export anything, such as for a purely object-oriented module.
+sub ds_to_hash {
+       my $self = shift;
 
-=head1 FUNCTIONS
+       my ( $ds, $type ) = @_;
 
-=head2 function1
+       confess "need ds" unless $ds;
+       confess "need type" unless $type;
 
-=cut
+       my $hash;
 
-sub function1 {
-}
+       foreach my $t ( keys %$ds ) {
+               my $name = lc($t);
+               $name =~ s/\W+/_/g;
 
-=head2 function2
+               # FIXME get rid of non hash values in data_structure for consistency?
+               next unless ref($ds->{$t}) eq 'HASH';
 
-=cut
+               if ( defined( $ds->{$t}->{$type} ) ) {
+                       $hash->{$name} = $ds->{$t}->{$type};
+               }
+       }
 
-sub function2 {
+       return $hash;
 }
 
 =head1 AUTHOR
@@ -53,7 +60,7 @@ Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
 
 =head1 COPYRIGHT & LICENSE
 
-Copyright 2005 Dobrica Pavlinusic, All Rights Reserved.
+Copyright 2005-2007 Dobrica Pavlinusic, All Rights Reserved.
 
 This program is free software; you can redistribute it and/or modify it
 under the same terms as Perl itself.
index 7cc4089..939d295 100644 (file)
@@ -3,7 +3,7 @@ package WebPAC::Output::JSON;
 use warnings;
 use strict;
 
-use base qw/WebPAC::Common Class::Accessor/;
+use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
 __PACKAGE__->mk_accessors(qw(path));
 
 #use Encode qw/from_to/;
@@ -82,21 +82,7 @@ sub add {
 
        $log->debug("id: $id ds = ",dump($ds));
 
-       my $item;
-
-       foreach my $t ( keys %$ds ) {
-               my $name = lc($t);
-               $name =~ s/\W+/_/g;
-
-               # FIXME get rid of non hash values in data_structure for consistency?
-               next unless ref($ds->{$t}) eq 'HASH';
-
-               if ( defined( $ds->{$t}->{display} ) ) {
-                       $item->{$name} = $ds->{$t}->{display};
-               }
-       }
-
-       push @{ $self->{_data} }, $item;
+       push @{ $self->{_data} }, $self->ds_to_hash( $ds, 'display' );
 
        return 1;
 }