From: Matt S Trout Date: Wed, 17 May 2006 00:22:06 +0000 (+0000) Subject: Missing stuff for DateTime branch X-Git-Tag: v0.07002~75^2~182^2~4 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=445e5e31324c54ce3ffe96199e4e308efaa0823e;p=dbsrgits%2FDBIx-Class.git Missing stuff for DateTime branch --- diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm new file mode 100644 index 0000000..72c8844 --- /dev/null +++ b/lib/DBIx/Class/InflateColumn/DateTime.pm @@ -0,0 +1,40 @@ +package DBIx::Class::InflateColumn::DateTime; + +use strict; +use warnings; +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/InflateColumn/); + +__PACKAGE__->mk_group_accessors('simple' => '__datetime_parser'); + +sub register_column { + my ($self, $column, $info, @rest) = @_; + $self->next::method($column, $info, @rest); + if ($info->{data_type} =~ /^datetime$/i) { + $self->inflate_column( + $column => + { + inflate => sub { + my ($value, $obj) = @_; + $obj->_datetime_parser->parse_datetime($value); + }, + deflate => sub { + my ($value, $obj) = @_; + $obj->_datetime_parser->format_datetime($value); + }, + } + ); + } +} + +sub _datetime_parser { + my $self = shift; + if (my $parser = $self->__datetime_parser) { + return $parser; + } + my $parser = $self->result_source->storage->datetime_parser(@_); + return $self->__datetime_parser($parser); +} + +1; diff --git a/t/lib/DBICTest/Schema/Event.pm b/t/lib/DBICTest/Schema/Event.pm new file mode 100644 index 0000000..92dad5d --- /dev/null +++ b/t/lib/DBICTest/Schema/Event.pm @@ -0,0 +1,16 @@ +package DBICTest::Schema::Event; + +use base qw/DBIx::Class/; + +__PACKAGE__->load_components(qw/InflateColumn::DateTime PK::Auto Core/); + +__PACKAGE__->table('event'); + +__PACKAGE__->add_columns( + id => { data_type => 'integer', is_auto_increment => 1 }, + starts_at => { data_type => 'datetime' } +); + +__PACKAGE__->set_primary_key('id'); + +1;