1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
|
# Data::Report::Base.pm -- Base class for reporters
# Author : Johan Vromans
# Created On : Wed Dec 28 13:18:40 2005
# Last Modified By: Johan Vromans
# Last Modified On: Sun Feb 9 20:34:18 2020
# Update Count : 319
# Status : Unknown, Use with caution!
package Data::Report::Base;
=head1 NAME
Data::Report::Base - Base class for reporter plugins
=head1 SYNOPSIS
This module implements that basic functionality common to all reporters.
Its documentation still has to be written.
=cut
use strict;
use warnings;
use Carp;
my $style_pat = qr/^[a-zA-Z]\w*$/;
################ User API ################
sub new {
my ($class, $args) = @_;
$class = ref($class) || $class;
my $type = delete($args->{type});
my $style = delete($args->{style}) || "default";
my $self = bless { _base_type => lc( $type // "" ),
_base_fields => [],
_base_fdata => {},
_base_style => $style,
}, $class;
$self->_checkname($style)
or croak("Invalid style name: \"$style\"");
foreach my $arg ( keys(%$args) ) {
my $val = delete($args->{$arg});
if ( my $c = $self->can("set_$arg") ) {
$c->($self, $val);
}
else {
croak("Unhandled attribute: \"$arg\"");
}
}
# Return object.
$self;
}
sub start {
my $self = shift;
$self->_argcheck(0);
croak("No layout specified") unless $self->{_base_fdata};
croak("Reporter already started") if $self->{_base_started};
$self->{_base_needpre} = 1;
$self->{_base_needhdr} = 1;
$self->{_base_needskip} = 0;
$self->set_output(\*STDOUT) unless $self->{_base_out};
$self->set_style("default") unless $self->{_base_style};
$self->set_topheading($self->can("_top_heading"))
unless $self->{_base_topheading};
$self->set_heading($self->can("_std_heading"))
unless $self->{_base_heading};
$self->set_stylist($self->can("_std_stylist"))
unless $self->{_base_stylist};
$self->{_base_close} ||= sub {};
$self->{_base_started} = 1;
$self->{_base_used} = 0;
}
sub add {
my ($self, $data) = @_;
croak("Reporter not started") unless $self->{_base_started};
while ( my($k,$v) = each(%$data) ) {
next if $k =~ /^_/;
croak("Invalid field: \"$k\"\n")
unless defined $self->{_base_fdata}->{$k};
}
}
sub finish {
my $self = shift;
$self->_argcheck(0);
croak("Reporter not started") unless $self->{_base_started};
$self->{_base_started} = 0;
}
sub close {
my $self = shift;
$self->_argcheck(0);
croak("Reporter is not finished") if $self->{_base_started};
$self->{_base_close}->();
}
################ Attributes ################
#### Type
sub get_type { shift->{_base_type} }
#### Style
sub set_style {
my ($self, $style) = @_;
$self->_argcheck(1);
$self->_checkname($style)
or croak("Invalid style name: \"$style\"");
$self->{_base_style} = $style;
}
sub get_style {
my $self = shift;
$self->_argcheck(0);
$self->{_base_style};
}
#### Layout
sub set_layout {
my ($self, $layout) = @_;
$self->_argcheck(1);
foreach my $col ( @$layout ) {
if ( $col->{name} ) {
$self->_checkname($col->{name})
or croak("Invalid column name: \"$col->{name}\"");
my $a = { name => $col->{name},
title => $col->{title} || ucfirst(lc(_T($a->{name}))),
width => $col->{width} || length($a->{title}),
align => $col->{align} || "<",
style => $col->{style} || $col->{name},
truncate => $col->{truncate},
};
$self->_checkname($a->{style})
or croak("Invalid column style: \"$a->{style}\"");
$self->{_base_fdata}->{$a->{name}} = $a;
push(@{$self->{_base_fields}}, $a);
}
else {
croak("Missing column name in layout\n");
}
}
# Return object.
$self;
}
#### Fields (order of)
sub set_fields {
my ($self, $f) = @_;
$self->_argcheck(1);
my @nf; # new order of fields
foreach my $fld ( @$f ) {
my $a = $self->{_base_fdata}->{$fld};
croak("Unknown field: \"$fld\"\n")
unless defined($a);
push(@nf, $a);
}
$self->{_base_fields} = \@nf;
# PBP: Return nothing sensible.
return;
}
sub get_fields {
my $self = shift;
$self->_argcheck(0);
[ map { $_->{name} } @{$self->{_base_fields}} ];
}
#### Width (set one or more)
sub set_width {
my ($self, $w) = @_;
while ( my($fld,$width) = each(%$w) ) {
croak("Unknown field: \"$fld\"\n")
unless defined($self->{_base_fdata}->{$fld});
my $ow = $self->{_base_fdata}->{$fld}->{width};
if ( $width =~ /^\+(\d+)$/ ) {
$ow += $1;
}
elsif ( $width =~ /^-(\d+)$/ ) {
$ow -= $1;
}
elsif ( $width =~ /^(\d+)\%$/ ) {
$ow *= $1;
$ow = int($ow/100);
}
elsif ( $width =~ /^\d+$/ ) {
$ow = $width;
}
else {
croak("Invalid width specification \"$width\" for field \"$fld\"\n");
}
$self->{_base_fdata}->{$fld}->{width} = $ow;
}
# PBP: Return nothing sensible.
return;
}
#### Width (get all)
sub get_widths {
my $self = shift;
$self->_argcheck(0);
{ map { $_ => $self->{_base_fdata}->{$_}->{width} } $self->get_fields }
}
#### Output
sub set_output {
my ($self, $out) = @_;
$self->_argcheck(1);
$self->{_base_close} = sub {};
if ( ref($out) ) {
if ( UNIVERSAL::isa($out, 'SCALAR') ) {
$self->{_base_out} = sub { $$out .= join("", @_) };
}
elsif ( UNIVERSAL::isa($out, 'ARRAY') ) {
$self->{_base_out} = sub {
push(@$out, map { +"$_\n" } split(/\n/, $_)) foreach @_;
};
}
else {
$self->{_base_out} = sub { print {$out} (@_) };
$self->{_base_close} = sub { CORE::close($out) or croak("Close: $!") };
}
}
else {
open(my $fd, ">", $out)
or croak("Cannot create \"$out\": $!");
$self->{_base_out} = sub { print {$fd} (@_) };
$self->{_base_close} = sub { CORE::close($fd) or croak("Close \"$out\": $!") };
}
}
#### Stylist
sub set_stylist {
my ($self, $stylist_code) = @_;
$self->_argcheck(1);
croak("Stylist must be a function (code ref)")
if $stylist_code && !UNIVERSAL::isa($stylist_code, 'CODE');
$self->{_base_stylist} = $stylist_code;
}
sub get_stylist {
my ($self) = @_;
$self->_argcheck(0);
$self->{_base_stylist};
}
#### Heading generator
sub set_heading {
my ($self, $heading_code) = @_;
$self->_argcheck(1);
croak("Header must be a function (code ref)")
if $heading_code && !UNIVERSAL::isa($heading_code, 'CODE');
$self->{_base_heading} = $heading_code;
}
sub get_heading {
my ($self) = @_;
$self->_argcheck(0);
$self->{_base_heading};
}
sub set_topheading {
my ($self, $heading_code) = @_;
$self->_argcheck(1);
croak("Header must be a function (code ref)")
if $heading_code && !UNIVERSAL::isa($heading_code, 'CODE');
$self->{_base_topheading} = $heading_code;
}
sub get_topheading {
my ($self) = @_;
$self->_argcheck(0);
$self->{_base_topheading} || sub {};
}
################ Friend methods ################
sub _argcheck {
my ($pkg, $exp) = @_;
my ($package, $filename, $line, $subroutine) = do { package DB; caller(1) };
my $got = scalar(@DB::args)-1;
return if $exp == $got;
$got ||= "none";
$Carp::CarpLevel++;
Carp::croak($subroutine." requires ".
( $exp == 0 ? "no arguments" :
$exp == 1 ? " 1 argument" :
" $exp arguments" ).
" ($got supplied)");
}
sub _get_fields {
my $self = shift;
$self->_argcheck(0);
$self->{_base_fields};
}
sub _get_fdata {
my $self = shift;
$self->_argcheck(0);
$self->{_base_fdata};
}
sub _print {
my $self = shift;
$self->{_base_out}->(@_);
$self->{_base_used}++;
}
sub _started {
my $self = shift;
$self->_argcheck(0);
$self->{_base_started};
}
sub _getstyle {
my ($self, $row, $cell) = @_;
$self->_argcheck(defined $cell ? 2 : 1);
my $stylist = $self->{_base_stylist};
return unless $stylist;
return $stylist->($self, $row) unless $cell;
my $a = $stylist->($self, "*", $cell) || {};
my $b = $stylist->($self, $row, $cell) || {};
return { %$a, %$b };
}
sub _checkhdr {
my $self = shift;
$self->_argcheck(0);
if ( $self->{_base_needhdr} ) {
$self->{_base_needhdr} = 0;
$self->_pageskip if $self->can("_pageskip");
$self->get_topheading->($self);
$self->get_heading->($self);
}
}
sub _needhdr {
my $self = shift;
$self->_argcheck(1);
$self->{_base_needhdr} = shift;
}
sub _does_needhdr {
my $self = shift;
$self->_argcheck(0);
$self->{_base_needhdr};
}
sub _checkname {
my $self = shift;
$self->_argcheck(1);
shift =~ $style_pat;
}
1;
=head1 AUTHOR
Johan Vromans, C<< <jvromans at squirrel.nl> >>
=head1 BUGS
Please report any bugs or feature requests to
C<bug-data-report at rt.cpan.org>, or through the web interface at
L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-Report>.
I will be notified, and then you'll automatically be notified of progress on
your bug as I make changes.
=head1 COPYRIGHT & LICENSE
Copyright 2006 Squirrel Consultancy, all rights reserved.
This program is free software; you can redistribute it and/or modify it
under the same terms as Perl itself.
=cut
|