/[webpac2]/trunk/lib/WebPAC/Output/JSON.pm
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /trunk/lib/WebPAC/Output/JSON.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 913 - (hide annotations)
Tue Oct 30 20:11:02 2007 UTC (16 years, 7 months ago) by dpavlin
File size: 1637 byte(s)
 r1372@llin:  dpavlin | 2007-10-30 20:26:52 +0100
 first real method in common WebPAC::Output as a move towards pluggable
 outputs

1 dpavlin 881 package WebPAC::Output::JSON;
2    
3     use warnings;
4     use strict;
5    
6 dpavlin 913 use base qw/WebPAC::Common WebPAC::Output Class::Accessor/;
7 dpavlin 881 __PACKAGE__->mk_accessors(qw(path));
8    
9 dpavlin 883 #use Encode qw/from_to/;
10 dpavlin 881 use Data::Dump qw/dump/;
11 dpavlin 883 use JSON;
12     use File::Slurp;
13 dpavlin 881
14     =head1 NAME
15    
16     WebPAC::Output::JSON - Create JSON output
17    
18     =head1 VERSION
19    
20     Version 0.00
21    
22     =cut
23    
24     our $VERSION = '0.00';
25    
26     =head1 SYNOPSIS
27    
28     Create JSON output for export into other sistems
29    
30     =head1 FUNCTIONS
31    
32     =head2 new
33    
34     my $output = new WebPAC::Output::JSON({
35     path => '/path/to/file.js',
36     });
37    
38     Options are:
39    
40     =over 4
41    
42     =item path
43    
44     path to JSON file
45    
46     =back
47    
48     =head2 init
49    
50     $output->init;
51    
52     =cut
53    
54     sub init {
55     my $self = shift;
56     my $log = $self->_get_logger;
57    
58     $log->debug('init');
59    
60 dpavlin 883 $self->{_data} = [];
61    
62 dpavlin 881 return 1;
63     }
64    
65    
66     =head2 add
67    
68     Adds one entry to database.
69    
70     $est->add( 42, $ds );
71    
72     =cut
73    
74     sub add {
75     my $self = shift;
76    
77     my ( $id, $ds ) = @_;
78    
79     my $log = $self->_get_logger;
80     $log->logdie("need id") unless defined $id;
81     $log->logdie("need ds") unless $ds;
82    
83     $log->debug("id: $id ds = ",dump($ds));
84    
85 dpavlin 913 push @{ $self->{_data} }, $self->ds_to_hash( $ds, 'display' );
86 dpavlin 883
87 dpavlin 881 return 1;
88     }
89    
90     =head2 finish
91    
92     $output->finish;
93    
94     =cut
95    
96     sub finish {
97     my $self = shift;
98    
99     my $log = $self->_get_logger();
100    
101 dpavlin 883 $log->info("writing JSON output to ", $self->path);
102     write_file( $self->path, objToJson( { items => $self->{_data} } ) );
103 dpavlin 881
104     }
105    
106     =head1 AUTHOR
107    
108     Dobrica Pavlinusic, C<< <dpavlin@rot13.org> >>
109    
110     =head1 COPYRIGHT & LICENSE
111    
112     Copyright 2007 Dobrica Pavlinusic, All Rights Reserved.
113    
114     This program is free software; you can redistribute it and/or modify it
115     under the same terms as Perl itself.
116    
117     =cut
118    
119     1; # End of WebPAC::Output::JSON

  ViewVC Help
Powered by ViewVC 1.1.26