Skip to content

Instantly share code, notes, and snippets.

@shiba-yu36
Created March 17, 2011 14:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shiba-yu36/874453 to your computer and use it in GitHub Desktop.
Save shiba-yu36/874453 to your computer and use it in GitHub Desktop.
schema設定のほう
package My::Schema;
use strict;
use warnings;
use Teng::Schema::Declare;
Teng::Schema::Declare->load_plugin('Column::DateTime');
table {
name "tags";
pk "id";
columns qw( name created_at updated_at );
datetime_columns qw(created_at updated_at);
# 下と同様の効果があったらいいな
# inflate qr{^(?:created_at|updated_at)$} => \&inflate_datetime;
# deflate qr{^(?:created_at|updated_at)$} => \&deflate_datetime;
};
# sub inflate_datetime {
# my ($col_value) = @_;
# return DateTime::Format::MySQL->parse_datetime($col_value);
# }
# sub deflate_datetime {
# my ($col_value) = @_;
# return DateTime::Format::MySQL->format_datetime($col_value);
# }
1;
package Teng::Schema::Declare::Plugin::Column::DateTime;
use strict;
use warnings;
use base qw(Teng::Schema::Declare::Plugin);
sub register {
my ($self, $option) = @_;
$self->register_declare_method(datetime_columns => sub {
my (@columns) = @_;
my $columns_regexp = join('|', @columns);
my $regexp = qr{^(?:$columns_regexp)$};
inflate $regexp => \&inflate_datetime;
deflate $regexp => \&deflate_datetime;
});
}
sub inflate_datetime {
my ($col_value) = @_;
return DateTime::Format::MySQL->parse_datetime($col_value);
}
sub deflate_datetime {
my ($col_value) = @_;
return DateTime::Format::MySQL->format_datetime($col_value);
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment