X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/85da554f9ca728db840c0122e2c3535953919c64..3cc8ad871fc62723dbb9879a004a2af49d21bfef:/SL/DB/Helpers/Mappings.pm diff --git a/SL/DB/Helpers/Mappings.pm b/SL/DB/Helpers/Mappings.pm index 15ce697b5..13a798543 100644 --- a/SL/DB/Helpers/Mappings.pm +++ b/SL/DB/Helpers/Mappings.pm @@ -2,6 +2,10 @@ package SL::DB::Helpers::Mappings; 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( @@ -69,6 +73,49 @@ sub get_package_names { 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/(? model mappings used by the L script. If you add a new table that has custom mappings, add it here. +=head2 db + +A special function provided here is E. 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