From: Brian Cassidy Date: Tue, 12 May 2009 19:39:07 +0000 (-0300) Subject: refactor module to itegrate with InflateColumn::DateTime and TimeStamp X-Git-Tag: 0.04~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8e52f1dac7a38f80608ab1af805e70aa9e9dafd1;p=dbsrgits%2FDBIx-Class-DateTime-Epoch.git refactor module to itegrate with InflateColumn::DateTime and TimeStamp --- diff --git a/Changes b/Changes index 9439089..c9e191a 100644 --- a/Changes +++ b/Changes @@ -1,8 +1,12 @@ Revision history for Perl extension DBIx::Class::DateTime::Epoch. -0.04 Tue Oct 30 2007 +0.04 XXX - switch to Module::Install + [ THINGS THAT WILL BREAK YOUR CODE ] + - refactor module to itegrate with InflateColumn::DateTime and TimeStamp. + Requires DBIx::Class 0.08103. Please read the new documentation. + 0.03 Wed Apr 25 2007 - don't override user-supplied values for update() diff --git a/Makefile.PL b/Makefile.PL index 8cc7369..07e9d69 100644 --- a/Makefile.PL +++ b/Makefile.PL @@ -7,10 +7,12 @@ if( -e 'MANIFEST.SKIP' ) { name 'DBIx-Class-DateTime-Epoch'; all_from 'lib/DBIx/Class/DateTime/Epoch.pm'; -requires 'DBIx::Class' => '0.08103'; +requires 'DBIx::Class' => '0.08103'; # InflateColumn::DateTime overriding possible requires 'DateTime'; test_requires 'Test::More'; +test_requires 'DBIx::Class::TimeStamp' => '0.07'; # removes data_type checking +test_requires 'DBICx::TestDatabase'; repository 'http://github.com/bricas/dbix-class-datetime-epoch'; diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm index f0f74b3..afb1414 100644 --- a/lib/DBIx/Class/DateTime/Epoch.pm +++ b/lib/DBIx/Class/DateTime/Epoch.pm @@ -9,74 +9,108 @@ use base qw( DBIx::Class ); use DateTime; +__PACKAGE__->load_components( qw/InflateColumn::DateTime/ ); + +sub _inflate_to_datetime { + my( $self, $value, $info, @rest ) = @_; + $self->next::method( $value, $info, @rest ) + unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch'; + + return DateTime->from_epoch( epoch => $value ); +} + +sub _deflate_from_datetime { + my( $self, $value, $info, @rest ) = @_; + $self->next::method( $value, $info, @rest ) + unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch'; + + return $value->epoch; +} + +1; + +__END__ + =head1 NAME -DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based DateTime objects for DBIx::Class +DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects =head1 SYNOPSIS - package foo; + package MySchema::Foo; use base qw( DBIx::Class ); - __PACKAGE__->load_components( qw( DateTime::Epoch Core ) ); + __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) ); __PACKAGE__->add_columns( name => { data_type => 'varchar', - size => 10 + size => 10, + }, + bar => { # epoch stored as an int + data_type => 'bigint', + inflate_datetime => 1, }, - bar => { - data_type => 'bigint', - epoch => 1 + baz => { # epoch stored as a string + data_type => 'varchar', + size => 50, + inflate_datetime => 'epoch', }, + # working in conjunction with DBIx::Class::TimeStamp creation_time => { - data_type => 'bigint', - epoch => 'ctime' + data_type => 'bigint', + inflate_datetime => 1, + set_on_create => 1, }, modification_time => { - data_type => 'bigint', - epoch => 'mtime' + data_type => 'bigint', + inflate_datetime => 1, + set_on_create => 1, + set_on_update => 1, } ); =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). +This module automatically inflates/deflates DateTime objects from/to epoch +values for the specified columns. This module is essentially an extension to +L so all of the settings, including +C and C, are also valid. -=head1 METHODS +A column will be recognized as an epoch time given one of the following scenarios: + +=over 4 -=head2 register_column +=item * C is an C of some sort and C is also set to a true value -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. +=item * C is some other value (e.g. C) and C is explicitly set to C. -=head2 insert +=back + +L can also be used in conjunction with this module to support +epoch-based columns that are automatically set on creation of a row and updated subsequent +modifications. + +=head1 METHODS -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 _inflate_to_datetime( ) -=head2 update +Overrides column inflation to use Cfrom_epoch>. -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. +=head2 _deflate_from_datetime( ) + +Overrides column deflation to call C on the column value. =head1 SEE ALSO =over 4 -=item * DateTime - =item * DBIx::Class +=item * DBIx::Class::TimeStamp + +=item * DateTime + =back =head1 AUTHORS @@ -87,58 +121,10 @@ Adam Paynter Eadapay@cpan.orgE =head1 COPYRIGHT AND LICENSE -Copyright 2007 by Brian Cassidy +Copyright 2006-2009 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/02-schema.t b/t/02-schema.t new file mode 100644 index 0000000..66f3664 --- /dev/null +++ b/t/02-schema.t @@ -0,0 +1,53 @@ +use strict; +use warnings; + +use lib 't/lib'; + +use Test::More tests => 15; + +use DBICx::TestDatabase; +use DateTime; +my $schema = DBICx::TestDatabase->new( 'MySchema' ); + +my $rs = $schema->resultset( 'Foo' ); + +{ + my $now = DateTime->now; + my $epoch = $now->epoch; + + $schema->populate( 'Foo', [ [ qw( id bar baz creation_time modification_time ) ], [ 1, ( $epoch ) x 4 ] ] ); + + { + my $row = $rs->find( 1 ); + + isa_ok( $row->bar, 'DateTime' ); + isa_ok( $row->baz, 'DateTime' ); + ok( $row->bar == $now, 'inflate: epoch as int' ); + ok( $row->baz == $now, 'inflate: epoch as varchar' ); + } + + { + $rs->create( { bar => $now, baz => $now } ); + my $row = $rs->find( 2 ); + + isa_ok( $row->bar, 'DateTime' ); + isa_ok( $row->baz, 'DateTime' ); + is( $row->get_column( 'bar' ), $epoch, 'deflate: epoch as int' ); + is( $row->get_column( 'baz' ), $epoch, 'deflate: epoch as varchar' ); + + # courtesy of TimeStamp + isa_ok( $row->creation_time, 'DateTime' ); # courtesy of TimeStamp + isa_ok( $row->modification_time, 'DateTime' ); + like( $row->get_column( 'creation_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + + my $mtime = $row->modification_time; + sleep( 1 ); + $row->update( { name => 'test' } ); + + $row = $rs->find( 2 ); + isa_ok( $row->modification_time, 'DateTime' ); + like( $row->get_column( 'modification_time' ), qr/^\d+$/, 'TimeStamp as epoch' ); + ok( $row->modification_time > $mtime, 'mtime column was updated' ); + } +} diff --git a/t/lib/MySchema.pm b/t/lib/MySchema.pm new file mode 100644 index 0000000..9abbaa2 --- /dev/null +++ b/t/lib/MySchema.pm @@ -0,0 +1,10 @@ +package MySchema; + +use strict; +use warnings; + +use base qw( DBIx::Class::Schema ); + +__PACKAGE__->load_classes; + +1; diff --git a/t/lib/MySchema/Foo.pm b/t/lib/MySchema/Foo.pm new file mode 100644 index 0000000..ca4f5ba --- /dev/null +++ b/t/lib/MySchema/Foo.pm @@ -0,0 +1,46 @@ +package MySchema::Foo; + +use strict; +use warnings; + +use base qw( DBIx::Class ); + +__PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) ); +__PACKAGE__->table( 'foo' ); +__PACKAGE__->add_columns( + id => { + data_type => 'bigint', + is_auto_increment => 1, + is_nullable => 0, + }, + name => { + data_type => 'varchar', + size => 10, + is_nullable => 1, + }, + bar => { # epoch stored as an int + data_type => 'bigint', + inflate_datetime => 1, + }, + baz => { # epoch stored as a string + data_type => 'varchar', + size => 50, + inflate_datetime => 'epoch', + }, + # working in conjunction with DBIx::Class::TimeStamp + creation_time => { + data_type => 'bigint', + inflate_datetime => 1, + set_on_create => 1, + }, + modification_time => { + data_type => 'bigint', + inflate_datetime => 1, + set_on_create => 1, + set_on_update => 1, + } +); + +__PACKAGE__->set_primary_key( 'id' ); + +1;