X-Git-Url: http://wagnertech.de/gitweb/gitweb.cgi/mfinanz.git/blobdiff_plain/4fd22b569d4436293e0a9d364d7356b5bfc503e5..3cc8ad871fc62723dbb9879a004a2af49d21bfef:/SL/DB/Helpers/Mappings.pm diff --git a/SL/DB/Helpers/Mappings.pm b/SL/DB/Helpers/Mappings.pm index d4ee675e5..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( @@ -14,7 +18,6 @@ my @lxoffice_blacklist_permanent = qw( # these are not managed _yet_, but will hopefully at some point. # if you are confident that one of these works, remove it here. my @lxoffice_blacklist_temp = qw( - schema_info ); my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp); @@ -56,6 +59,7 @@ my %lxoffice_package_names = ( rma => 'RMA', sepa_export => 'sepa_export', sepa_export_items => 'sepa_export_item', + schema_info => 'schema_info', tax => 'tax', taxkeys => 'taxkey', units => 'unit', @@ -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