File Coverage

blib/lib/SPOPS/Export.pm
Criterion Covered Total %
statement 16 24 66.6
branch 1 2 50.0
condition n/a
subroutine 5 9 55.5
pod n/a
total 22 35 62.8


line stmt bran cond sub pod time code
1             package SPOPS::Export;
2              
3             # $Id: Export.pm,v 3.4 2004/06/02 00:48:21 lachoy Exp $
4              
5 5     5   36180 use strict;
  5         15  
  5         237  
6 5     5   27 use base qw( Class::Accessor Class::Factory );
  5         12  
  5         5502  
7 5     5   14678 use SPOPS::Exception qw( spops_error );
  5         14  
  5         55  
8              
9             $SPOPS::Export::VERSION = sprintf("%d.%02d", q$Revision: 3.4 $ =~ /(\d+)\.(\d+)/);
10              
11 5     5   399 use constant AKEY => '_attrib';
  5         14  
  5         5408  
12              
13             my @FIELDS = qw( object_class where value include_id skip_fields DEBUG );
14             SPOPS::Export->mk_accessors( @FIELDS );
15              
16             sub new {
17 5     5   7418 my ( $pkg, $type, $params ) = @_;
18 5         13 my $class = eval { $pkg->get_factory_class( $type ) };
  5         53  
19 5 50       212 spops_error $@ if ( $@ );
20 0           my $self = bless( {}, $class );;
21 0           foreach my $field ( $self->get_fields ) {
22 0           $self->$field( $params->{ $field } );
23             }
24 0           return $self->initialize( $params );
25             }
26              
27              
28 0     0     sub initialize { return $_[0] }
29              
30 0     0     sub get_fields { return @FIELDS }
31              
32              
33             # Class::Accessor stuff
34              
35 0     0     sub get { return $_[0]->{ AKEY() }{ $_[1] } }
36 0     0     sub set { return $_[0]->{ AKEY() }{ $_[1] } = $_[2] }
37              
38              
39             # Main event
40              
41             sub run {
42             my ( $self ) = @_;
43             my $object_class = $self->object_class;
44             unless ( $object_class ) {
45             spops_error "Cannot export objects without an object class! ",
46             "Please set using\n",
47             "\$exporter->object_class( \$object_class )";
48             }
49             my @export_fields = $self->_find_export_fields;
50             my @output = ();
51             push @output, $self->create_header( \@export_fields );
52              
53             my $iter = $object_class->fetch_iterator({ where => $self->where,
54             value => $self->value,
55             DEBUG => $self->DEBUG });
56             while ( my $o = $iter->get_next ) {
57             push @output, $self->create_record( $o, \@export_fields );
58             }
59             push @output, $self->create_footer;
60             return join( "", @output );
61             }
62              
63             # Subclasses should override these actions as necessary -- note that
64             # if you remove the 'return' you'll get a nasty surprise, since the
65             # exporter object is returned :-)
66              
67             sub create_header { return }
68             sub create_record { return }
69             sub create_footer { return }
70              
71              
72             # Private
73              
74             sub _find_export_fields {
75             my ( $self ) = @_;
76             my %skip_fields = ();
77             my $object_class = $self->object_class;
78             if ( $self->skip_fields ) {
79             map { $skip_fields{ $_ }++ } @{ $self->skip_fields };
80             }
81             unless ( $self->include_id ) {
82             $skip_fields{ $object_class->CONFIG->{id_field} }++;
83             }
84             return grep { ! $skip_fields{ $_ } } @{ $object_class->field_list };
85             }
86              
87              
88             ########################################
89             # INITIALIZE
90              
91             __PACKAGE__->register_factory_type( object => 'SPOPS::Export::Object' );
92             __PACKAGE__->register_factory_type( xml => 'SPOPS::Export::XML' );
93             __PACKAGE__->register_factory_type( perl => 'SPOPS::Export::Perl' );
94             __PACKAGE__->register_factory_type( sql => 'SPOPS::Export::SQL' );
95             __PACKAGE__->register_factory_type( dbdata => 'SPOPS::Export::DBI::Data' );
96              
97             1;
98              
99             __END__