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