Meta.pm

filter: Statementstmt 92.5% 50 / 54 Branchbran 65.0% 13 / 20 Conditioncond 25.0% 3 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 92.3% 12 / 13 Podpod 100% 10 / 10 totaltotal 75.2% 88 / 117 CC 17 SCAR 13.9
CC
17
Cov
77%
setIndexValue
12.0
get
5.1
getMeta
4.2
CRAP 4.0
« Meta/Named/List.pm ↑ Summary Meta/File.pm »
1 #
2 # @author Bodo (Hugo) Barwich
3 # @version 2026-01-29
4 # @package Object::Meta
5 # @subpackage lib/Object/Meta.pm
6  
7 # This Module defines Classes to manage Data in an indexed List
8 #
9 #---------------------------------
10 # Requirements:
11 #
12 #---------------------------------
13 # Features:
14 #
15  
16 #==============================================================================
17 # The Object::Meta Package
18  
19 =head1 NAME
20  
21 Object::Meta - Library to manage raw data and meta data as one object but keeping it separate
22  
23 =cut
24  
25 package Object::Meta;
26  
27 7 our $VERSION = '1.2.1';
28  
29 #----------------------------------------------------------------------------
30 #Dependencies
31  
32 7 ▶use constant LIST_DATA => 0;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 7 ▶use constant LIST_META_DATA => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 DESCRIPTION
36  
37 C<Object::Meta> implements a class to manage raw data and additional meta data as an object
38  
39 Of special importance is the B<Index Field> which is use to create an automatical index
40 in the C<Object::Meta::List>.
41  
42 It does not require lengthly creation of definition modules.
43  
44 =cut
45  
46 #----------------------------------------------------------------------------
47 #Constructors
48  
49 =head1 METHODS
50  
51 =head2 Constructor
52  
53 =head3 new ( [ DATA ] )
54  
55 This is the constructor for a new C<Object::Meta> object.
56  
57 B<Parameters:>
58  
59 =over 4
60  
61 =item C<DATA>
62  
63 The B<raw data> which is passed in a hash like fashion,
64 using key and value pairs.
65  
66 =back
67  
68 =cut
69  
70 58 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
71 58 ▶ my $class = ref( $_[0] ) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0580
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table33%1/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
72  
73 #Set the Default Attributes and assign the initial Values
74 58 my $self = [ {}, {} ];
75  
76 #Bestow Objecthood
77 58 bless $self, $class;
78  
79 58 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 35 23 58
80  
81 #Parameters are a Key / Value List
82 35 Object::Meta::set( $self, @_[ 1 .. $#_ ] );
83 }
84  
85 58 return $self;
86 }
87  
88 58 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
89 58 my $self = $_[0];
90  
91 #Free the Lists
92 58 $self->[LIST_DATA] = ();
93 58 $self->[LIST_META_DATA] = ();
94 }
95  
96 #----------------------------------------------------------------------------
97 #Administration Methods
98  
99 =head2 Administration Methods
100  
101 =head3 set ( DATA )
102  
103 This method will populate the B<raw Data Fields> with values.
104  
105 B<Parameters:>
106  
107 =over 4
108  
109 =item C<DATA>
110  
111 A list which is passed in a hash like fashion, using key and value pairs.
112  
113 =back
114  
115 =cut
116  
117 89 ▶sub set {
Subroutine set: called CC=3 SCAR=11.1
Pod documented
118 89 my ( $self, %hshprms ) = @_;
119  
120 89 foreach ( keys %hshprms ) {
121  
122 #The Field Name must not be empty
123 153 ▶ if ( $_ ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "") 153 0 153
124 153 $self->[LIST_DATA]{$_} = $hshprms{$_};
125 }
126 }
127 }
128  
129 =head3 setMeta ( DATA )
130  
131 This method will assign values to B<Meta Data Fields>.
132  
133 B<Parameters:>
134  
135 =over 4
136  
137 =item C<DATA>
138  
139 A list which is passed in a hash like fashion, using key and value pairs.
140  
141 =back
142  
143 =cut
144  
145 151 ▶sub setMeta {
Subroutine setMeta: called CC=3 SCAR=11.1
Pod documented
146 151 my ( $self, %hshprms ) = @_;
147  
148 151 foreach ( keys %hshprms ) {
149  
150 #The Field Name must not be empty
151 157 ▶ if ( $_ ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ ne "") 157 0 157
152 157 $self->[LIST_META_DATA]{$_} = $hshprms{$_};
153 }
154 }
155 }
156  
157 =head3 setIndexField ( INDEX_FIELD )
158  
159 This method configure the B<Index Field> for this object.
160  
161 B<Parameters:>
162  
163 =over 4
164  
165 =item C<INDEX_FIELD>
166  
167 The name of the Field which contains the Value by which the object
168 will be indexed.
169  
170 =back
171  
172 =cut
173  
174 42 ▶sub setIndexField {
Subroutine setIndexField: called CC=2 SCAR=7.1
Pod documented
175 42 my ( $self, $sindexfield ) = @_;
176  
177 42 ▶ if ( defined $sindexfield ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sindexfield) 42 0 42
178 42 Object::Meta::setMeta( $self, 'indexfield', $sindexfield );
179 }
180  
181 }
182  
183 =head3 setIndexValue ( INDEX_VALUE )
184  
185 This Method assigns the value for the B<Index Field> for this object.
186  
187 B<Parameters:>
188  
189 =over 4
190  
191 =item C<INDEX_VALUE>
192  
193 The scalar value of the Field by which the object will be indexed.
194  
195 =back
196  
197 =cut
198  
199 0 ▶sub setIndexValue {
Subroutine setIndexValue: not called CC=3 SCAR=24.8
Pod documented
200 0 my ( $self, $sindexvalue ) = @_;
201 0 my $sindexfield = Object::Meta::getIndexField $self;
202  
203 0 ▶ if ( defined $sindexvalue
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sindexvalue and $sindexfield ne "") 0 0 0
Condition0%0/3
defined $sindexvalue and $sindexfield ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $sindexvalue and $sindexfield ne ""
defined $sindexvalue $sindexfield ne ""
Truth table0%0/3
A and B
A= defined $sindexvalueB= $sindexfield ne ""
ABresult
0X0
100
111
204 && $sindexfield ne '' )
205 {
206 0 Object::Meta::set( $self, $sindexfield, $sindexvalue );
207 }
208 }
209  
210 =head3 Clear ()
211  
212 This method removes all raw data and meta data.
213  
214 Only the index field configuration is still kept.
215  
216 See L<Method C<setIndexField()>|/"setIndexField ( INDEX_FIELD )">
217  
218 =cut
219  
220 1 ▶sub Clear {
Subroutine Clear: called CC=1 SCAR=0.0
Pod documented
221 1 my $self = $_[0];
222  
223 #Preserve Index Configuration
224 1 my $sindexfield = Object::Meta::getIndexField $self;
225  
226 1 $self->[LIST_DATA] = ();
227 1 $self->[LIST_META_DATA] = ();
228  
229 #Restore Index Configuration
230 1 Object::Meta::setIndexField $self, $sindexfield;
231 }
232  
233 #----------------------------------------------------------------------------
234 #Consultation Methods
235  
236 =head2 Consultation Methods
237  
238 =head3 get ( FIELD_NAME [, DEFAULT_VALUE [. IS_META ] ] )
239  
240 This Method retrieves the value of the field with name C<FIELD_NAME> for this object.
241 It can be a B<Physical Field> or a B<Meta Field>.
242  
243 B<Parameters:>
244  
245 =over 4
246  
247 =item C<FIELD_NAME>
248  
249 The name of the Field which value it must return.
250  
251 =item C<DEFAULT_VALUE>
252  
253 The default value to return if the Field does not exist.
254 (Otherwise it would return C<undef>)
255  
256 =item C<IS_META>
257  
258 whether the C<FIELD_NAME> is a B<Meta Field>.
259  
260 =back
261  
262 =cut
263  
264 121 ▶sub get {
Subroutine get: called CC=5 SCAR=16.3
Pod documented
265 121 my ( $self, $sfieldname, $sdefault, $imta ) = @_;
266 121 my $srs = $sdefault;
267  
268 121 ▶ unless ($imta) {
Branch: 1 branch
BranchTrueFalseTotal
if ($imta) { } 13 108 121
269 108 ▶ if ( defined $sfieldname
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sfieldname and $sfieldname ne "") 108 0 108
Condition33%1/3
defined $sfieldname and $sfieldname ne ""
!ll&&!rl&&r
00108
MC/DC0%0/2
defined $sfieldname and $sfieldname ne ""
defined $sfieldname $sfieldname ne ""
Truth table33%1/3
A and B
A= defined $sfieldnameB= $sfieldname ne ""
ABresult
0X0
100
111
270 && $sfieldname ne '' )
271 {
272 108 ▶ if ( exists $self->[LIST_DATA]{$sfieldname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->[0]{$sfieldname}) { } 105 3 108
273 105 $srs = $self->[LIST_DATA]{$sfieldname};
274 }
275 else {
276 #Check as Meta Field
277 3 $srs = Object::Meta::getMeta( $self, $sfieldname, $sdefault );
278 }
279 }
280 }
281 else #A Meta Field is requested
282 {
283 #Check a Meta Field
284 13 $srs = Object::Meta::getMeta( $self, $sfieldname, $sdefault );
285 }
286  
287 121 return $srs;
288 }
289  
290 =head3 getMeta ( FIELD_NAME [, DEFAULT_VALUE ] )
291  
292 This Method retrieves the value of the B<Meta Field> with name C<FIELD_NAME> for this object.
293  
294 B<Parameters:>
295  
296 =over 4
297  
298 =item C<FIELD_NAME>
299  
300 The name of the Field which value it must return.
301  
302 =item C<DEFAULT_VALUE>
303  
304 The default value to return if the Field does not exist.
305 (Otherwise it would return C<undef>)
306  
307 =back
308  
309 =cut
310  
311 201 ▶sub getMeta {
Subroutine getMeta: called CC=4 SCAR=14.5
Pod documented
312 201 my ( $self, $sfieldname, $sdefault ) = @_;
313 201 my $srs = $sdefault;
314  
315 201 ▶ if ( defined $sfieldname
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sfieldname and $sfieldname ne "") 201 0 201
Condition33%1/3
defined $sfieldname and $sfieldname ne ""
!ll&&!rl&&r
00201
MC/DC0%0/2
defined $sfieldname and $sfieldname ne ""
defined $sfieldname $sfieldname ne ""
Truth table33%1/3
A and B
A= defined $sfieldnameB= $sfieldname ne ""
ABresult
0X0
100
111
316 && $sfieldname ne '' )
317 {
318 $srs = $self->[LIST_META_DATA]{$sfieldname}
319 201 ▶ if ( exists $self->[LIST_META_DATA]{$sfieldname} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->[1]{$sfieldname} 163 38 201
320  
321 }
322  
323 201 return $srs;
324 }
325  
326 =head3 getIndexField ()
327  
328 This method retrieves the Name of the B<Index Field> with the object will be indexed.
329  
330 The name of the B<Index Field> is a B<Meta Field> which is stored separately.
331  
332 B<Returns:> The Name of the B<Index Field> or an empty String if the Field is not set.
333  
334 =cut
335  
336 23 ▶sub getIndexField {
Subroutine getIndexField: called CC=1 SCAR=0.0
Pod documented
337 23 return Object::Meta::getMeta( $_[0], 'indexfield', '' );
338 }
339  
340 =head3 getIndexValue ()
341  
342 This method retrieves the Value of the B<Index Field> by which the object will be indexed.
343  
344 =cut
345  
346 17 ▶sub getIndexValue {
Subroutine getIndexValue: called CC=1 SCAR=0.0
Pod documented
347 17 my $sindexfield = Object::Meta::getIndexField $_[0];
348  
349 #print "idx fld: '$sindexfield'; idx vl: '" . $_[0]->get($sindexfield) . "'\n";
350  
351 17 return Object::Meta::get( $_[0], $sindexfield );
352 }
353  
354 7 return 1;
« Meta/Named/List.pm ↑ Summary Meta/File.pm »