Revision 3cc8ad87
Von Sven Schöling vor mehr als 14 Jahren hinzugefügt
SL/DB/Helpers/Mappings.pm | ||
---|---|---|
|
||
use strict;
|
||
|
||
use Exporter qw(import);
|
||
|
||
our @EXPORT_OK = qw(db);
|
||
|
||
# these will not be managed as Rose::DB models, because they are not normalized
|
||
# significant changes are needed to get them done.
|
||
my @lxoffice_blacklist_permanent = qw(
|
||
... | ... | |
return LXOFFICE => \%lxoffice_package_names;
|
||
}
|
||
|
||
sub db {
|
||
my $string = $_[0];
|
||
my $lookup = $lxoffice_package_names{$_[0]} ||
|
||
plurify($lxoffice_package_names{singlify($_[0])});
|
||
|
||
for my $thing ($string, $lookup) {
|
||
|
||
# best guess? its already the name. like part. camelize it first
|
||
my $class = "SL::DB::" . camelify($thing);
|
||
return $class if defined *{ $class. '::' };
|
||
|
||
# next, someone wants a manager and pluralized.
|
||
my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
|
||
return $manager if defined *{ $manager . '::' };
|
||
}
|
||
|
||
die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
|
||
}
|
||
|
||
sub camelify {
|
||
my ($str) = @_;
|
||
$str =~ s/_+(.)/uc($1)/ge;
|
||
ucfirst $str;
|
||
}
|
||
|
||
sub snakify {
|
||
my ($str) = @_;
|
||
$str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
|
||
lcfirst $str;
|
||
}
|
||
|
||
sub plurify {
|
||
my ($str) = @_;
|
||
$str . 's';
|
||
}
|
||
|
||
sub singlify {
|
||
my ($str) = @_;
|
||
local $/ = 's';
|
||
chomp $str;
|
||
$str;
|
||
}
|
||
|
||
1;
|
||
|
||
__END__
|
||
... | ... | |
L<scripts/rose_auto_create_model.pl> script. If you add a new table that has
|
||
custom mappings, add it here.
|
||
|
||
=head2 db
|
||
|
||
A special function provided here is E<db>. Without it you'd have to write:
|
||
|
||
my $part = SL::DB::Part->new(id => 1234);
|
||
my @all_parts = SL::DB::Manager::Part->get_all;
|
||
|
||
with them it becomes:
|
||
|
||
my $part = db('part')->new(id => 123);
|
||
my @all_parts = db('parts')->get_all;
|
||
|
||
You don't have to care about add that SL::DB:: incantation anymore. Also, a
|
||
simple s at the end will get you the associated Manager class.
|
||
|
||
db is written to try to make sense of what you give it, but if all fails, it
|
||
will die with an error.
|
||
|
||
=head1 BUGS
|
||
|
||
nothing yet
|
t/helper/mapping.t | ||
---|---|---|
use Test::More tests => 12;
|
||
|
||
use_ok 'SL::DB::Helpers::ALL';
|
||
use_ok 'SL::DB::Helpers::Mappings', qw(db);
|
||
|
||
is db('part'), 'SL::DB::Part';
|
||
is db('parts'), 'SL::DB::Manager::Part';
|
||
|
||
is db('order'), 'SL::DB::Order';
|
||
is db('orders'), 'SL::DB::Manager::Order';
|
||
|
||
is db('gl'), 'SL::DB::GLTransaction';
|
||
is db('gls'), 'SL::DB::Manager::GLTransaction';
|
||
|
||
is db('ar'), 'SL::DB::Invoice';
|
||
is db('ars'), 'SL::DB::Manager::Invoice';
|
||
|
||
is db('Unit'), 'SL::DB::Unit';
|
||
is db('Units'), 'SL::DB::Manager::Unit';
|
Auch abrufbar als: Unified diff
Exportierbarer Modelfinder "db" in SL::DB::Helpers::Mappings.