From: Brian Cassidy Date: Wed, 25 Apr 2007 17:00:55 +0000 (+0000) Subject: line endings, tabs, version bump, leave user-supplied values for update() X-Git-Tag: 0.03~1 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=dbsrgits%2FDBIx-Class-DateTime-Epoch.git;a=commitdiff_plain;h=e4480da23e270a6f93b8fe3f9f66fdd565d14467 line endings, tabs, version bump, leave user-supplied values for update() --- diff --git a/Build.PL b/Build.PL index 2ad85b2..86daf7d 100644 --- a/Build.PL +++ b/Build.PL @@ -1,20 +1,20 @@ -use strict; - -use Module::Build; - -my $build = Module::Build->new( - module_name => 'DBIx::Class::DateTime::Epoch', - dist_author => 'Brian Cassidy ', - license => 'perl', - create_readme => 1, - create_makefile_pl => 'traditional', - requires => { - 'DateTime' => 0, - 'DBIx::Class' => 0 - }, - build_requires => { - 'Test::More' => 0 - }, -); - -$build->create_build_script; +use strict; + +use Module::Build; + +my $build = Module::Build->new( + module_name => 'DBIx::Class::DateTime::Epoch', + dist_author => 'Brian Cassidy ', + license => 'perl', + create_readme => 1, + create_makefile_pl => 'traditional', + requires => { + 'DateTime' => 0, + 'DBIx::Class' => 0 + }, + build_requires => { + 'Test::More' => 0 + }, +); + +$build->create_build_script; diff --git a/Changes b/Changes index 50a7dd8..bda906c 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,10 @@ -Revision history for Perl extension DBIx::Class::DateTime::Epoch. - -0.02 Mon Jul 17 2006 - - Re-release onto CPAN - -0.01 Wed Mar 22 2006 - - original version; \ No newline at end of file +Revision history for Perl extension DBIx::Class::DateTime::Epoch. + +0.03 Wed Apr 25 2007 + - don't override user-supplied values for update() + +0.02 Mon Jul 17 2006 + - Re-release onto CPAN + +0.01 Wed Mar 22 2006 + - original version; diff --git a/MANIFEST.SKIP b/MANIFEST.SKIP new file mode 100644 index 0000000..f21a21f --- /dev/null +++ b/MANIFEST.SKIP @@ -0,0 +1,29 @@ +# Avoid version control files. +\bRCS\b +\bCVS\b +,v$ +\B\.svn\b + +# Avoid Makemaker generated and utility files. +\bMakefile$ +\bblib +\bMakeMaker-\d +\bpm_to_blib$ +\bblibdirs$ +^MANIFEST\.SKIP$ + +# Avoid Module::Build generated and utility files. +\bBuild$ +\b_build + +# Avoid temp and backup files. +~$ +\.tmp$ +\.old$ +\.bak$ +\#$ +\b\.# +\.DS_Store$ + +# No tarballs! +\.gz$ diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm index e18e3d4..395fe94 100644 --- a/lib/DBIx/Class/DateTime/Epoch.pm +++ b/lib/DBIx/Class/DateTime/Epoch.pm @@ -1,144 +1,146 @@ -package DBIx::Class::DateTime::Epoch; - -use strict; -use warnings; - -our $VERSION = '0.02'; - -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 AUTHOR - -=over 4 - -=item * Brian Cassidy Ebricas@cpan.orgE - -=item * Adam Paynter Eadapay@cpan.orgE - -=back - -=head1 COPYRIGHT AND LICENSE - -Copyright 2006 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; - - for my $column ( @{ $self->mtime_columns } ) { - $self->set_column( $column => $time ); - } - - $self->next::method( @_ ); -} - -1; \ No newline at end of file +package DBIx::Class::DateTime::Epoch; + +use strict; +use warnings; + +our $VERSION = '0.03'; + +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 AUTHOR + +=over 4 + +=item * Brian Cassidy Ebricas@cpan.orgE + +=item * Adam Paynter Eadapay@cpan.orgE + +=back + +=head1 COPYRIGHT AND LICENSE + +Copyright 2006 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; diff --git a/t/01-use.t b/t/01-use.t index c0f7dba..f4dd83c 100644 --- a/t/01-use.t +++ b/t/01-use.t @@ -1,5 +1,5 @@ -use Test::More tests => 1; - -BEGIN { - use_ok( 'DBIx::Class::DateTime::Epoch' ); -} +use Test::More tests => 1; + +BEGIN { + use_ok( 'DBIx::Class::DateTime::Epoch' ); +} diff --git a/t/98-pod_coverage.t b/t/98-pod_coverage.t index 73a83b0..2c5ca56 100644 --- a/t/98-pod_coverage.t +++ b/t/98-pod_coverage.t @@ -1,4 +1,4 @@ -use Test::More; -eval "use Test::Pod::Coverage 1.00"; -plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; -all_pod_coverage_ok(); \ No newline at end of file +use Test::More; +eval "use Test::Pod::Coverage 1.00"; +plan skip_all => "Test::Pod::Coverage 1.00 required for testing POD coverage" if $@; +all_pod_coverage_ok(); diff --git a/t/99-pod.t b/t/99-pod.t index 3afe2fa..437887a 100644 --- a/t/99-pod.t +++ b/t/99-pod.t @@ -1,4 +1,4 @@ -use Test::More; -eval "use Test::Pod 1.00"; -plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; -all_pod_files_ok(); \ No newline at end of file +use Test::More; +eval "use Test::Pod 1.00"; +plan skip_all => "Test::Pod 1.00 required for testing POD" if $@; +all_pod_files_ok();