]> wagnertech.de Git - mfinanz.git/blobdiff - SL/DB/Helpers/Mappings.pm
Exportierbarer Modelfinder "db" in SL::DB::Helpers::Mappings.
[mfinanz.git] / SL / DB / Helpers / Mappings.pm
index 0ba43d9f18d8c4120e2d7a200d9491057fbf3381..13a7985438495bdb21a98301089875c7fa94ad56 100644 (file)
@@ -2,7 +2,11 @@ package SL::DB::Helpers::Mappings;
 
 use strict;
 
-# threse will not be managed as Rose::DB models, because they are not normalized
+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(
   acc_trans audittrail customertax datev defaults department dpt_trans
@@ -14,11 +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(
-  bank_accounts buchungsgruppen contacts custom_variable_configs
-  custom_variables custom_variables_validity drafts dunning dunning_config
-  employee follow_up_links follow_ups generic_translations history_erp language
-  license notes payment_terms pricegroup rma schema_info sepa_export
-  sepa_export_items tax taxkeys
 );
 
 my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_temp);
@@ -29,15 +28,40 @@ my @lxoffice_blacklist = (@lxoffice_blacklist_permanent, @lxoffice_blacklist_tem
 my %lxoffice_package_names = (
   ar                             => 'invoice',
   ap                             => 'purchase_invoice',
+  bank_accounts                  => 'bank_account',
+  buchungsgruppen                => 'buchungsgruppe',
+  contacts                       => 'contact',
+  custom_variable_configs        => 'custom_variable_config',
+  custom_variables               => 'custom_variable',
+  custom_variables_validity      => 'custom_variable_validity',
   delivery_orders                => 'delivery_order',
   delivery_order_items           => 'delivery_order_item',
+  drafts                         => 'draft',
+  dunning                        => 'dunning',
+  dunning_config                 => 'dunning_config',
+  employee                       => 'employee',
+  follow_up_links                => 'follow_up_link',
+  follow_ups                     => 'follow_up',
+  generic_translations           => 'generic_translation',
   gl                             => 'GLTransaction',
+  history_erp                    => 'history',
   invoice                        => 'invoice_item',
+  language                       => 'language',
+  license                        => 'licemse',
+  notes                          => 'note',
   orderitems                     => 'order_item',
   oe                             => 'order',
   parts                          => 'part',
+  payment_terms                  => 'payment_term',
   price_factors                  => 'price_factor',
+  pricegroup                     => 'pricegroup',
   printers                       => 'Printer',
+  rma                            => 'RMA',
+  sepa_export                    => 'sepa_export',
+  sepa_export_items              => 'sepa_export_item',
+  schema_info                    => 'schema_info',
+  tax                            => 'tax',
+  taxkeys                        => 'taxkey',
   units                          => 'unit',
 );
 
@@ -49,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/(?<!^)\u(.)/'_' . lc($1)/ge;
+  lcfirst $str;
+}
+
+sub plurify {
+  my ($str) = @_;
+  $str . 's';
+}
+
+sub singlify {
+  my ($str) = @_;
+  local $/ = 's';
+  chomp $str;
+  $str;
+}
+
 1;
 
 __END__
@@ -67,6 +134,24 @@ This modul stores table <-> model mappings used by the
 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