This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Votes.pm
63 lines (51 loc) · 1.42 KB
/
Votes.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package MetaCPAN::Contest::Vote::Votes;
use Moose;
use Try::Tiny;
use JSON;
use MooseX::Types::Path::Class 'Dir';
use MooseX::Types::Common::Numeric 'PositiveInt';
use namespace::autoclean;
has vote_storage => (
is => 'ro',
required => 1,
isa => Dir,
coerce => 1,
);
sub BUILD {
my ($self) = @_;
confess "storage directory '@{[ $self->vote_storage ]}' isn't writable"
unless -w $self->vote_storage;
}
sub vote_for {
my ($self, $user) = @_;
$self->vote_storage->file($user->{id} . '.json');
}
sub has_voted {
my ($self, $user) = @_;
-e $self->vote_for($user);
}
sub submit_ballot {
my ($self, $user, $ballot) = @_;
# this is probably a bit racy, but it seems like the worst that could happen
# is one overwriting his own vote, which doesn't seem too bad
die "broken github user\n"
if !exists $user->{id} || !PositiveInt->check($user->{id});
die "you already voted\n"
if $self->has_voted($user);
my $vote_file = $self->vote_for($user);
try {
my $fh = $vote_file->openw;
print { $fh } encode_json {
time => time,
user => $user,
ballot => $ballot,
} or die "failed to write ballot: $!";
close $fh or die "failed to flush ballot: $!";
}
catch {
$vote_file->remove;
die "Failed to register vote: $_\n";
};
}
__PACKAGE__->meta->make_immutable;
1;