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