1 package DBIx::Class::DateTime::Epoch;
\r
6 our $VERSION = '0.01';
\r
8 use base qw( DBIx::Class );
\r
14 DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based DateTime objects for DBIx::Class
\r
20 use base qw( DBIx::Class );
\r
22 __PACKAGE__->load_components( qw( DateTime::Epoch Core ) );
\r
23 __PACKAGE__->add_columns(
\r
25 data_type => 'varchar',
\r
29 data_type => 'bigint',
\r
33 data_type => 'bigint',
\r
36 modification_time => {
\r
37 data_type => 'bigint',
\r
44 This module automatically inflates/deflates DateTime objects
\r
45 corresponding to applicable columns. Columns may also be
\r
46 defined to specify their nature, such as columns representing a
\r
47 creation time (set at time of insertion) or a modification time
\r
48 (set at time of every update).
\r
52 =head2 register_column
\r
54 This method will automatically add inflation and deflation rules
\r
55 to a column if an epoch value has been set in the column's definition.
\r
56 If the epoch value is 'ctime' (creation time) or 'mtime'
\r
57 (modification time), it will be registered as such for later
\r
58 use by the insert and the update methods.
\r
62 This method will set the value of all registered creation time
\r
63 columns to the current time. No changes will be made to a column
\r
64 whose value has already been set.
\r
68 This method will set the value of all registered modification time
\r
69 columns to the current time. This will overwrite a column's value,
\r
70 even if it has been already set.
\r
86 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
\r
90 =head1 COPYRIGHT AND LICENSE
\r
92 Copyright 2006 by Brian Cassidy
\r
94 This library is free software; you can redistribute it and/or modify
\r
95 it under the same terms as Perl itself.
\r
99 __PACKAGE__->mk_classdata( ctime_columns => [ ] );
\r
100 __PACKAGE__->mk_classdata( mtime_columns => [ ] );
\r
102 sub register_column {
\r
103 my( $class, $col, $info ) = @_;
\r
104 $class->next::method( $col, $info );
\r
106 if( my $type = $info->{ epoch } ) {
\r
107 $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime';
\r
108 $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime';
\r
110 $class->inflate_column(
\r
112 inflate => sub { DateTime->from_epoch( epoch => shift ) },
\r
113 deflate => sub { shift->epoch }
\r
123 for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) {
\r
124 next if defined $self->get_column( $column );
\r
125 $self->store_column( $column => $time );
\r
128 $self->next::method( @_ );
\r
135 for my $column ( @{ $self->mtime_columns } ) {
\r
136 $self->set_column( $column => $time );
\r
139 $self->next::method( @_ );
\r