| 1 | # Perl | ||||||||||||||||||||
| 2 | # | ||||||||||||||||||||
| 3 | # Class Report::Porf::Table::Simple::AutoColumnConfigurator | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # Perl Open Report Framework (Porf) | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | # Configure Report columns automatically | ||||||||||||||||||||
| 8 | # | ||||||||||||||||||||
| 9 | # Ralf Peine, Wed May 14 10:39:50 2014 | ||||||||||||||||||||
| 10 | # | ||||||||||||||||||||
| 11 | # More documentation at the end of file | ||||||||||||||||||||
| 12 | #------------------------------------------------------------------------------ | ||||||||||||||||||||
| 13 | |||||||||||||||||||||
| 14 | 1 | $VERSION = "2.000"; | |||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | #-------------------------------------------------------------------------------- | ||||||||||||||||||||
| 20 | # | ||||||||||||||||||||
| 21 | # Report::Porf::Table::Simple::AutoColumnConfigurator | ||||||||||||||||||||
| 22 | # | ||||||||||||||||||||
| 23 | #-------------------------------------------------------------------------------- | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | package Report::Porf::Table::Simple::AutoColumnConfigurator; | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | # --- new Instance, Do NOT call direct!!! ----------------- | ||||||||||||||||||||
| 28 | sub new | ||||||||||||||||||||
| 29 | 13 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||
| 30 | 13 | my $caller = $_[0]; | |||||||||||||||||||
| 31 | 13 | ▶ | my $class = ref($caller) || $caller; | ||||||||||||||||||
|
Condition33%1/3
ref $caller || $caller
MC/DC0%0/2
ref $caller || $caller
ref $caller
$caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
| |||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | # let the class go | ||||||||||||||||||||
| 34 | 13 | my $self = {}; | |||||||||||||||||||
| 35 | 13 | bless $self, $class; | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 13 | $self->{Configurators} = {}; | |||||||||||||||||||
| 38 | 13 | $self->{max_column_width} = 60; | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 13 | return $self; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | # --- create_report_configuration as string list --- | ||||||||||||||||||||
| 44 | 1 | ▶ | sub report_configuration_as_string { | ||||||||||||||||||
|
Subroutine
report_configuration_as_string: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 45 | 1 | my ($self, | |||||||||||||||||||
| 46 | $list_ref, | ||||||||||||||||||||
| 47 | $max_rows_to_inspect | ||||||||||||||||||||
| 48 | ) = @_; | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | 1 | my $config_list = $self->create_report_configuration($list_ref, $max_rows_to_inspect); | |||||||||||||||||||
| 51 | 1 | my @result; | |||||||||||||||||||
| 52 | |||||||||||||||||||||
| 53 | 1 | foreach my $config (@$config_list) { | |||||||||||||||||||
| 54 | 6 | my $line = '$report->cc( '; | |||||||||||||||||||
| 55 | 6 | foreach my $key (sort(keys(%$config))) { | |||||||||||||||||||
| 56 | 24 | $line .= " $key => ". $config->{$key}.', '; | |||||||||||||||||||
| 57 | } | ||||||||||||||||||||
| 58 | 6 | $line .= ");"; | |||||||||||||||||||
| 59 | 6 | push (@result, $line); | |||||||||||||||||||
| 60 | } | ||||||||||||||||||||
| 61 | 1 | return \@result; | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | # --- create_report_configuration --- | ||||||||||||||||||||
| 65 | 13 | ▶ | sub create_report_configuration { | ||||||||||||||||||
|
Subroutine
create_report_configuration: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||||||||||
| 66 | 13 | my ($self, | |||||||||||||||||||
| 67 | $list_ref, | ||||||||||||||||||||
| 68 | $max_rows_to_inspect | ||||||||||||||||||||
| 69 | ) = @_; | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 13 | ▶ | return [] unless $list_ref && scalar @$list_ref; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$list_ref and scalar @$list_ref
MC/DC0%0/1
$list_ref and scalar @$list_ref
$list_ref
Truth table50%1/2
A
A= $list_ref
| |||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | 13 | my $ref_info = ref($list_ref->[0]); | |||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 13 | ▶ | return $self->create_hash_report_configuration($list_ref, $max_rows_to_inspect) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | if uc($ref_info) eq 'HASH'; | ||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 2 | ▶ | return $self->create_array_report_configuration($list_ref, $max_rows_to_inspect) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | if uc($ref_info) eq 'ARRAY'; | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | die "cannot create auto configuration for '$ref_info' elements."; | |||||||||||||||||||
| 82 | } | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # --- crate the default report configuration for a list of hashes -------- | ||||||||||||||||||||
| 85 | 11 | ▶ | sub create_hash_report_configuration { | ||||||||||||||||||
|
Subroutine
create_hash_report_configuration: called CC=14 SCAR=26.5
Pod
undocumented
| |||||||||||||||||||||
| 86 | 11 | my ($self, | |||||||||||||||||||
| 87 | $list_ref, | ||||||||||||||||||||
| 88 | $max_rows_to_inspect | ||||||||||||||||||||
| 89 | ) = @_; | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 11 | ▶ | return [] unless $list_ref && scalar @$list_ref; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$list_ref and scalar @$list_ref
MC/DC0%0/1
$list_ref and scalar @$list_ref
$list_ref
Truth table50%1/2
A
A= $list_ref
| |||||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 11 | my @config_list; | |||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 11 | my %hash_key_store = (); | |||||||||||||||||||
| 96 | 11 | ▶ | $max_rows_to_inspect = 10 unless defined $max_rows_to_inspect; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 97 | 11 | ▶ | $max_rows_to_inspect = $#$list_ref if $max_rows_to_inspect == -1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 98 | |||||||||||||||||||||
| 99 | 11 | my $row_count = 0; | |||||||||||||||||||
| 100 | 11 | foreach my $data (@$list_ref) { | |||||||||||||||||||
| 101 | 84 | foreach my $key (sort(keys(%$data))) { | |||||||||||||||||||
| 102 | 185 | ▶ | next unless defined $key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | 185 | ▶ | $hash_key_store{$key} = length ($key) unless $hash_key_store{$key}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | 185 | ▶ | my $text_length = length ($data->{$key} || '0'); | ||||||||||||||||||
|
Condition100%2/2
$data->{$key} || '0'
MC/DC100%1/1
$data->{$key} || '0'
$data->{$key}
Truth table100%2/2
A
A= $data->{$key}
| |||||||||||||||||||||
| 105 | $hash_key_store{$key} = $text_length | ||||||||||||||||||||
| 106 | 185 | ▶ | if $hash_key_store{$key} < $text_length; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | 84 | ▶ | last if $row_count++ >= $max_rows_to_inspect; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | } | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | 11 | foreach my $key (sort(keys(%hash_key_store))) { | |||||||||||||||||||
| 112 | 58 | my $width = $hash_key_store{$key}; | |||||||||||||||||||
| 113 | 58 | ▶ | $width = $self->{max_column_width} if $width > $self->{max_column_width}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 114 | 58 | push (@config_list, {-h => $key, -vn => $key, -w => $width, -a => 'l'}); | |||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | 11 | return \@config_list; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | # --- crate the default report configuration for a list of arrays -------- | ||||||||||||||||||||
| 121 | 2 | ▶ | sub create_array_report_configuration { | ||||||||||||||||||
|
Subroutine
create_array_report_configuration: called CC=14 SCAR=27.1
Pod
undocumented
| |||||||||||||||||||||
| 122 | 2 | my ($self, | |||||||||||||||||||
| 123 | $list_ref, | ||||||||||||||||||||
| 124 | $max_rows_to_inspect | ||||||||||||||||||||
| 125 | ) = @_; | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | 2 | ▶ | return [] unless $list_ref && scalar @$list_ref; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$list_ref and scalar @$list_ref
MC/DC0%0/1
$list_ref and scalar @$list_ref
$list_ref
Truth table50%1/2
A
A= $list_ref
| |||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 2 | my @config_list; | |||||||||||||||||||
| 130 | |||||||||||||||||||||
| 131 | 2 | ▶ | $max_rows_to_inspect = 10 unless defined $max_rows_to_inspect; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 2 | ▶ | $max_rows_to_inspect = $#$list_ref if $max_rows_to_inspect == -1; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 133 | |||||||||||||||||||||
| 134 | 2 | my $row_count = 0; | |||||||||||||||||||
| 135 | 2 | my $max_columns = 0; | |||||||||||||||||||
| 136 | 2 | my @column_lengths; | |||||||||||||||||||
| 137 | 2 | foreach my $data (@$list_ref) { | |||||||||||||||||||
| 138 | 12 | my $columns = scalar @$data; | |||||||||||||||||||
| 139 | 12 | ▶ | $max_columns = $columns if $columns > $max_columns; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 140 | 12 | foreach my $idx (0..($columns-1)) { | |||||||||||||||||||
| 141 | 58 | ▶ | $column_lengths[$idx] = $column_lengths[$idx] || '0'; | ||||||||||||||||||
|
Condition100%2/2
$column_lengths[$idx] || '0'
MC/DC100%1/1
$column_lengths[$idx] || '0'
$column_lengths[$idx]
Truth table100%2/2
A
A= $column_lengths[$idx]
| |||||||||||||||||||||
| 142 | 58 | ▶ | my $text_length = length ($data->[$idx] || '0'); | ||||||||||||||||||
|
Condition50%1/2
$data->[$idx] || '0'
MC/DC0%0/1
$data->[$idx] || '0'
$data->[$idx]
Truth table50%1/2
A
A= $data->[$idx]
| |||||||||||||||||||||
| 143 | 58 | ▶ | $column_lengths[$idx] = $text_length | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 144 | if $column_lengths[$idx] < $text_length; | ||||||||||||||||||||
| 145 | } | ||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 12 | ▶ | last if $row_count++ >= $max_rows_to_inspect; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 148 | } | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | 2 | foreach my $idx (0..($max_columns-1)) { | |||||||||||||||||||
| 151 | 11 | my $width = $column_lengths[$idx]; | |||||||||||||||||||
| 152 | 11 | ▶ | $width = $self->{max_column_width} if $width > $self->{max_column_width}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 153 | 11 | push (@config_list, {-h => ($idx+1).'. Column', -vi => $idx, -w => $width, -a => 'l'}); | |||||||||||||||||||
| 154 | } | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 2 | return \@config_list; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # --- create report with automatic configured columns ------------------ | ||||||||||||||||||||
| 160 | 10 | ▶ | sub create_report { | ||||||||||||||||||
|
Subroutine
create_report: called CC=5 SCAR=16.4
Pod
undocumented
| |||||||||||||||||||||
| 161 | 10 | my ($self, | |||||||||||||||||||
| 162 | $list_ref, | ||||||||||||||||||||
| 163 | $report_framework, | ||||||||||||||||||||
| 164 | $format | ||||||||||||||||||||
| 165 | ) = @_; | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 10 | ▶ | return undef unless $list_ref && scalar @$list_ref; | ||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$list_ref and scalar @$list_ref
MC/DC0%0/1
$list_ref and scalar @$list_ref
$list_ref
Truth table50%1/2
A
A= $list_ref
| |||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 10 | my $first_element = $list_ref->[0]; | |||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 10 | ▶ | $report_framework = Report::Porf::Framework::get() unless $report_framework; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 172 | 10 | my $report = $report_framework->create_report($format); | |||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 10 | $report->set_default_cell_value(''); | |||||||||||||||||||
| 175 | |||||||||||||||||||||
| 176 | 10 | foreach my $config_option (@{$self->create_report_configuration($list_ref)}) { | |||||||||||||||||||
| 177 | 50 | $report->cc (%$config_option); | |||||||||||||||||||
| 178 | } | ||||||||||||||||||||
| 179 | |||||||||||||||||||||
| 180 | 10 | $report->configure_complete(); | |||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 10 | return $report; | |||||||||||||||||||
| 183 | } | ||||||||||||||||||||