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.
86 =item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>
88 =item * Adam Paynter E<lt>adapay@cpan.orgE<gt>
92 =head1 COPYRIGHT AND LICENSE
94 Copyright 2006 by Brian Cassidy
96 This library is free software; you can redistribute it and/or modify
97 it under the same terms as Perl itself.
101 __PACKAGE__->mk_classdata( ctime_columns => [ ] );
102 __PACKAGE__->mk_classdata( mtime_columns => [ ] );
104 sub register_column {
105 my( $class, $col, $info ) = @_;
106 $class->next::method( $col, $info );
108 if( my $type = $info->{ epoch } ) {
109 $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime';
110 $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime';
112 $class->inflate_column(
114 inflate => sub { DateTime->from_epoch( epoch => shift ) },
115 deflate => sub { shift->epoch }
125 for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) {
126 next if defined $self->get_column( $column );
127 $self->store_column( $column => $time );
130 $self->next::method( @_ );
136 my %dirty = $self->get_dirty_columns;
138 for my $column ( @{ $self->mtime_columns } ) {
139 next if exists $dirty{ $column };
140 $self->set_column( $column => $time );
143 $self->next::method( @_ );