Missing stuff for DateTime branch
Matt S Trout [Wed, 17 May 2006 00:22:06 +0000 (00:22 +0000)]
lib/DBIx/Class/InflateColumn/DateTime.pm [new file with mode: 0644]
t/lib/DBICTest/Schema/Event.pm [new file with mode: 0644]

diff --git a/lib/DBIx/Class/InflateColumn/DateTime.pm b/lib/DBIx/Class/InflateColumn/DateTime.pm
new file mode 100644 (file)
index 0000000..72c8844
--- /dev/null
@@ -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 (file)
index 0000000..92dad5d
--- /dev/null
@@ -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;