Put this code in the base class for your
Class::DBI-derived data class, and you can define meta-information (using
Config::Scoped) for your database rows, including per-column data. The name of the file is
mumble.cfg, where
mumble is the last part of your CDBI class name, and is in the same directory.
The result produces a hash accessible by calling ->Config on either the class name or an instance. For example, if you want to know that the name column of the My::DB::Address should be displayed as a textfield of width 15, you can put this meta information in Address.cfg as
name { type = textfield width = 15 }
and then access that in Template Toolkit as:
type = row.Config.name.type;
width = row.Config.name.width;
This box is too short to show more... I'm probably going to submit this as a separate module soon as a CDBI plugin.
sub Config {
my $self = shift;
my $class = ref $self || $self;
## compute filename relative to me, based on my packagename
my $p = __PACKAGE__;
(my $s = $class) =~ s/^\Q$p\E::// or die "$p is not prefix of $class
+!";
$p = __FILE__;
$p =~ s/\.pm$// or die "$p doesn't end with .pm!";
require File::Spec;
my $file = File::Spec->catfile($p, split '::', $s) . ".cfg";
my $config = do {
if (-e $file) {
require Config::Scoped;
Config::Scoped->new
(file => $file,
warnings => {qw(permissions off parameter off)},
)->parse;
} else {
{}
}
};
{
no strict 'refs';
*{$class . '::Config'} = sub { $config };
}
return $config;
}