afb14148816695718a5baa91cffd7a06f3da309a
[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.04';
7
8 use base qw( DBIx::Class );
9
10 use DateTime;
11
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
34 =head1 NAME
35
36 DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects
37
38 =head1 SYNOPSIS
39
40     package MySchema::Foo;
41     
42     use base qw( DBIx::Class );
43     
44     __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
45     __PACKAGE__->add_columns(
46         name => {
47             data_type => 'varchar',
48             size      => 10,
49         },
50         bar => { # epoch stored as an int
51             data_type        => 'bigint',
52             inflate_datetime => 1,
53         },
54         baz => { # epoch stored as a string
55             data_type        => 'varchar',
56             size             => 50,
57             inflate_datetime => 'epoch',
58         },
59         # working in conjunction with DBIx::Class::TimeStamp
60         creation_time => {
61             data_type        => 'bigint',
62             inflate_datetime => 1,
63             set_on_create    => 1,
64         },
65         modification_time => {
66             data_type        => 'bigint',
67             inflate_datetime => 1,
68             set_on_create    => 1,
69             set_on_update    => 1,
70         }
71     );
72
73 =head1 DESCRIPTION
74
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.
79
80 A column will be recognized as an epoch time given one of the following scenarios:
81
82 =over 4
83
84 =item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
85
86 =item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
87
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
95
96 =head2 _inflate_to_datetime( )
97
98 Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
99
100 =head2 _deflate_from_datetime( )
101
102 Overrides column deflation to call C<epoch()> on the column value.
103
104 =head1 SEE ALSO
105
106 =over 4
107
108 =item * DBIx::Class
109
110 =item * DBIx::Class::TimeStamp
111
112 =item * DateTime
113
114 =back
115
116 =head1 AUTHORS
117
118 Brian Cassidy E<lt>bricas@cpan.orgE<gt>
119
120 Adam Paynter E<lt>adapay@cpan.orgE<gt>
121
122 =head1 COPYRIGHT AND LICENSE
123
124 Copyright 2006-2009 by Brian Cassidy
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