Debian Codegraph @

t/cdbi-t-ocache/25-closures_in_meta.t

raw
1 use strict;
2 use Test::More;
3 use Class::DBI::Sweet;
4 Class::DBI::Sweet->default_search_attributes({ use_resultset_cache => 0 });
5 Class::DBI::Sweet->cache(Cache::MemoryCache->new(
6 { namespace => "SweetTest", default_expires_in => 60 } ) );
7
8 package main;
9
10 BEGIN {
11 eval "use Cache::MemoryCache";
12
13 eval { require 't/cdbi-t/testlib/MyBase.pm' };
14 plan skip_all => "Need MySQL for this test" if $@;
15
16 eval "use DateTime::Format::MySQL";
17 plan skip_all => "Need DateTime::Format::MySQL for this test" if $@;
18 }
19
20 package Temp::DBI;
21
22 BEGIN {
23 eval "use Cache::MemoryCache";
24 plan skip_all => "needs Cache::Cache for testing" if $@;
25 eval { require 't/cdbi-t/testlib/MyBase.pm' };
26 }
27 use base 'MyBase';
28
29 # typically provided by Class::DBI::mysql
30
31 sub autoinflate_dates {
32 my $class = shift;
33 my $columns = $class->_column_info;
34
35 foreach my $c (keys %$columns) {
36 my $type = $columns->{$c}->{type};
37 next unless ($type =~ m/^(date)/);
38 my $i_method = "parse_$type";
39 my $d_method = "format_$type";
40 $class->has_a(
41 $c => 'DateTime',
42 inflate => sub { DateTime::Format::MySQL->$i_method(shift) },
43 deflate => sub { DateTime::Format::MySQL->$d_method(shift) },
44 );
45 }
46 }
47
48 package Temp::DateTable;
49 use base 'Temp::DBI';
50
51 __PACKAGE__->set_table();
52 __PACKAGE__->columns(All => qw/my_id my_datetime my_date/,);
53
54 __PACKAGE__->autoinflate_dates;
55
56 sub create_sql {
57 return qq{
58 my_id integer not null auto_increment primary key,
59 my_datetime datetime,
60 my_date date
61 };
62 }
63
64 # typically provided by Class::DBI::mysql
65
66 sub _column_info {
67 return { map { 'my_' . $_ => { type => $_ } } qw(datetime date) };
68 }
69
70 package main;
71
72 plan tests => 1;
73 my $dt = DateTime->now();
74 my $row = Temp::DateTable->create(
75 {
76 map {
77 my $method = "format_$_";
78 ("my_$_" => DateTime::Format::MySQL->$method($dt))
79 }
80 qw(date datetime)
81 }
82 );
83
84 my $date = eval { $row->my_date };
85 isa_ok($date, 'DateTime');
86
87 1;