From 3cc8ad871fc62723dbb9879a004a2af49d21bfef Mon Sep 17 00:00:00 2001 From: =?utf8?q?Sven=20Sch=C3=B6ling?= Date: Mon, 13 Sep 2010 15:35:31 +0200 Subject: [PATCH] Exportierbarer Modelfinder "db" in SL::DB::Helpers::Mappings. --- SL/DB/Helpers/Mappings.pm | 65 +++++++++++++++++++++++++++++++++++++++ t/helper/mapping.t | 19 ++++++++++++ 2 files changed, 84 insertions(+) create mode 100644 t/helper/mapping.t 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 diff --git a/t/helper/mapping.t b/t/helper/mapping.t new file mode 100644 index 000000000..d0c015cf6 --- /dev/null +++ b/t/helper/mapping.t @@ -0,0 +1,19 @@ +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'; -- 2.20.1