X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FDBIx%2FClass%2FDateTime%2FEpoch.pm;h=afb14148816695718a5baa91cffd7a06f3da309a;hb=8e52f1dac7a38f80608ab1af805e70aa9e9dafd1;hp=fe33d0f2072f8e570751055b0b0edd4149315375;hpb=916169687c015cf3be76d2a840f6df92a250c594;p=dbsrgits%2FDBIx-Class-DateTime-Epoch.git diff --git a/lib/DBIx/Class/DateTime/Epoch.pm b/lib/DBIx/Class/DateTime/Epoch.pm index fe33d0f..afb1414 100644 --- a/lib/DBIx/Class/DateTime/Epoch.pm +++ b/lib/DBIx/Class/DateTime/Epoch.pm @@ -1,142 +1,130 @@ -package DBIx::Class::DateTime::Epoch; - -use strict; -use warnings; - -our $VERSION = '0.01'; - -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 - -=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.04'; + +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 columns to/from DateTime objects + +=head1 SYNOPSIS + + package MySchema::Foo; + + use base qw( DBIx::Class ); + + __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) ); + __PACKAGE__->add_columns( + name => { + data_type => 'varchar', + size => 10, + }, + 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, + } + ); + +=head1 DESCRIPTION + +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. + +A column will be recognized as an epoch time given one of the following scenarios: + +=over 4 + +=item * C is an C of some sort and C is also set to a true value + +=item * C is some other value (e.g. C) and C is explicitly set to C. + +=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 + +=head2 _inflate_to_datetime( ) + +Overrides column inflation to use Cfrom_epoch>. + +=head2 _deflate_from_datetime( ) + +Overrides column deflation to call C on the column value. + +=head1 SEE ALSO + +=over 4 + +=item * DBIx::Class + +=item * DBIx::Class::TimeStamp + +=item * DateTime + +=back + +=head1 AUTHORS + +Brian Cassidy Ebricas@cpan.orgE + +Adam Paynter Eadapay@cpan.orgE + +=head1 COPYRIGHT AND LICENSE + +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 +