From: Chris Nehren Date: Fri, 25 Jun 2010 20:09:54 +0000 (-0400) Subject: beginning of redesign X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c5f5125c9dc29dbbf1d42ccadc17e9c6215fd8b3;p=p5sagit%2FEmail-Archive.git beginning of redesign --- diff --git a/lib/Email/Archive.pm b/lib/Email/Archive.pm index 83d86d1..a27196f 100644 --- a/lib/Email/Archive.pm +++ b/lib/Email/Archive.pm @@ -1,6 +1,6 @@ package Email::Archive; use Moose; -use Module::Load; +use Email::Archive::Storage::DBI; has storage => ( is => 'rw', @@ -10,26 +10,8 @@ has storage => ( 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; diff --git a/lib/Email/Archive/Storage/DBI.pm b/lib/Email/Archive/Storage/DBI.pm index 2017b10..72546fd 100644 --- a/lib/Email/Archive/Storage/DBI.pm +++ b/lib/Email/Archive/Storage/DBI.pm @@ -1,5 +1,6 @@ package Email::Archive::Storage::DBI; use Moose; +use Carp; use DBI; use File::ShareDir 'module_file'; use File::Slurp 'read_file'; @@ -9,12 +10,6 @@ use SQL::Abstract; use autodie; with q/Email::Archive::Storage/; -has dsn => ( - is => 'ro', - isa => 'Str', - required => 1, -); - has sqla => ( is => 'ro', isa => 'SQL::Abstract', @@ -41,7 +36,8 @@ has deployed_schema_version => ( default => 0, ); -my $SCHEMA_VERSION = 0; + +my $SCHEMA_VERSION = 1; sub store { my ($self, $email) = @_; @@ -88,9 +84,18 @@ sub _deploy { $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; } @@ -101,13 +106,4 @@ sub BUILD { } } -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; diff --git a/test_run b/test_run index 0174bf4..8385f7f 100644 --- a/test_run +++ b/test_run @@ -4,12 +4,11 @@ use warnings; 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', @@ -17,7 +16,8 @@ my $email = Email::Simple->create( 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);