oops
[dbsrgits/DBIx-Class-DateTime-Epoch.git] / lib / DBIx / Class / DateTime / Epoch.pm
CommitLineData
77f8d0c0 1package DBIx::Class::DateTime::Epoch;\r
2\r
3use strict;\r
4use warnings;\r
5\r
c553ccdb 6our $VERSION = '0.02';\r
ab93b294 7\r
77f8d0c0 8use base qw( DBIx::Class );\r
9\r
10use DateTime;\r
11\r
ab93b294 12=head1 NAME\r
13\r
14DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based DateTime objects for DBIx::Class\r
15\r
16=head1 SYNOPSIS\r
17\r
18 package foo;\r
19 \r
20 use base qw( DBIx::Class );\r
21 \r
22 __PACKAGE__->load_components( qw( DateTime::Epoch Core ) );\r
23 __PACKAGE__->add_columns(\r
24 name => {\r
25 data_type => 'varchar',\r
26 size => 10\r
27 },\r
28 bar => {\r
29 data_type => 'bigint',\r
7f67d2d5 30 epoch => 1\r
ab93b294 31 },\r
32 creation_time => {\r
33 data_type => 'bigint',\r
7f67d2d5 34 epoch => 'ctime'\r
ab93b294 35 },\r
36 modification_time => {\r
37 data_type => 'bigint',\r
7f67d2d5 38 epoch => 'mtime'\r
ab93b294 39 }\r
40 );\r
41\r
42=head1 DESCRIPTION\r
43\r
44This module automatically inflates/deflates DateTime objects\r
45corresponding to applicable columns. Columns may also be\r
46defined to specify their nature, such as columns representing a\r
47creation time (set at time of insertion) or a modification time\r
48(set at time of every update).\r
49\r
7f67d2d5 50=head1 METHODS\r
51\r
ab93b294 52=head2 register_column\r
53\r
54This method will automatically add inflation and deflation rules\r
7f67d2d5 55to a column if an epoch value has been set in the column's definition.\r
56If the epoch value is 'ctime' (creation time) or 'mtime'\r
ab93b294 57(modification time), it will be registered as such for later\r
58use by the insert and the update methods.\r
59\r
60=head2 insert\r
61\r
62This method will set the value of all registered creation time\r
63columns to the current time. No changes will be made to a column\r
64whose value has already been set.\r
65\r
66=head2 update\r
67\r
68This method will set the value of all registered modification time\r
69columns to the current time. This will overwrite a column's value,\r
70even if it has been already set.\r
71\r
72=head1 SEE ALSO\r
73\r
74=over 4\r
75\r
76=item * DateTime\r
77\r
78=item * DBIx::Class\r
79\r
80=back\r
81\r
82=head1 AUTHOR\r
83\r
91616968 84=over 4\r
85\r
ab93b294 86=item * Brian Cassidy E<lt>bricas@cpan.orgE<gt>\r
87\r
c553ccdb 88=item * Adam Paynter E<lt>adapay@cpan.orgE<gt>\r
89\r
ab93b294 90=back\r
91\r
92=head1 COPYRIGHT AND LICENSE\r
93\r
94Copyright 2006 by Brian Cassidy\r
95\r
96This library is free software; you can redistribute it and/or modify\r
97it under the same terms as Perl itself. \r
98\r
99=cut\r
100\r
77f8d0c0 101__PACKAGE__->mk_classdata( ctime_columns => [ ] );\r
102__PACKAGE__->mk_classdata( mtime_columns => [ ] );\r
103\r
104sub register_column {\r
105 my( $class, $col, $info ) = @_;\r
106 $class->next::method( $col, $info );\r
107 \r
7f67d2d5 108 if( my $type = $info->{ epoch } ) {\r
77f8d0c0 109 $class->ctime_columns( [ @{ $class->ctime_columns }, $col ] ) if $type eq 'ctime';\r
110 $class->mtime_columns( [ @{ $class->mtime_columns }, $col ] ) if $type eq 'mtime';\r
111 \r
112 $class->inflate_column(\r
113 $col => {\r
114 inflate => sub { DateTime->from_epoch( epoch => shift ) },\r
115 deflate => sub { shift->epoch }\r
116 }\r
117 );\r
118 }\r
119}\r
120\r
121sub insert {\r
122 my $self = shift;\r
123 my $time = time;\r
124 \r
125 for my $column ( @{ $self->ctime_columns }, @{ $self->mtime_columns } ) {\r
126 next if defined $self->get_column( $column );\r
127 $self->store_column( $column => $time );\r
128 }\r
129\r
130 $self->next::method( @_ );\r
131}\r
132\r
133sub update {\r
134 my $self = shift;\r
135 my $time = time;\r
136 \r
137 for my $column ( @{ $self->mtime_columns } ) {\r
138 $self->set_column( $column => $time );\r
139 }\r
140 \r
141 $self->next::method( @_ );\r
142}\r
143\r
1441;