File Coverage

blib/lib/Vote/Count/Helper/Table.pm
Criterion Covered Total %
statement 44 52 84.6
branch n/a
condition n/a
subroutine 9 10 90.0
pod 2 2 100.0
total 55 64 85.9


line stmt bran cond sub pod time code
1 2     2   1224 use strict;
  2         5  
  2         69  
2 2     2   12 use warnings;
  2         3  
  2         67  
3 2     2   51 use 5.024;
  2         8  
4              
5             package Vote::Count::Helper::Table;
6 2     2   9 no warnings 'experimental';
  2         4  
  2         97  
7 2     2   14 use feature qw /postderef signatures/;
  2         3  
  2         216  
8 2     2   14 use Sort::Hash;
  2         5  
  2         114  
9 2     2   12 use Vote::Count::TextTableTiny qw/generate_table/;
  2         4  
  2         142  
10              
11             our $VERSION='2.01';
12              
13             # ABSTRACT: Non OO Components for the Vote::Charge implementation of STV.
14              
15             =head1 NAME
16              
17             Vote::Count::Helper::Table
18              
19             =head1 VERSION 2.01
20              
21             =head1 Description
22              
23             Table Formatting Helpers for use within Vote::Count.
24              
25             =cut
26              
27             =pod
28              
29             =head1 SYNOPSIS
30              
31             use Vote::Count::Helper::Table 'ChargeTable';
32             # $chargesPerChoice and $chargedPerChoice are from Vote::Count::Charge::Cascade
33             say ChargeTable( $chargesPerChoice, $chargedPerChoice );
34              
35             use Vote::Count::Helper::Table 'WeightedTable';
36             # When weighted voting is used will generate a table
37             # with the Top Count and Approval totals
38             say WeightedTable( $STV_Election );
39              
40             =cut
41              
42             use Exporter::Easy (
43 2         15 OK => [ 'WeightedTable', 'ChargeTable' ],
44 2     2   511 );
  2         1480  
45              
46             =head2 ChargeTable
47              
48             Arguments: $chargesPerChoice, $chargedPerChoice
49              
50             chargesPerChoice is a HashRef with the choices as keys, and the values the charge assessed each ballot supporting the choice.
51              
52             chargedPerChoice is a HashRef with the choices as keys and the values a HashRef with the keys value, count, surplus, where value is the total vote value charged for the choice, count is the number of ballots that contributed, and surplus the value above quota charged.
53              
54             =cut
55              
56 0     0 1 0 sub ChargeTable ( $chargesPerChoice, $chargedPerChoice ) {
  0         0  
  0         0  
  0         0  
57 0         0 my @rows = (['Choice','Charge','Value Charged', 'Votes Charged','Surplus'] );
58 0         0 for my $c ( sort keys $chargesPerChoice->%* ) {
59             push @rows, [
60             $c, $chargesPerChoice->{$c},
61             $chargedPerChoice->{$c}{'value'},
62             $chargedPerChoice->{$c}{'count'},
63 0         0 $chargedPerChoice->{$c}{'surplus'}
64             ]
65             }
66 0         0 return generate_table(
67             rows => \@rows,
68             style => 'markdown',
69             align => [qw/ l l r r r/]
70             ) . "\n";
71             }
72              
73             =head2 WeightedTable
74              
75             Formats the current Vote Totals by Approval and Top Count when weighted voting is in use, for STV/Vote Charge methods.
76              
77             =cut
78              
79 1     1 1 8 sub WeightedTable ( $I ) {
  1         2  
  1         2  
80 1         9 my $approval = $I->Approval()->RawCount();
81 1         16 my $tc = $I->TopCount();
82 1         4 my $tcr = $tc->RawCount();
83 1         35 my $vv = $I->VoteValue();
84 1         4 my %data =();
85 1         6 my @active = $I->GetActiveList();
86 1         5 for my $choice ( @active ) {
87             $data{ $choice } = {
88             'votevalue' => $tcr->{ $choice },
89             'votes' => sprintf( "%.2f",$tcr->{ $choice } / $vv),
90             'approvalvalue' => $approval->{ $choice },
91 8         74 'approval' => sprintf( "%.2f", $approval->{ $choice } / $vv),
92             };
93             }
94 1         5 my @rows = ( [ 'Rank', 'Choice', 'Votes', 'VoteValue', 'Approval', 'Approval Value' ] );
95 1         16 my %byrank = $tc->HashByRank()->%*;
96 1         8 for my $r ( sort { $a <=> $b } ( keys %byrank ) ) {
  9         15  
97 5         11 my @choice = sort $byrank{$r}->@*;
98 5         7 for my $choice (@choice) {
99             # my $votes = $tcr->{$choice};
100 8         10 my $D = $data{$choice};
101             my @row = (
102             $r, $choice, $D->{'votes'}, $D->{'votevalue'},
103 8         27 $D->{'approval'}, $D->{'approvalvalue'} );
104 8         18 push @rows, ( \@row );
105             }
106             }
107 1         8 return generate_table(
108             rows => \@rows,
109             style => 'markdown',
110             align => [qw/ l l r r r r/]
111             ) . "\n";
112             }
113             1;
114              
115             #FOOTER
116              
117             =pod
118              
119             BUG TRACKER
120              
121             L<https://github.com/brainbuz/Vote-Count/issues>
122              
123             AUTHOR
124              
125             John Karr (BRAINBUZ) brainbuz@cpan.org
126              
127             CONTRIBUTORS
128              
129             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
130              
131             LICENSE
132              
133             This module is released under the GNU Public License Version 3. See license file for details. For more information on this license visit L<http://fsf.org>.
134              
135             SUPPORT
136              
137             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
138              
139             =cut
140