Count/Common.pm

filter: Statementstmt 98.3% 59 / 60 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 19 / 19 Podpod 80.0% 8 / 10 totaltotal 96.0% 97 / 101 CC 10 SCAR 8.0
CC
10
Cov
97%
VotesActive
5.1
_defaultactive
3.0
BallotSetType
3.0
CRAP 2.2
« Count/Start.pm ↑ Summary Count/Log.pm »
1 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 25 ▶use 5.022;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 package Vote::Count::Common;
6 25 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 25 ▶use feature qw /postderef signatures/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 25 ▶no warnings 'experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 25 ▶use Storable 3.15 'dclone';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 25 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # ABSTRACT: Role shared by Count and Matrix for common functionality. See Vote::Count Documentation.
15  
16 25 our $VERSION='1.10';
17  
18 =head1 NAME
19  
20 Vote::Count::Common
21  
22 =head1 VERSION 1.10
23  
24 =head1 Synopsis
25  
26 This Role is consumed by Vote::Count and Vote::Count::Matrix. It provides common methods for the Active Set.
27  
28 =cut
29  
30 25 has 'Active' => (
31 is => 'ro',
32 isa => 'HashRef',
33 lazy => 1,
34 builder => '_defaultactive',
35 );
36  
37 25 has 'VoteValue' => (
38 is => 'ro',
39 isa => 'Int',
40 default => 1,
41 );
42  
43 25 has 'WithdrawalList' => (
44 is => 'rw',
45 isa => 'Str',
46 required => 0,
47 );
48  
49 ▶sub GetChoices ( $self ) {
Subroutine GetChoices: called CC=1 SCAR=0.0
Pod undocumented
50 7 return sort keys( $self->BallotSet()->{'choices'}->%* );
51 }
52  
53 ▶sub _defaultactive ( $self ) {
Subroutine _defaultactive: called CC=3 SCAR=11.0
54 83 my $active = dclone $self->BallotSet()->{'choices'} ;
55 83 ▶ if ( $self->WithdrawalList ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->WithdrawalList) 1 82 83
56 1 for my $w (path( $self->WithdrawalList )->lines({ chomp => 1})) {
57 4 delete $active->{$w};
58 }
59 }
60 83 return $active;
61 }
62  
63 ▶sub SetActive ( $self, $active ) {
Subroutine SetActive: called CC=2 SCAR=6.9
Pod documented
64 # Force deref
65 28 $self->{'Active'} = dclone $active;
66 # if there is a child PairMatrix, update it too.
67 28 ▶ if ( defined $self->{'PairMatrix'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"PairMatrix"}) 8 20 28
68 8 $self->{'PairMatrix'}{'Active'} = $self->{'Active'};
69 }
70 }
71  
72 ▶sub ResetActive ( $self ) {
Subroutine ResetActive: called CC=1 SCAR=0.0
Pod documented
73 1 $self->{'Active'} = $self->_defaultactive();
74 }
75  
76 # I was typing the equivalent too often. made a method.
77 ▶sub SetActiveFromArrayRef ( $self, $active ) {
Subroutine SetActiveFromArrayRef: called CC=1 SCAR=0.0
Pod documented
78 5 $self->SetActive( { map { $_ => 1 } $active->@* } );
79 }
80  
81 ▶sub GetActive ( $self ) {
Subroutine GetActive: called CC=1 SCAR=0.0
Pod documented
82 # Force deref
83 4 my $active = $self->Active();
84 4 return dclone $active;
85 }
86  
87 # this deref also happens a lot
88 ▶sub GetActiveList( $self ) {
Subroutine GetActiveList: called CC=1 SCAR=0.0
Pod documented
89 55 return ( sort( keys( $self->Active->%* ) ) );
90 }
91  
92 ▶sub VotesCast ( $self ) {
Subroutine VotesCast: called CC=1 SCAR=0.0
Pod documented
93 34 return $self->BallotSet()->{'votescast'};
94 }
95  
96 ▶sub VotesActive ( $self ) {
Subroutine VotesActive: called CC=5 SCAR=16.2
Pod documented
97 6 ▶ unless ( $self->BallotSet()->{'options'}{'rcv'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->BallotSet->{"options"}{"rcv"}) 0 6 6
98 0 die "VotesActive Method only supports rcv";
99 }
100 6 my $set = $self->BallotSet();
101 6 my $active = $self->Active();
102 6 my $activeCount = 0;
103 LOOPVOTESACTIVE:
104 6 for my $B ( values $set->{ballots}->%* ) {
105 36 for my $V ( $B->{'votes'}->@* ) {
106 39 ▶ if ( defined $active->{$V} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $active->{$V}) 33 6 39
107 33 $activeCount += $B->{'count'};
108 33 next LOOPVOTESACTIVE;
109 }
110 }
111 }
112 6 return $activeCount;
113 }
114  
115 ▶sub BallotSetType ( $self ) {
Subroutine BallotSetType: called CC=3 SCAR=11.0
Pod undocumented
116 14 ▶ if ( $self->BallotSet()->{'options'}{'rcv'} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($self->BallotSet->{'options'}{'rcv'}) { } 9 5 14
elsif ($self->BallotSet->{'options'}{'range'}) { } 4 1 5
117 9 return 'rcv';
118 }
119 elsif ( $self->BallotSet()->{'options'}{'range'} ) {
120 4 return 'range';
121 }
122 else {
123 1 die "BallotSetType is undefined or unknown type.";
124 }
125 }
126  
127 ▶sub GetBallots ( $self ) {
Subroutine GetBallots: called CC=1 SCAR=0.0
Pod documented
128 2 return $self->BallotSet()->{'ballots'};
129 }
130  
131 25 1;
132  
133 =head2 Usage
134  
135 This role is consumed by Vote::Count and Vote::Count::Matrix.
136  
137 =head3 new
138  
139 Has the following Attributes:
140  
141 =head4 WithdrawalList
142  
143 A text file containing choices 1 per line that are withdrawn. Use when a choice may be included in the ballots but should be treated as not-present. Removing a choice from the choices list in a Ballot File will generate an exception from ReadBallots if it appears on any Ballots. Withdrawing a choice will exclude it from the Active Set if it is present in the Ballots.
144  
145 =head4 VoteValue
146  
147 Use to set a Vote Value for methods that weight votes. The default value is 1.
148  
149 =head3 Active
150  
151 Get Active Set as HashRef to the active set. Changing the new HashRef will change the internal Active Set, GetActive is recommended as it will return a HashRef that is a copy instead.
152  
153 =head3 GetActive
154  
155 Returns a hashref containing a copy of the Active Set.
156  
157 =head3 Choices
158  
159 Returns an array of all of the Choices in the Ballot Set.
160  
161 =head3 GetActiveList
162  
163 Returns a simple array of the members of the Active Set.
164  
165  
166 =head3 ResetActive
167  
168 Sets the Active Set to the full choices list of the BallotSet.
169  
170 =head3 SetActive
171  
172 Sets the Active Set to provided HashRef. The values to the hashref should evaluate as True.
173  
174 =head3 SetActiveFromArrayRef
175  
176 Same as SetActive except it takes an ArrayRef of the choices to be set as Active.
177  
178  
179 =head3 BallotSet
180  
181 Get BallotSet
182  
183 =head3 GetBallots
184  
185 Get just the Ballots from the BallotSet.
186  
187 =head3 PairMatrix
188  
189 Get a Matrix Object for the Active Set. Generated and cached on the first request.
190  
191  
192 =head3 UpdatePairMatrix
193  
194 Regenerate and cache Matrix with current Active Set.
195  
196  
197 =head3 VotesCast
198  
199 Returns the number of votes cast.
200  
201  
202 =head3 VotesActive
203  
204 Returns the number of non-exhausted ballots based on the current Active Set.
205  
206 =head3 VoteValue
207  
208 Sets a VoteValue for use in weighted systems like STV. The default value is 1. Approval and TopCount are aware of VoteValue for RCV ballots.
209  
210 =cut
211  
212 #FOOTER
213  
214 =pod
215  
216 BUG TRACKER
217  
218 L<https://github.com/brainbuz/Vote-Count/issues>
219  
220 AUTHOR
221  
222 John Karr (BRAINBUZ) brainbuz@cpan.org
223  
224 CONTRIBUTORS
225  
226 Copyright 2019 by John Karr (BRAINBUZ) brainbuz@cpan.org.
227  
228 LICENSE
229  
230 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>.
231  
232 =cut
« Count/Start.pm ↑ Summary Count/Log.pm »