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