Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 3cc8ad87

Von Sven Schöling vor etwa 14 Jahren hinzugefügt

  • ID 3cc8ad871fc62723dbb9879a004a2af49d21bfef
  • Vorgänger 85da554f
  • Nachfolger f97b0778

Exportierbarer Modelfinder "db" in SL::DB::Helpers::Mappings.

Unterschiede anzeigen:

SL/DB/Helpers/Mappings.pm
2 2

  
3 3
use strict;
4 4

  
5
use Exporter qw(import);
6

  
7
our @EXPORT_OK = qw(db);
8

  
5 9
# these will not be managed as Rose::DB models, because they are not normalized
6 10
# significant changes are needed to get them done.
7 11
my @lxoffice_blacklist_permanent = qw(
......
69 73
  return LXOFFICE => \%lxoffice_package_names;
70 74
}
71 75

  
76
sub db {
77
  my $string = $_[0];
78
  my $lookup = $lxoffice_package_names{$_[0]} ||
79
      plurify($lxoffice_package_names{singlify($_[0])});
80

  
81
  for my $thing ($string, $lookup) {
82

  
83
    # best guess? its already the name. like part. camelize it first
84
    my $class = "SL::DB::" . camelify($thing);
85
    return $class if defined *{ $class. '::' };
86

  
87
    # next, someone wants a manager and pluralized.
88
    my $manager = "SL::DB::Manager::" . singlify(camelify($thing));
89
    return $manager if defined *{ $manager . '::' };
90
  }
91

  
92
  die "Can't resolve '$string' as a database model, sorry. Did you perhaps forgot to load it?";
93
}
94

  
95
sub camelify {
96
  my ($str) = @_;
97
  $str =~ s/_+(.)/uc($1)/ge;
98
  ucfirst $str;
99
}
100

  
101
sub snakify {
102
  my ($str) = @_;
103
  $str =~ s/(?<!^)\u(.)/'_' . lc($1)/ge;
104
  lcfirst $str;
105
}
106

  
107
sub plurify {
108
  my ($str) = @_;
109
  $str . 's';
110
}
111

  
112
sub singlify {
113
  my ($str) = @_;
114
  local $/ = 's';
115
  chomp $str;
116
  $str;
117
}
118

  
72 119
1;
73 120

  
74 121
__END__
......
87 134
L<scripts/rose_auto_create_model.pl> script.  If you add a new table that has
88 135
custom mappings, add it here.
89 136

  
137
=head2 db
138

  
139
A special function provided here is E<db>. Without it you'd have to write:
140

  
141
  my $part = SL::DB::Part->new(id => 1234);
142
  my @all_parts = SL::DB::Manager::Part->get_all;
143

  
144
with them it becomes:
145

  
146
  my $part = db('part')->new(id => 123);
147
  my @all_parts = db('parts')->get_all;
148

  
149
You don't have to care about add that SL::DB:: incantation anymore. Also, a
150
simple s at the end will get you the associated Manager class.
151

  
152
db is written to try to make sense of what you give it, but if all fails, it
153
will die with an error.
154

  
90 155
=head1 BUGS
91 156

  
92 157
nothing yet

Auch abrufbar als: Unified diff