File Coverage

blib/lib/Vote/Count/Score.pm
Criterion Covered Total %
statement 48 48 100.0
branch 8 8 100.0
condition 4 4 100.0
subroutine 9 9 100.0
pod 2 2 100.0
total 71 71 100.0


line stmt bran cond sub pod time code
1 39     39   22942 use strict;
  39         137  
  39         1308  
2 39     39   232 use warnings;
  39         82  
  39         1024  
3 39     39   741 use 5.024;
  39         140  
4 39     39   218 use feature qw /postderef signatures/;
  39         82  
  39         3764  
5              
6             package Vote::Count::Score;
7              
8 39     39   264 use Moose::Role;
  39         106  
  39         489  
9              
10             # use Storable 3.15 qw(dclone);
11             # use Try::Tiny;
12              
13             our $VERSION='2.00';
14              
15             =head1 NAME
16              
17             Vote::Count::Score
18              
19             =head1 VERSION 2.00
20              
21             =cut
22              
23             # ABSTRACT: Provides Score Method for Range Ballots to Vote::Count objects
24              
25 39     39   213444 no warnings 'experimental';
  39         98  
  39         1767  
26 39     39   260 use Vote::Count::RankCount;
  39         87  
  39         21673  
27             # use Try::Tiny;
28             # use boolean;
29              
30             =pod
31              
32             =head1 Synopsis
33              
34             my $RangeElection = Vote::Count->new(
35             BallotSet => read_range_ballots('t/data/tennessee.range.json')
36             );
37             my $scored = $RangeElection->Score();
38              
39             =head1 Range Score Methods
40              
41             When Range (Cardinal) Ballots are used, it is simple and obvious to total the scores provided by the voters. This contrasts to the related Borda Method which assigns scores based on position on a Ranked Ballot.
42              
43             =head2 Score
44              
45             Returns a RankCount Object with the choices scored using the scores set by the voters, for Range Ballots.
46              
47             =cut
48              
49 11     11 1 1456 sub Score ( $self, $active = undef ) {
  11         21  
  11         19  
  11         16  
50 11         308 my $depth = $self->BallotSet()->{'depth'};
51             # my @Ballots = $self->BallotSet()->{'ballots'}->@*;
52 11 100       105 $active = $self->Active() unless defined $active;
53 11         40 my %scores = ( map { $_ => 0 } keys( $active->%* ) );
  56         115  
54 11         272 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
55 48         98 for my $choice ( keys %scores ) {
56 272 100       493 if ( defined $ballot->{'votes'}{$choice} ) {
57 122         210 $scores{$choice} += $ballot->{'count'} * $ballot->{'votes'}{$choice};
58             }
59             }
60             }
61 11         54 return Vote::Count::RankCount->Rank( \%scores );
62             }
63              
64             =head2 RangeBallotPair
65              
66             Used for pairings against Range Ballots. Used by Condorcet and STAR.
67              
68             where $I is a Vote::Count object created with a Range Ballot Set.
69              
70             my ( $countA, $countB ) = $I->RangeBallotPair( $A, $B) ;
71              
72             =cut
73              
74 141     141 1 1142 sub RangeBallotPair ( $self, $A, $B ) {
  141         210  
  141         193  
  141         190  
  141         199  
75 141         182 my $countA = 0;
76 141         203 my $countB = 0;
77 141         407 my $approval = $self->Approval();
78 141         4224 for my $ballot ( $self->BallotSet()->{'ballots'}->@* ) {
79 836   100     2004 my $prefA = $ballot->{'votes'}{$A} || 0;
80 836   100     1750 my $prefB = $ballot->{'votes'}{$B} || 0;
81 836 100       1599 if ( $prefA > $prefB ) { $countA += $ballot->{'count'} }
  194 100       324  
82 207         339 elsif ( $prefA < $prefB ) { $countB += $ballot->{'count'} }
83             else { }
84             }
85 141         1206 return ( $countA, $countB );
86             }
87              
88             1;
89              
90             #FOOTER
91              
92             =pod
93              
94             BUG TRACKER
95              
96             L<https://github.com/brainbuz/Vote-Count/issues>
97              
98             AUTHOR
99              
100             John Karr (BRAINBUZ) brainbuz@cpan.org
101              
102             CONTRIBUTORS
103              
104             Copyright 2019-2021 by John Karr (BRAINBUZ) brainbuz@cpan.org.
105              
106             LICENSE
107              
108             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>.
109              
110             SUPPORT
111              
112             This software is provided as is, per the terms of the GNU Public License. Professional support and customisation services are available from the author.
113              
114             =cut
115