package Email::Archive;
use Moose;
-use Module::Load;
+use Email::Archive::Storage::DBI;
has storage => (
is => 'rw',
retrieve
search
/],
- lazy_build => 1,
+ lazy => 1,
+ default => sub { Email::Archive::Storage::DBI->new }
);
-has dsn => (
- is => 'ro',
- isa => 'Str',
- required => 1,
-);
-
-has storage_class => (
- is => 'ro',
- isa => 'Str',
- required => 1,
- default => 'Email::Archive::Storage::DBI',
-);
-
-sub _build_storage {
- my ($self) = @_;
- load $self->storage_class;
- my $storage = $self->storage_class->new(dsn => $self->dsn);
-}
-
1;
package Email::Archive::Storage::DBI;
use Moose;
+use Carp;
use DBI;
use File::ShareDir 'module_file';
use File::Slurp 'read_file';
use autodie;
with q/Email::Archive::Storage/;
-has dsn => (
- is => 'ro',
- isa => 'Str',
- required => 1,
-);
-
has sqla => (
is => 'ro',
isa => 'SQL::Abstract',
default => 0,
);
-my $SCHEMA_VERSION = 0;
+
+my $SCHEMA_VERSION = 1;
sub store {
my ($self, $email) = @_;
$self->do($sql);
}
-sub BUILD {
+sub _deployed {
my ($self) = @_;
- $self->dbh(DBI->connect($self->dsn));
+ my $schema_version = eval { $self->selectcol_array('SELECT schema_version FROM metadata') };
+ if(defined $schema_version and $schema_version =~ /^\d+$/) {
+ $self->deployed_schema_version($schema_version);
+ return $schema_version =~ /^\d+$/;
+ }
+}
+
+sub db_connect {
+ my ($self, $dsn) = @_;
+ $self->dbh(DBI->connect($dsn));
if(!$self->_deployed) {
$self->_deploy;
}
}
}
-sub _deployed {
- my ($self) = @_;
- my $schema_version = eval { $self->selectcol_array('SELECT schema_version FROM metadata') };
- if(defined $schema_version and $schema_version =~ /^\d+$/) {
- $self->deployed_schema_version($schema_version);
- return $schema_version =~ /^\d+$/;
- }
-}
-
1;
use Email::Simple;
use Email::Simple::Creator;
-use lib 'lib';
use Email::Archive;
my $email = Email::Simple->create(
header => [
- From => 'casey@geeknest.com',
+ From => 'foo@example.com',
To => 'drain@example.com',
Subject => 'Message in a bottle',
'Message-ID' => 'helloworld',
body => 'hello there!'
);
-my $e = Email::Archive->new({dsn => 'dbi:SQLite:dbname=test.db'});
+my $e = Email::Archive->new();
+$e->storage->db_connect('dbi:SQLite:dbname=test.db');
print "sending @{[$email->as_string]}\n";
$e->store($email);