Initial Import
Adam Paynter [Wed, 22 Mar 2006 20:01:24 +0000 (20:01 +0000)]
lib/DBIx/Class/DateTime/Epoch.pm [new file with mode: 0644]

diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm
new file mode 100644 (file)
index 0000000..06ce9ce
--- /dev/null
@@ -0,0 +1,53 @@
+package DBIx::Class::DateTime::Epoch;\r
+\r
+use strict;\r
+use warnings;\r
+\r
+use base qw( DBIx::Class );\r
+\r
+use DateTime;\r
+\r
+__PACKAGE__->mk_classdata( ctime_columns => [ ] );\r
+__PACKAGE__->mk_classdata( mtime_columns => [ ] );\r
+\r
+sub register_column {\r
+    my( $class, $col, $info ) = @_;\r
+    $class->next::method( $col, $info );\r
+    \r
+    if( my $type = $info->{ datetime } ) {\r
+        $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime';\r
+        $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime';\r
+        \r
+        $class->inflate_column(\r
+            $col => {\r
+                inflate => sub { DateTime->from_epoch( epoch => shift ) },\r
+                deflate => sub { shift->epoch }\r
+            }\r
+        );\r
+    }\r
+}\r
+\r
+sub insert {\r
+    my $self = shift;\r
+    my $time = time;\r
+    \r
+    for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) {\r
+        next if defined $self->get_column( $column );\r
+        $self->store_column( $column => $time );\r
+    }\r
+\r
+    $self->next::method( @_ );\r
+}\r
+\r
+sub update {\r
+    my $self = shift;\r
+    my $time = time;\r
+    \r
+    for my $column ( @{ $self->mtime_columns } ) {\r
+        $self->set_column( $column => $time );\r
+    }\r
+    \r
+    $self->next::method( @_ );\r
+}\r
+\r
+1;
\ No newline at end of file