added tests for required modules, minor documentation update
[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
0b375b68 33=head1 DESCRIPTION
34
35This module figures out the type of DateTime::Format::* class to
36inflate/deflate with based on the type of DBIx::Class::Storage::DBI::*
37that you are using. If you switch from one database to a different
5d0a2955 38one your code should continue to work without modification (though note
39that this feature is new as of 0.07, so it may not be perfect yet - bug
40reports to the list very much welcome).
0b375b68 41
943f93f2 42For more help with using components, see L<DBIx::Class::Manual::Component/USING>.
de78905b 43
0b375b68 44=cut
45
445e5e31 46__PACKAGE__->load_components(qw/InflateColumn/);
47
48__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
49
9b83fccd 50=head2 register_column
51
52Chains with the L<DBIx::Class::Row/register_column> method, and sets
53up datetime columns appropriately. This would not normally be
54directly called by end users.
55
56=cut
57
445e5e31 58sub register_column {
59 my ($self, $column, $info, @rest) = @_;
60 $self->next::method($column, $info, @rest);
c209c4fd 61 return unless defined($info->{data_type});
62 my $type = lc($info->{data_type});
f011970e 63 $type = 'datetime' if ($type =~ /^timestamp/);
dda9af55 64 my $timezone;
65 if ( exists $info->{extra} and exists $info->{extra}{timezone} and defined $info->{extra}{timezone} ) {
66 $timezone = $info->{extra}{timezone};
67 }
68
c209c4fd 69 if ($type eq 'datetime' || $type eq 'date') {
70 my ($parse, $format) = ("parse_${type}", "format_${type}");
445e5e31 71 $self->inflate_column(
72 $column =>
73 {
74 inflate => sub {
75 my ($value, $obj) = @_;
dda9af55 76 my $dt = $obj->_datetime_parser->$parse($value);
77 $dt->set_time_zone($timezone) if $timezone;
78 return $dt;
445e5e31 79 },
80 deflate => sub {
81 my ($value, $obj) = @_;
dda9af55 82 $value->set_time_zone($timezone) if $timezone;
c209c4fd 83 $obj->_datetime_parser->$format($value);
445e5e31 84 },
85 }
86 );
87 }
88}
89
90sub _datetime_parser {
91 my $self = shift;
92 if (my $parser = $self->__datetime_parser) {
93 return $parser;
94 }
95 my $parser = $self->result_source->storage->datetime_parser(@_);
96 return $self->__datetime_parser($parser);
97}
98
991;
0b375b68 100__END__
101
102=head1 SEE ALSO
103
104=over 4
105
106=item More information about the add_columns method, and column metadata,
107 can be found in the documentation for L<DBIx::Class::ResultSource>.
108
109=back
110
111=head1 AUTHOR
112
113Matt S. Trout <mst@shadowcatsystems.co.uk>
114
115=head1 CONTRIBUTORS
116
117Aran Deltac <bluefeet@cpan.org>
118
119=head1 LICENSE
120
121You may distribute this code under the same terms as Perl itself.
122