Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Associate a config file with a Class::DBI class for meta information

by merlyn (Sage)
on Apr 15, 2005 at 16:35 UTC ( [id://448251]=CUFP: print w/replies, xml ) Need Help??

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; }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://448251]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (3)
As of 2024-04-25 17:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found