Quiq/ClassConfig.pm

filter: Statementstmt 93.5% 58 / 62 Branchbran 77.7% 14 / 18 Conditioncond 44.4% 4 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 92.3% 12 / 13 Podpod 100% 5 / 5 totaltotal 84.0% 95 / 113 CC 15 SCAR 12.9
CC
15
Cov
85%
defCumulate
6.1
defMemoize
6.0
defSearch
4.0
CRAP 3.6
« Quiq/Html/Form/Matrix.pm ↑ Summary Quiq/Color.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::ClassConfig - Verwalte Information auf Klassenebene
8  
9 =head1 SYNOPSIS
10  
11 Klasse einbinden:
12  
13 use base qw/... Quiq::ClassConfig/;
14  
15 Information definieren (Anwendungsbeispiel):
16  
17 package Model::Object;
18
19 __PACKAGE__->def(
20 table => 'Object',
21 prefix => 'Obj',
22 columns => [
23 id => {
24 domain => 'Integer',
25 primaryKey => 1,
26 notNull => 1,
27 description => 'Primärschlüssel',
28 },
29 ...
30 ],
31 ...
32 );
33  
34 Information abfragen:
35  
36 my $table = Model::Object->defGet('table');
37 =>
38 Object
39  
40 Information suchen:
41  
42 my $table = Model::Object->defSearch('table');
43 =>
44 Object
45  
46 =head1 DESCRIPTION
47  
48 Die Klasse ermöglicht, Information in Klassen zu hinterlegen und
49 abzufragen. Anstatt hierfür Klassenvariablen mit C<our> zu
50 definieren, verwaltet die Klasse sämliche Information in einem
51 einzigen Hash (je Klasse natürlich) mit dem Namen
52 C<%ClassConfig>. Die Methoden der Klasse verwalten (erzeugen,
53 setzen, lesen) diesen Hash.
54  
55 =cut
56  
57 # -----------------------------------------------------------------------------
58  
59 package Quiq::ClassConfig;
60  
61 6 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
62 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
64  
65 6 our $VERSION = '1.240';
66  
67 6 ▶use Quiq::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
68  
69 # -----------------------------------------------------------------------------
70  
71 =head1 METHODS
72  
73 =head2 Information definieren
74  
75 =head3 def() - Definiere/Setze Klassen-Information
76  
77 =head4 Synopsis
78  
79 $h = $class->def(@keyVal);
80  
81 =head4 Arguments
82  
83 =over 4
84  
85 =item @keyVal
86  
87 Liste der Schlüssel/Wert-Paare
88  
89 =back
90  
91 =head4 Returns
92  
93 Referenz auf den Hash
94  
95 =head4 Description
96  
97 Erzeuge einen globalen Hash in Klasse $class und weise diesem die
98 Schlüssel/Wert-Paare @keyVal zu. Existiert der Hash bereits,
99 wird er nicht neu erzeugt.
100  
101 =cut
102  
103 # -----------------------------------------------------------------------------
104  
105 2 ▶sub def {
Subroutine def: called CC=3 SCAR=11.0
Pod documented
106 2 my $class = shift;
107 # @_: @keyVal
108  
109 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
110 2 my $h = *{$class.'::ClassConfig'}{HASH};
111 2 ▶ if (!$h) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($h) 2 0 2
112 2 *{$class.'::ClassConfig'} = $h = {};
113 }
114  
115 2 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 2 6
116 4 my $key = shift;
117 4 $h->{$key} = shift;
118 }
119  
120 2 return $h;
121 }
122  
123 # -----------------------------------------------------------------------------
124  
125 =head2 Information abfragen
126  
127 =head3 defGet() - Liefere Klassen-Information
128  
129 =head4 Synopsis
130  
131 $val = $class->defGet($key);
132 @vals = $class->defGet(@keys);
133  
134 =head4 Arguments
135  
136 =over 4
137  
138 =item $key bzw. @keys
139  
140 Schlüssel bzw. Liste von Schlüsseln.
141  
142 =back
143  
144 =head4 Returns
145  
146 =over 4
147  
148 =item $val bzw. @vals
149  
150 Wert bzw. Liste von Werten.
151  
152 =back
153  
154 =head4 Description
155  
156 Liefere die Werte zu den Schlüsseln @keys. Im Skalarkontext
157 liefere den Wert des ersten Schlüssels.
158  
159 =cut
160  
161 # -----------------------------------------------------------------------------
162  
163 3 ▶sub defGet {
Subroutine defGet: called CC=4 SCAR=13.9
Pod documented
164 3 my $class = shift;
165 # @_: @keys
166  
167 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
168 3 my $ref = *{$class.'::ClassConfig'}{HASH};
169  
170 3 my @arr;
171 3 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 3 6
172 3 my $key = shift;
173 3 ▶ CORE::push @arr,$ref? $ref->{$key}: undef;
Branch: 1 branch
BranchTrueFalseTotal
$ref ? : 2 1 3
174 }
175  
176 3 ▶ return wantarray? @arr: $arr[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
177 }
178  
179 # -----------------------------------------------------------------------------
180  
181 =head3 defMemoize() - Berechne und chache Klassen-Information
182  
183 =head4 Synopsis
184  
185 $val = $class->defMemoize($key,$sub);
186  
187 =head4 Arguments
188  
189 =over 4
190  
191 =item $key
192  
193 Schlüssel
194  
195 =item $sub
196  
197 Subroutine, die den Wert berechnet. Diese hat den Aufbau
198  
199 sub {
200 my ($class,$key) = @_;
201 ...
202 return $val;
203 }
204  
205 =back
206  
207 =head4 Returns
208  
209 =over 4
210  
211 =item $val
212  
213 Berechneter bzw. gecachter Wert
214  
215 =back
216  
217 =head4 Description
218  
219 Berechne den Wert per Subroutine $sub, speichere ihn unter dem
220 Schlüssel $key und liefere ihn schließlich zurück. Der Wert wird
221 nur beim ersten Aufruf berechnet, danach wird der gespeicherte
222 Wert unmittelbar geliefert.
223  
224 =cut
225  
226 # -----------------------------------------------------------------------------
227  
228 0 ▶sub defMemoize {
Subroutine defMemoize: not called CC=2 SCAR=17.9
Pod documented
229 0 my ($class,$key,$sub) = @_;
230  
231 0 my $h = $class->def; # Hash wird erzeugt, wenn er nicht existiert
232 0 ▶ return $h->{$key} //= $class->$sub($key);
Condition0%0/3
$h->{$key} //= $class->$sub($key)
l!l&&r!l&&!r
000
MC/DC0%0/2
$h->{$key} //= $class->$sub($key)
$h->{$key} $class->$sub($key)
Truth table0%0/3
A //= B
A= $h->{$key}B= $class->$sub($key)
ABresult
1X1
011
000
233 }
234  
235 # -----------------------------------------------------------------------------
236  
237 =head2 Information suchen
238  
239 =head3 defSearch() - Suche Klassen-Information in Vererbungshierarchie
240  
241 =head4 Synopsis
242  
243 $val = $class->defSearch($key);
244  
245 =head4 Arguments
246  
247 =over 4
248  
249 =item $key
250  
251 Schlüssel der Information.
252  
253 =back
254  
255 =head4 Returns
256  
257 Die gesuchte Information oder C<undef>.
258  
259 =head4 Description
260  
261 Suche "von unten nach oben" in der Vererbungshierarchie, beginnend
262 mit Klasse $class, die Information $key. Die erste Klasse, die die
263 Information besitzt, liefert den Wert. Existiert die Information
264 nicht, wird C<undef> geliefert.
265  
266 =cut
267  
268 # -----------------------------------------------------------------------------
269  
270 1 ▶sub defSearch {
Subroutine defSearch: called CC=4 SCAR=14.0
Pod documented
271 1 my ($class,$key) = @_;
272  
273 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
274 1 for my $class ($class,Quiq::Perl->baseClassesISA($class)) {
275 2 my $ref = *{$class.'::ClassConfig'}{HASH};
276 2 ▶ if ($ref && exists $ref->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and exists $ref->{$key}) 1 1 2
Condition66%2/3
$ref and exists $ref->{$key}
!ll&&!rl&&r
101
MC/DC50%1/2
$ref and exists $ref->{$key}
$ref exists $ref->{$key}
Truth table66%2/3
A and B
A= $refB= exists $ref->{$key}
ABresult
0X0
100
111
277 1 return $ref->{$key};
278 }
279 }
280  
281 0 return undef;
282 }
283  
284 # -----------------------------------------------------------------------------
285  
286 =head3 defCumulate() - Sammele Klassen-Information entlang Vererbungshierarchie
287  
288 =head4 Synopsis
289  
290 @arr | $arr = $class->defCumulate($key);
291  
292 =head4 Arguments
293  
294 =over 4
295  
296 =item $key
297  
298 Schlüssel der Information.
299  
300 =back
301  
302 =head4 Returns
303  
304 Liste der Werte. Im Skalarkontext eine Referenz auf die Liste.
305  
306 =head4 Description
307  
308 Durchlaufe die Klassenhierarchie I<von oben nach unten> und sammele
309 alle Werte des Attributs $key ein und liefere die Liste der
310 Werte zurück.
311  
312 Diese Methode ist nützlich, wenn z.B. Attributnamen entlang
313 einer Vererbungshierarchie definiert werden (je höher die Klasse
314 desto allgemeiner das Attribut) und für eine gegebene Klasse
315 die Liste der Attributnamen bestimmt werden soll.
316  
317 =head4 Example
318  
319 Klassenhierarchie:
320  
321 package Object;
322 use base/Quiq::ClassConfig/;
323
324 __PACKAGE__->def(
325 Attributes => [qw/
326 Id
327 /],
328 );
329
330 package Person;
331 use base qw/Object/;
332
333 __PACKAGE__->def(
334 Attributes => [qw/
335 Vorname
336 Nachname
337 /],
338 );
339  
340 Attributes der Klasse Person:
341  
342 @attributes = Person->defCumulate('Attributes');
343 =>
344 ('Id', 'Vorname', 'Nachname')
345  
346 =cut
347  
348 # -----------------------------------------------------------------------------
349  
350 3 ▶sub defCumulate {
Subroutine defCumulate: called CC=6 SCAR=18.1
Pod documented
351 3 my ($class,$key) = @_;
352  
353 6 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
354  
355 3 my @arr;
356 3 for my $class (reverse(Quiq::Perl->baseClassesISA($class)),$class) {
357 9 my $ref = *{$class.'::ClassConfig'}{HASH};
358 9 ▶ if ($ref && exists $ref->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref and exists $ref->{$key}) 5 4 9
Condition66%2/3
$ref and exists $ref->{$key}
!ll&&!rl&&r
405
MC/DC50%1/2
$ref and exists $ref->{$key}
$ref exists $ref->{$key}
Truth table66%2/3
A and B
A= $refB= exists $ref->{$key}
ABresult
0X0
100
111
359 5 my $val = $ref->{$key};
360 5 ▶ push @arr,ref $val? @$val: $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ? : 5 0 5
361 }
362 }
363  
364 3 ▶ return wantarray? @arr: \@arr;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
365 }
366  
367 # -----------------------------------------------------------------------------
368  
369 =head1 VERSION
370  
371 1.240
372  
373 =head1 AUTHOR
374  
375 Frank Seitz, L<http://fseitz.de/>
376  
377 =head1 COPYRIGHT
378  
379 Copyright (C) 2026 Frank Seitz
380  
381 =head1 LICENSE
382  
383 This code is free software; you can redistribute it and/or modify
384 it under the same terms as Perl itself.
385  
386 =cut
387  
388 # -----------------------------------------------------------------------------
389  
390 6 1;
391  
392 # eof
« Quiq/Html/Form/Matrix.pm ↑ Summary Quiq/Color.pm »