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