ResultSet/Display.pm untested

filter: Statementstmt 0.0% 0 / 48 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 92 CC 7 SCAR 22.3
CC
7
Cov
0%
display
20.0
new
12.0
display_one
6.0
CRAP 9.3
1 package VUser::ResultSet::Display;
2 use warnings;
3 use strict;
4  
5 # Copyright 2006 Randy Smith <perlstalker@vuser.org>
6 # $Id: Display.pm,v 1.5 2007-09-24 20:16:07 perlstalker Exp $
7  
8 use VUser::ExtLib qw(:config);
9 our $VERSION = "0.3.1";
10  
11 use base qw(Exporter);
12 our @EXPORT = ();
13 our @EXPORT_OK = ();
14 our %EXPORT_TAGS = ();
15  
16 my $c_sec = 'vuser'; # Configuration section
17  
18 sub new
19 {
20 my $class = shift;
21 my $cfg = shift;
22  
23 my $self = {};
24  
25 my $display_format = strip_ws($cfg->{$c_sec}{'display format'});
26 if (not defined $display_format
27 or $display_format eq ''
28 or $display_format eq 'normal'
29 or $display_format eq 'standard'
30 ) {
31 bless $self, $class;
32 } else {
33 # Load the display module
34 eval "require VUser::ResultSet::Display::$display_format;";
35 die "Unable to load display module $display_format: $@\n" if $@;
36 bless $self, "VUser::ResultSet::Display::$display_format";
37 }
38  
39 $self->init($cfg);
40 return $self;
41 }
42  
43 # Takes a ResultSet, array or array ref of ResultSets (or array refs of RSs)
44 # and displays them.
45 sub display
46 {
47 my $self = shift;
48 my @rsets = @_;
49
50 foreach my $rs (@rsets) {
51 if (defined $rs
52 and UNIVERSAL::isa($rs, 'VUser::ResultSet')
53 ) {
54 #print "Display RS with: ", ref $self, "\n";
55 eval { $self->display_one($rs); };
56 die "Can't display result set: $@\n" if $@;
57 } elsif (ref $rs eq 'ARRAY') {
58 # $rs is a list ref. Dereference the list and send it to
59 # display() again to process any ResultSets within that list.
60 $self->display(@$rs);
61 print "\n";
62 } else {
63 # It's not a VUser::ResultSet
64 }
65 }
66 }
67  
68 # Overloaded by sub-classes
69 sub display_one
70 {
71 my $self = shift;
72 my $rs = shift; # VUser::ResultSet
73  
74 my @meta = $rs->get_metadata;
75  
76 foreach my $row ($rs->results()) {
77 foreach (my $i = 0; $i < @meta; $i++) {
78 printf("%s: %s\n",
79 $meta[$i]->name,
80 defined $row->[$i]? $row->[$i] : 'undef');
81 }
82 #print "\n";
83 }
84 }
85  
86 sub init {}
87 sub version { return $VERSION; }
88  
89 1;
90  
91 __END__