added back-compat for the "epoch => 1", etc syntax
[dbsrgits/DBIx-Class-DateTime-Epoch.git] / lib / DBIx / Class / DateTime / Epoch.pm
CommitLineData
e4480da2 1package DBIx::Class::DateTime::Epoch;
2
3use strict;
4use warnings;
5
29f37a29 6our $VERSION = '0.05';
e4480da2 7
8use base qw( DBIx::Class );
9
10use DateTime;
11
29f37a29 12__PACKAGE__->load_components( qw( DynamicDefault InflateColumn::DateTime ) );
13
14# back compat
15sub register_column {
16 my( $class, $col, $info ) = @_;
17
18 if( my $type = delete $info->{ epoch } ) {
19 $info->{ inflate_datetime } = 'epoch';
20
21 if( $type =~ m{^[cm]time$} ) {
22 __PACKAGE__->load_components( 'TimeStamp' );
23 $info->{ dynamic_default_on_create } = 'get_timestamp';
24 $info->{ dynamic_default_on_update } = 'get_timestamp' if $type eq 'mtime';
25 }
26 }
27
28 $class->next::method( $col, $info );
29}
8e52f1da 30
31sub _inflate_to_datetime {
32 my( $self, $value, $info, @rest ) = @_;
33 $self->next::method( $value, $info, @rest )
34 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
35
36 return DateTime->from_epoch( epoch => $value );
37}
38
39sub _deflate_from_datetime {
40 my( $self, $value, $info, @rest ) = @_;
41 $self->next::method( $value, $info, @rest )
42 unless $info->{ data_type } =~ m{int} || $info->{ inflate_datetime } eq 'epoch';
43
44 return $value->epoch;
45}
46
471;
48
49__END__
50
e4480da2 51=head1 NAME
52
8e52f1da 53DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects
e4480da2 54
55=head1 SYNOPSIS
56
8e52f1da 57 package MySchema::Foo;
e4480da2 58
59 use base qw( DBIx::Class );
60
8e52f1da 61 __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
e4480da2 62 __PACKAGE__->add_columns(
63 name => {
64 data_type => 'varchar',
8e52f1da 65 size => 10,
66 },
67 bar => { # epoch stored as an int
68 data_type => 'bigint',
69 inflate_datetime => 1,
e4480da2 70 },
8e52f1da 71 baz => { # epoch stored as a string
72 data_type => 'varchar',
73 size => 50,
74 inflate_datetime => 'epoch',
e4480da2 75 },
8e52f1da 76 # working in conjunction with DBIx::Class::TimeStamp
e4480da2 77 creation_time => {
8e52f1da 78 data_type => 'bigint',
79 inflate_datetime => 1,
80 set_on_create => 1,
e4480da2 81 },
82 modification_time => {
8e52f1da 83 data_type => 'bigint',
84 inflate_datetime => 1,
85 set_on_create => 1,
86 set_on_update => 1,
e4480da2 87 }
88 );
89
90=head1 DESCRIPTION
91
8e52f1da 92This module automatically inflates/deflates DateTime objects from/to epoch
93values for the specified columns. This module is essentially an extension to
94L<DBIx::Class::InflateColumn::DateTime> so all of the settings, including
95C<locale> and C<timezone>, are also valid.
e4480da2 96
8e52f1da 97A column will be recognized as an epoch time given one of the following scenarios:
98
99=over 4
e4480da2 100
8e52f1da 101=item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
e4480da2 102
8e52f1da 103=item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
e4480da2 104
8e52f1da 105=back
106
107L<DBIx::Class::TimeStamp> can also be used in conjunction with this module to support
108epoch-based columns that are automatically set on creation of a row and updated subsequent
109modifications.
110
111=head1 METHODS
e4480da2 112
8e52f1da 113=head2 _inflate_to_datetime( )
e4480da2 114
8e52f1da 115Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
e4480da2 116
8e52f1da 117=head2 _deflate_from_datetime( )
118
119Overrides column deflation to call C<epoch()> on the column value.
e4480da2 120
121=head1 SEE ALSO
122
123=over 4
124
e4480da2 125=item * DBIx::Class
126
8e52f1da 127=item * DBIx::Class::TimeStamp
128
129=item * DateTime
130
e4480da2 131=back
132
0a389a2c 133=head1 AUTHORS
e4480da2 134
0a389a2c 135Brian Cassidy E<lt>bricas@cpan.orgE<gt>
e4480da2 136
0a389a2c 137Adam Paynter E<lt>adapay@cpan.orgE<gt>
e4480da2 138
139=head1 COPYRIGHT AND LICENSE
140
8e52f1da 141Copyright 2006-2009 by Brian Cassidy
e4480da2 142
143This library is free software; you can redistribute it and/or modify
144it under the same terms as Perl itself.
145
146=cut
147