File Coverage

blib/lib/Resque/Stat.pm
Criterion Covered Total %
statement 3 15 20.0
branch 0 2 0.0
condition 0 6 0.0
subroutine 1 6 16.6
pod 5 5 100.0
total 9 34 26.4


line stmt bran cond sub pod time code
1             package Resque::Stat;
2             # ABSTRACT: The stat subsystem. Used to keep track of integer counts.
3             $Resque::Stat::VERSION = '0.42';
4 9     9   69 use Moose;
  9         23  
  9         85  
5              
6             has resque => (
7             is => 'ro',
8             required => 1,
9             handles => [qw/ redis key /]
10             );
11              
12             sub get {
13 0     0 1   my ($self, $stat) = @_;
14 0 0         $self->redis->get( $self->key( stat => $stat ) ) || 0;
15             }
16              
17             sub set {
18 0     0 1   my ($self, $stat, $value) = @_;
19 0   0       $self->redis->set( $self->key( stat => $stat ), ($value||0)+0 );
20             }
21              
22             sub incr {
23 0     0 1   my ( $self, $stat, $by ) = @_;
24 0   0       $by ||= 1;
25 0           $self->redis->incrby( $self->key( stat => $stat ), $by );
26             }
27              
28             sub decr {
29 0     0 1   my ( $self, $stat, $by ) = @_;
30 0   0       $by ||= 1;
31 0           $self->redis->decrby( $self->key( stat => $stat ), $by );
32             }
33              
34             sub clear {
35 0     0 1   my ( $self, $stat ) = @_;
36 0           $self->redis->del( $self->key( stat => $stat ) );
37             }
38              
39             __PACKAGE__->meta->make_immutable();
40              
41             __END__
42              
43             =pod
44              
45             =encoding UTF-8
46              
47             =head1 NAME
48              
49             Resque::Stat - The stat subsystem. Used to keep track of integer counts.
50              
51             =head1 VERSION
52              
53             version 0.42
54              
55             =head1 ATTRIBUTES
56              
57             =head2 resque
58              
59             =head1 METHODS
60              
61             =head2 get
62              
63             Returns the int value of a stat, given a string stat name.
64              
65             my $value = $resque_stat->get( 'stat_name' );
66              
67             =head2 set
68              
69             Set the int value of a a given stat name.
70              
71             $resque_stat->set( stat_name => 5 );
72              
73             =head2 incr
74              
75             For a string stat name, increments the stat by one.
76              
77             Can optionally accept a second int parameter. The stat is then
78             incremented by that amount.
79              
80             my $value = $resque_stat->incr( 'stat_name', $optional_inc_by );
81              
82             =head2 decr
83              
84             For a string stat name, decrements the stat by one.
85              
86             Can optionally accept a second int parameter. The stat is then
87             decremented by that amount.
88              
89             my $value = $resque_stat->decr( 'stat_name', $optional_dec_by );
90              
91             =head2 clear
92              
93             Removes a stat from Redis, effectively setting it to 0.
94              
95             $resque_stat->clear( 'stat_name' );
96              
97             =head1 AUTHOR
98              
99             Diego Kuperman <diego@freekeylabs.com>
100              
101             =head1 COPYRIGHT AND LICENSE
102              
103             This software is copyright (c) 2021 by Diego Kuperman.
104              
105             This is free software; you can redistribute it and/or modify it under
106             the same terms as the Perl 5 programming language system itself.
107              
108             =cut