switch to module::install, perltidy
[dbsrgits/DBIx-Class-DateTime-Epoch.git] / lib / DBIx / Class / DateTime / Epoch.pm
index 43c8755..f0f74b3 100644 (file)
-package DBIx::Class::DateTime::Epoch;\r
-\r
-use strict;\r
-use warnings;\r
-\r
-our $VERSION = '0.01';\r
-\r
-use base qw( DBIx::Class );\r
-\r
-use DateTime;\r
-\r
-=head1 NAME\r
-\r
-DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based DateTime objects for DBIx::Class\r
-\r
-=head1 SYNOPSIS\r
-\r
-    package foo;\r
-    \r
-    use base qw( DBIx::Class );\r
-    \r
-    __PACKAGE__->load_components( qw( DateTime::Epoch Core ) );\r
-    __PACKAGE__->add_columns(\r
-        name => {\r
-            data_type => 'varchar',\r
-            size      => 10\r
-        },\r
-        bar => {\r
-            data_type => 'bigint',\r
-            epoch     => 1\r
-        },\r
-        creation_time => {\r
-            data_type => 'bigint',\r
-            epoch     => 'ctime'\r
-        },\r
-        modification_time => {\r
-            data_type => 'bigint',\r
-            epoch     => 'mtime'\r
-        }\r
-    );\r
-\r
-=head1 DESCRIPTION\r
-\r
-This module automatically inflates/deflates DateTime objects\r
-corresponding to applicable columns. Columns may also be\r
-defined to specify their nature, such as columns representing a\r
-creation time (set at time of insertion) or a modification time\r
-(set at time of every update).\r
-\r
-=head1 METHODS\r
-\r
-=head2 register_column\r
-\r
-This method will automatically add inflation and deflation rules\r
-to a column if an epoch value has been set in the column's definition.\r
-If the epoch value is 'ctime' (creation time) or 'mtime'\r
-(modification time), it will be registered as such for later\r
-use by the insert and the update methods.\r
-\r
-=head2 insert\r
-\r
-This method will set the value of all registered creation time\r
-columns to the current time. No changes will be made to a column\r
-whose value has already been set.\r
-\r
-=head2 update\r
-\r
-This method will set the value of all registered modification time\r
-columns to the current time. This will overwrite a column's value,\r
-even if it has been already set.\r
-\r
-=head1 SEE ALSO\r
-\r
-=over 4\r
-\r
-=item * DateTime\r
-\r
-=item * DBIx::Class\r
-\r
-=back\r
-\r
-=head1 AUTHOR\r
-\r
-=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
-\r
-=back\r
-\r
-=head1 COPYRIGHT AND LICENSE\r
-\r
-Copyright 2006 by Brian Cassidy\r
-\r
-This library is free software; you can redistribute it and/or modify\r
-it under the same terms as Perl itself. \r
-\r
-=cut\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->{ epoch } ) {\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
+package DBIx::Class::DateTime::Epoch;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.04';
+
+use base qw( DBIx::Class );
+
+use DateTime;
+
+=head1 NAME
+
+DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based DateTime objects for DBIx::Class
+
+=head1 SYNOPSIS
+
+    package foo;
+    
+    use base qw( DBIx::Class );
+    
+    __PACKAGE__->load_components( qw( DateTime::Epoch Core ) );
+    __PACKAGE__->add_columns(
+        name => {
+            data_type => 'varchar',
+            size      => 10
+        },
+        bar => {
+            data_type => 'bigint',
+            epoch     => 1
+        },
+        creation_time => {
+            data_type => 'bigint',
+            epoch     => 'ctime'
+        },
+        modification_time => {
+            data_type => 'bigint',
+            epoch     => 'mtime'
+        }
+    );
+
+=head1 DESCRIPTION
+
+This module automatically inflates/deflates DateTime objects
+corresponding to applicable columns. Columns may also be
+defined to specify their nature, such as columns representing a
+creation time (set at time of insertion) or a modification time
+(set at time of every update).
+
+=head1 METHODS
+
+=head2 register_column
+
+This method will automatically add inflation and deflation rules
+to a column if an epoch value has been set in the column's definition.
+If the epoch value is 'ctime' (creation time) or 'mtime'
+(modification time), it will be registered as such for later
+use by the insert and the update methods.
+
+=head2 insert
+
+This method will set the value of all registered creation time
+columns to the current time. No changes will be made to a column
+whose value has already been set.
+
+=head2 update
+
+This method will set the value of all registered modification time
+columns to the current time. This will overwrite a column's value,
+even if it has been already set.
+
+=head1 SEE ALSO
+
+=over 4
+
+=item * DateTime
+
+=item * DBIx::Class
+
+=back
+
+=head1 AUTHORS
+
+Brian Cassidy E<lt>bricas@cpan.orgE<gt>
+
+Adam Paynter E<lt>adapay@cpan.orgE<gt>
+
+=head1 COPYRIGHT AND LICENSE
+
+Copyright 2007 by Brian Cassidy
+
+This library is free software; you can redistribute it and/or modify
+it under the same terms as Perl itself. 
+
+=cut
+
+__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->{ epoch } ) {
+        $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;
+    my %dirty = $self->get_dirty_columns;
+
+    for my $column ( @{ $self->mtime_columns } ) {
+        next if exists $dirty{ $column };
+        $self->set_column( $column => $time );
+    }
+
+    $self->next::method( @_ );
+}
+
+1;