added new option to enforce Date or DateTime inflation, no matter what type the colum...
[dbsrgits/DBIx-Class.git] / lib / DBIx / Class / InflateColumn / DateTime.pm
CommitLineData
445e5e31 1package DBIx::Class::InflateColumn::DateTime;
2
3use strict;
4use warnings;
5use base qw/DBIx::Class/;
6
0b375b68 7=head1 NAME
8
5d0a2955 9DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from date and datetime columns.
0b375b68 10
11=head1 SYNOPSIS
12
13Load this component and then declare one or more
679a304a 14columns to be of the datetime, timestamp or date datatype.
0b375b68 15
16 package Event;
517d5d64 17 __PACKAGE__->load_components(qw/InflateColumn::DateTime Core/);
0b375b68 18 __PACKAGE__->add_columns(
19 starts_when => { data_type => 'datetime' }
20 );
21
22Then you can treat the specified column as a L<DateTime> object.
23
24 print "This event starts the month of ".
25 $event->starts_when->month_name();
26
dda9af55 27If you want to set a specific timezone for that field, use:
28
29 __PACKAGE__->add_columns(
30 starts_when => { data_type => 'datetime', extra => { timezone => "America/Chicago" } }
31 );
32
a97fe7e0 33If you want to inflate no matter what data_type your column is,
34use inflate_datetime or inflate_date:
35
36 __PACKAGE__->add_columns(
37 starts_when => { data_type => 'varchar', inflate_datetime => 1 }
38 );
39
40 __PACKAGE__->add_columns(
41 starts_when => { data_type => 'varchar', inflate_date => 1 }
42 );
43
0b375b68 44=head1 DESCRIPTION
45
46This module figures out the type of DateTime::Format::* class to
47inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
48that you are using. If you switch from one database to a different
5d0a2955 49one your code should continue to work without modification (though note
50that this feature is new as of 0.07, so it may not be perfect yet - bug
51reports to the list very much welcome).
0b375b68 52
943f93f2 53For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
de78905b 54
0b375b68 55=cut
56
445e5e31 57__PACKAGE__->load_components(qw/InflateColumn/);
58
59__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
60
9b83fccd 61=head2 register_column
62
63Chains with the L<DBIx::Class::Row/register_column> method, and sets
64up datetime columns appropriately. This would not normally be
65directly called by end users.
66
33a126ef 67In the case of an invalid date, L<DateTime> will throw an exception. To
68bypass these exceptions and just have the inflation return undef, use
69the C<datetime_undef_if_invalid> option in the column info:
70
71 "broken_date",
72 {
73 data_type => "datetime",
74 default_value => '0000-00-00',
75 is_nullable => 1,
76 datetime_undef_if_invalid => 1
77 }
78
9b83fccd 79=cut
80
445e5e31 81sub register_column {
82 my ($self, $column, $info, @rest) = @_;
83 $self->next::method($column, $info, @rest);
c209c4fd 84 return unless defined($info->{data_type});
85 my $type = lc($info->{data_type});
f011970e 86 $type = 'datetime' if ($type =~ /^timestamp/);
a97fe7e0 87 $type = 'datetime' if exists $info->{inflate_datetime} and $info->{inflate_datetime};
88 $type = 'date' if exists $info->{inflate_date} and $info->{inflate_date};
dda9af55 89 my $timezone;
90 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
91 $timezone = $info->{extra}{timezone};
92 }
93
dceeddc7 94 my $undef_if_invalid = $info->{datetime_undef_if_invalid};
95
c209c4fd 96 if ($type eq 'datetime' || $type eq 'date') {
97 my ($parse, $format) = ("parse_${type}", "format_${type}");
445e5e31 98 $self->inflate_column(
99 $column =>
100 {
101 inflate => sub {
102 my ($value, $obj) = @_;
33a126ef 103 my $dt = eval { $obj->_datetime_parser->$parse($value); };
dceeddc7 104 die "Error while inflating ${value} for ${column} on ${self}: $@"
105 if $@ and not $undef_if_invalid;
dda9af55 106 $dt->set_time_zone($timezone) if $timezone;
107 return $dt;
445e5e31 108 },
109 deflate => sub {
110 my ($value, $obj) = @_;
dda9af55 111 $value->set_time_zone($timezone) if $timezone;
c209c4fd 112 $obj->_datetime_parser->$format($value);
445e5e31 113 },
114 }
115 );
116 }
117}
118
119sub _datetime_parser {
120 my $self = shift;
121 if (my $parser = $self->__datetime_parser) {
122 return $parser;
123 }
124 my $parser = $self->result_source->storage->datetime_parser(@_);
125 return $self->__datetime_parser($parser);
126}
127
1281;
0b375b68 129__END__
130
131=head1 SEE ALSO
132
133=over 4
134
135=item More information about the add_columns method, and column metadata,
136 can be found in the documentation for L<DBIx::Class::ResultSource>.
137
138=back
139
140=head1 AUTHOR
141
142Matt S. Trout <mst@shadowcatsystems.co.uk>
143
144=head1 CONTRIBUTORS
145
146Aran Deltac <bluefeet@cpan.org>
147
148=head1 LICENSE
149
150You may distribute this code under the same terms as Perl itself.
151