File Coverage

blib/lib/Email/AutoReply/DB/BerkeleyDB.pm
Criterion Covered Total %
statement 19 21 90.4
branch n/a
condition n/a
subroutine 7 7 100.0
pod n/a
total 26 28 92.8


line stmt bran cond sub pod time code
1             package Email::AutoReply::DB::BerkeleyDB;
2             our $rcsid = '$Id: BerkeleyDB.pm 3002 2008-06-05 20:23:24Z adam $';
3              
4 1     1   5 use strict;
  1         1  
  1         39  
5 1     1   5 use warnings;
  1         1  
  1         30  
6              
7 1     1   394 use Email::AutoReply::DB '-Base';
  1         3  
  1         10  
8 1     1   649 use Email::AutoReply::Recipient;
  1     1   2  
  1     1   29  
  1         5  
  1         1  
  1         25  
  1         658  
  1         4  
  1         11  
9 1     1   837 use BerkeleyDB;
  0            
  0            
10             use Carp qw(confess);
11              
12             field 'email_autoreply_settings_dir';
13             field 'cachedb_file' => "replied_cache.db";
14             field 'cachedb_path'; # a path, not ending in a path separator
15             field '_db'; # the reference to the actual tied hash
16              
17             sub new {
18             $self = super;
19             $self->_check_path_available;
20             $self->_init_db;
21             return $self;
22             }
23              
24             sub _check_path_available {
25             my $dir = $self->email_autoreply_settings_dir;
26             defined($dir) or confess "must pass in email_autoreply_settings_dir";
27             $self->cachedb_path($self->email_autoreply_settings_dir);
28             }
29              
30             sub _init_db {
31             my %autoreply_cache;
32             my $filename = $self->cachedb_path . '/' . $self->cachedb_file;
33             tie %autoreply_cache, 'BerkeleyDB::Hash',
34             -Filename => $filename,
35             -Flags => DB_CREATE|DB_INIT_LOCK,
36             or die "Cannot open file $filename: $! $BerkeleyDB::Error\n";
37             $self->_db(\%autoreply_cache);
38             }
39              
40             sub store {
41             my $input_type = 'Email::AutoReply::Recipient';
42             ref $_[0] eq $input_type or confess "input object must be an $input_type";
43             $_[0]->email && $_[0]->timestamp or confess "invalid input";
44             $self->_db->{$_[0]->email} = $_[0]->timestamp;
45             }
46              
47             # INPUT: string to search for
48             # OUTPUT: Email::AutoReply::Recipient object, or zero
49             sub fetch {
50             my $timestamp = $self->_db->{$_[0]};
51             my $rv = 0;
52             if ($timestamp) {
53             $rv = Email::AutoReply::Recipient->new(
54             email => $_[0], timestamp => $timestamp
55             );
56             }
57             return $rv;
58             }
59              
60             # INPUT:
61             # OUTPUT: list of Email::AutoReply::Recipient objects, or an empty list
62             sub fetch_all {
63             return keys %{ $self->_db };
64             }
65              
66             return 1;
67             __END__