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