added entry to Changes for recent merge alterations
[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
9DBIx::Class::InflateColumn::DateTime - Auto-create DateTime objects from datetime columns.
10
11=head1 SYNOPSIS
12
13Load this component and then declare one or more
14columns to be of the datetime datatype.
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
32one your code will continue to work without modification.
33
34=cut
35
445e5e31 36__PACKAGE__->load_components(qw/InflateColumn/);
37
38__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser');
39
9b83fccd 40=head2 register_column
41
42Chains with the L<DBIx::Class::Row/register_column> method, and sets
43up datetime columns appropriately. This would not normally be
44directly called by end users.
45
46=cut
47
445e5e31 48sub register_column {
49 my ($self, $column, $info, @rest) = @_;
50 $self->next::method($column, $info, @rest);
c209c4fd 51 return unless defined($info->{data_type});
52 my $type = lc($info->{data_type});
53 if ($type eq 'datetime' || $type eq 'date') {
54 my ($parse, $format) = ("parse_${type}", "format_${type}");
445e5e31 55 $self->inflate_column(
56 $column =>
57 {
58 inflate => sub {
59 my ($value, $obj) = @_;
c209c4fd 60 $obj->_datetime_parser->$parse($value);
445e5e31 61 },
62 deflate => sub {
63 my ($value, $obj) = @_;
c209c4fd 64 $obj->_datetime_parser->$format($value);
445e5e31 65 },
66 }
67 );
68 }
69}
70
71sub _datetime_parser {
72 my $self = shift;
73 if (my $parser = $self->__datetime_parser) {
74 return $parser;
75 }
76 my $parser = $self->result_source->storage->datetime_parser(@_);
77 return $self->__datetime_parser($parser);
78}
79
801;
0b375b68 81__END__
82
83=head1 SEE ALSO
84
85=over 4
86
87=item More information about the add_columns method, and column metadata,
88 can be found in the documentation for L<DBIx::Class::ResultSource>.
89
90=back
91
92=head1 AUTHOR
93
94Matt S. Trout <mst@shadowcatsystems.co.uk>
95
96=head1 CONTRIBUTORS
97
98Aran Deltac <bluefeet@cpan.org>
99
100=head1 LICENSE
101
102You may distribute this code under the same terms as Perl itself.
103