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