Algorithm-Voting
view release on metacpan or search on metacpan
lib/Algorithm/Voting/Ballot.pm view on Meta::CPAN
# $Id: Ballot.pm 60 2008-09-02 12:11:49Z johntrammell $
# $URL: https://algorithm-voting.googlecode.com/svn/tags/rel-0.01-1/lib/Algorithm/Voting/Ballot.pm $
package Algorithm::Voting::Ballot;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
use Params::Validate 'validate';
__PACKAGE__->mk_accessors(qw/ candidate /);
=pod
=head1 NAME
Algorithm::Voting::Ballot - represents a ballot to cast in a race
=head1 SYNOPSIS
lib/Algorithm/Voting/Ballot.pm view on Meta::CPAN
=cut
sub new {
my $class = shift;
if (@_ == 1) {
return $class->new(candidate => $_[0]);
}
my %valid = (
candidate => 0,
);
my %args = validate(@_, \%valid);
return bless \%args, $class;
}
=head2 $ballot->candidate()
Returns a scalar (presumably a string, although this is not enforced)
containing the candidate for whom this ballot is cast.
=cut
lib/Algorithm/Voting/Plurality.pm view on Meta::CPAN
# $Id: Plurality.pm 60 2008-09-02 12:11:49Z johntrammell $
# $URL: https://algorithm-voting.googlecode.com/svn/tags/rel-0.01-1/lib/Algorithm/Voting/Plurality.pm $
package Algorithm::Voting::Plurality;
use strict;
use warnings;
use base 'Class::Accessor::Fast';
use List::Util 'sum';
use Params::Validate qw/ validate validate_pos ARRAYREF /;
__PACKAGE__->mk_accessors(qw/ tally /);
=pod
=head1 NAME
Algorithm::Voting::Plurality - use "Plurality" to decide the sole winner
=head1 SYNOPSIS
lib/Algorithm/Voting/Plurality.pm view on Meta::CPAN
my @c = qw( John Barack Ralph );
my $box = Algorithm::Voting::Plurality->new(candidates => \@c);
=cut
sub new {
my $class = shift;
my %valid = (
candidates => { type => ARRAYREF, optional => 1 },
);
my %args = validate(@_, \%valid);
my $self = bless \%args, $class;
$self->tally({});
return $self;
}
=head2 $box->candidates
Returns a list containing the candidate names used in the construction of the
ballot box. If no candidates were specified at construction of the box, the
empty list is returned.
lib/Algorithm/Voting/Plurality.pm view on Meta::CPAN
=head2 $box->add($ballot)
Add C<$ballot> to the box. C<$ballot> can be any object that we can call
method C<candidate()> on.
=cut
sub add {
my $self = shift;
my %valid = ( can => [ 'candidate' ], );
my ($ballot) = validate_pos(@_, \%valid);
$self->validate_ballot($ballot);
$self->increment_tally($ballot->candidate);
return $self->count;
}
=head2 $box->increment_tally($candidate)
Increments the tally for C<$candidate> by 1.
=cut
sub increment_tally {
my ($self, $candidate) = @_;
$self->tally->{$candidate} += 1;
return $self->tally->{$candidate};
}
=head2 $box->validate_ballot($ballot)
If this election is limited to a specific list of candidates, this method will
C<die()> if the candidate on C<$ballot> is not one of them.
=cut
sub validate_ballot {
my ($self, $ballot) = @_;
# if this ballot box has a list of "valid" candidates, verify that the
# candidate on this ballot is one of them.
if ($self->candidates) {
unless (grep { $_ eq $ballot->candidate } $self->candidates) {
die "Invalid ballot: candidate '@{[ $ballot->candidate ]}'",
" is not on the candidate list";
}
}
}
lib/Algorithm/Voting/Sortition.pm view on Meta::CPAN
# $Id: Sortition.pm 60 2008-09-02 12:11:49Z johntrammell $
# $URL: https://algorithm-voting.googlecode.com/svn/tags/rel-0.01-1/lib/Algorithm/Voting/Sortition.pm $
package Algorithm::Voting::Sortition;
use strict;
use warnings;
use Scalar::Util qw/reftype looks_like_number/;
use Digest::MD5;
use Math::BigInt;
use Params::Validate 'validate';
use base 'Class::Accessor::Fast';
=pod
=head1 NAME
Algorithm::Voting::Sortition - implements RFC 3797, "Publicly Verifiable
Nominations Committee (NomCom) Random Selection"
=head1 SYNOPSIS
lib/Algorithm/Voting/Sortition.pm view on Meta::CPAN
=cut
sub new {
my $class = shift;
my %valid = (
candidates => 1,
n => { default => -1 },
source => 0,
keystring => 0,
);
my %args = validate(@_, \%valid);
return bless \%args, $class;
}
=head2 $obj->candidates
Returns a list containing the current candidates.
=cut
sub candidates {
( run in 0.495 second using v1.01-cache-2.11-cpan-a5abf4f5562 )