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