File Coverage

blib/lib/DBIx/Class/DateTime/Epoch.pm
Criterion Covered Total %
statement 30 30 100.0
branch 11 12 91.6
condition 8 12 66.6
subroutine 7 7 100.0
pod 1 1 100.0
total 57 62 91.9


line stmt bran cond sub pod time code
1             package DBIx::Class::DateTime::Epoch;
2              
3 4     4   123411 use strict;
  4         10  
  4         96  
4 4     4   17 use warnings;
  4         9  
  4         1617  
5              
6             our $VERSION = '0.10';
7              
8 4     4   23 use base qw( DBIx::Class );
  4         11  
  4         934  
9              
10 4     4   79356 use DateTime;
  4         781870  
  4         1187  
11              
12             __PACKAGE__->load_components( qw( InflateColumn::DateTime ) );
13              
14             # back compat
15             sub add_columns {
16 4     4 1 126140 my( $class, @cols ) = @_;
17 4         10 my @columns;
18              
19 4         18 while (my $col = shift @cols) {
20 26 50       56 my $info = ref $cols[0] ? shift @cols : {};
21              
22 26 100       64 if( my $type = delete $info->{ epoch } ) {
23 8         17 $info->{ inflate_datetime } = 'epoch';
24              
25 8 100       37 if( $type =~ m{^[cm]time$} ) {
26 4         30 __PACKAGE__->load_components( 'TimeStamp' );
27 4         5252 $info->{ set_on_create } = 1;
28 4 100       17 $info->{ set_on_update } = 1 if $type eq 'mtime';
29             }
30             }
31              
32 26         81 push @columns, $col => $info;
33              
34             }
35              
36 4         17 $class->next::method( @columns );
37             }
38              
39             sub _inflate_to_datetime {
40 16     16   835616 my( $self, $value, $info, @rest ) = @_;
41             return $self->next::method( $value, $info, @rest )
42 16 100 66     141 unless $info->{ data_type } =~ m{int}i || (exists $info->{ inflate_datetime } && $info->{ inflate_datetime } eq 'epoch');
      66        
43              
44 14         114 return DateTime->from_epoch( epoch => $value );
45             }
46              
47             sub _deflate_from_datetime {
48 11     11   2083944 my( $self, $value, $info, @rest ) = @_;
49             return $self->next::method( $value, $info, @rest )
50 11 100 66     87 unless $info->{ data_type } =~ m{int}i || (exists $info->{ inflate_datetime } && $info->{ inflate_datetime } eq 'epoch');
      66        
51              
52 10         39 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 DATETIME::FORMAT DEPENDENCY
99              
100             There have been no assumptions made as to what RDBMS you will be using. As per
101             the note in the L<DBIx::Class::InflateColumn::DateTime documentation|DBIx::Class::InflateColumn::DateTime/DESCRIPTION>,
102             you will need to install the DateTime::Format::* module that matches your RDBMS
103             of choice.
104              
105             =head1 DESCRIPTION
106              
107             This module automatically inflates/deflates DateTime objects from/to epoch
108             values for the specified columns. This module is essentially an extension to
109             L<DBIx::Class::InflateColumn::DateTime> so all of the settings, including
110             C<locale> and C<timezone>, are also valid.
111              
112             A column will be recognized as an epoch time given one of the following scenarios:
113              
114             =over 4
115              
116             =item * C<data_type> is an C<int> of some sort and C<inflate_datetime> is also set to a true value
117              
118             =item * C<data_type> is some other value (e.g. C<varchar>) and C<inflate_datetime> is explicitly set to C<epoch>.
119              
120             =back
121              
122             L<DBIx::Class::TimeStamp> can also be used in conjunction with this module to support
123             epoch-based columns that are automatically set on creation of a row and updated subsequent
124             modifications.
125              
126             =head1 METHODS
127              
128             =head2 add_columns( )
129              
130             Provides backwards compatibility with the older DateTime::Epoch API.
131              
132             =head2 _inflate_to_datetime( )
133              
134             Overrides column inflation to use C<Datetime-E<gt>from_epoch>.
135              
136             =head2 _deflate_from_datetime( )
137              
138             Overrides column deflation to call C<epoch()> on the column value.
139              
140             =head1 SEE ALSO
141              
142             =over 4
143              
144             =item * L<DBIx::Class>
145              
146             =item * L<DBIx::Class::TimeStamp>
147              
148             =item * L<DateTime>
149              
150             =back
151              
152             =head1 AUTHORS
153              
154             Brian Cassidy E<lt>bricas@cpan.orgE<gt>
155              
156             Adam Paynter E<lt>adapay@cpan.orgE<gt>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             Copyright 2006-2012 by Brian Cassidy
161              
162             This library is free software; you can redistribute it and/or modify
163             it under the same terms as Perl itself.
164              
165             =cut
166