From: Adam Paynter Date: Wed, 22 Mar 2006 20:01:24 +0000 (+0000) Subject: Initial Import X-Git-Tag: 0.01~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DateTime-Epoch.git;a=commitdiff_plain;h=77f8d0c0c5582e32234c473f213534b5ca50d287 Initial Import --- diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm new file mode 100644 index 0000000..06ce9ce --- /dev/null +++ b/lib/DBIx/Class/DateTime/Epoch.pm @@ -0,0 +1,53 @@ +package DBIx::Class::DateTime::Epoch; + +use strict; +use warnings; + +use base qw( DBIx::Class ); + +use DateTime; + +__PACKAGE__->mk_classdata( ctime_columns => [ ] ); +__PACKAGE__->mk_classdata( mtime_columns => [ ] ); + +sub register_column { + my( $class, $col, $info ) = @_; + $class->next::method( $col, $info ); + + if( my $type = $info->{ datetime } ) { + $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime'; + $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime'; + + $class->inflate_column( + $col => { + inflate => sub { DateTime->from_epoch( epoch => shift ) }, + deflate => sub { shift->epoch } + } + ); + } +} + +sub insert { + my $self = shift; + my $time = time; + + for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) { + next if defined $self->get_column( $column ); + $self->store_column( $column => $time ); + } + + $self->next::method( @_ ); +} + +sub update { + my $self = shift; + my $time = time; + + for my $column ( @{ $self->mtime_columns } ) { + $self->set_column( $column => $time ); + } + + $self->next::method( @_ ); +} + +1; \ No newline at end of file