File Coverage

lib/Object/Meta.pm
Criterion Covered Total %
statement 48 52 92.3
branch 13 20 65.0
condition 3 12 25.0
subroutine 12 13 92.3
pod 10 10 100.0
total 86 107 80.3


line stmt bran cond sub pod time code
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             our $VERSION = '1.2.0';
28              
29             #----------------------------------------------------------------------------
30             #Dependencies
31              
32 7     7   591470 use constant LIST_DATA => 0;
  7         17  
  7         916  
33 7     7   56 use constant LIST_META_DATA => 1;
  7         35  
  7         7263  
34              
35             =head1 DESCRIPTION
36              
37             C implements a class to manage raw data and additional meta data as an object
38              
39             Of special importance is the B which is use to create an automatical index
40             in the C.
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.
56              
57             B
58              
59             =over 4
60              
61             =item C
62              
63             The B which is passed in a hash like fashion,
64             using key and value pairs.
65              
66             =back
67              
68             =cut
69              
70             sub new {
71 58   33 58 1 257054 my $class = ref( $_[0] ) || $_[0];
72              
73             #Set the Default Attributes and assign the initial Values
74 58         136 my $self = [ {}, {} ];
75              
76             #Bestow Objecthood
77 58         134 bless $self, $class;
78              
79 58 100       171 if ( scalar(@_) > 1 ) {
80              
81             #Parameters are a Key / Value List
82 35         128 Object::Meta::set( $self, @_[ 1 .. $#_ ] );
83             }
84              
85 58         221 return $self;
86             }
87              
88             sub DESTROY {
89 58     58   22317 my $self = $_[0];
90              
91             #Free the Lists
92 58         143 $self->[LIST_DATA] = ();
93 58         839 $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 with values.
104              
105             B
106              
107             =over 4
108              
109             =item C
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             sub set {
118 89     89 1 1392 my ( $self, %hshprms ) = @_;
119              
120 89         192 foreach ( keys %hshprms ) {
121              
122             #The Field Name must not be empty
123 153 50       301 if ( $_ ne '' ) {
124 153         416 $self->[LIST_DATA]{$_} = $hshprms{$_};
125             }
126             }
127             }
128              
129             =head3 setMeta ( DATA )
130              
131             This method will assign values to B.
132              
133             B
134              
135             =over 4
136              
137             =item C
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             sub setMeta {
146 151     151 1 1536 my ( $self, %hshprms ) = @_;
147              
148 151         363 foreach ( keys %hshprms ) {
149              
150             #The Field Name must not be empty
151 157 50       398 if ( $_ ne '' ) {
152 157         615 $self->[LIST_META_DATA]{$_} = $hshprms{$_};
153             }
154             }
155             }
156              
157             =head3 setIndexField ( INDEX_FIELD )
158              
159             This method configure the B for this object.
160              
161             B
162              
163             =over 4
164              
165             =item C
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             sub setIndexField {
175 42     42 1 1182 my ( $self, $sindexfield ) = @_;
176              
177 42 50       148 if ( defined $sindexfield ) {
178 42         93 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 for this object.
186              
187             B
188              
189             =over 4
190              
191             =item C
192              
193             The scalar value of the Field by which the object will be indexed.
194              
195             =back
196              
197             =cut
198              
199             sub setIndexValue {
200 0     0 1 0 my ( $self, $sindexvalue ) = @_;
201 0         0 my $sindexfield = Object::Meta::getIndexField $self;
202              
203 0 0 0     0 if ( defined $sindexvalue
204             && $sindexfield ne '' )
205             {
206 0         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|/"setIndexField ( INDEX_FIELD )">
217              
218             =cut
219              
220             sub Clear {
221 1     1 1 2 my $self = $_[0];
222              
223             #Preserve Index Configuration
224 1         5 my $sindexfield = Object::Meta::getIndexField $self;
225              
226 1         3 $self->[LIST_DATA] = ();
227 1         4 $self->[LIST_META_DATA] = ();
228              
229             #Restore Index Configuration
230 1         3 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 for this object.
241             It can be a B or a B.
242              
243             B
244              
245             =over 4
246              
247             =item C
248              
249             The name of the Field which value it must return.
250              
251             =item C
252              
253             The default value to return if the Field does not exist.
254             (Otherwise it would return C)
255              
256             =item C
257              
258             whether the C is a B.
259              
260             =back
261              
262             =cut
263              
264             sub get {
265 121     121 1 28694 my ( $self, $sfieldname, $sdefault, $imta ) = @_;
266 121         171 my $srs = $sdefault;
267              
268 121 100       270 unless ($imta) {
269 108 50 33     428 if ( defined $sfieldname
270             && $sfieldname ne '' )
271             {
272 108 100       233 if ( exists $self->[LIST_DATA]{$sfieldname} ) {
273 105         211 $srs = $self->[LIST_DATA]{$sfieldname};
274             }
275             else {
276             #Check as Meta Field
277 3         15 $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         52 $srs = Object::Meta::getMeta( $self, $sfieldname, $sdefault );
285             }
286              
287 121         368 return $srs;
288             }
289              
290             =head3 getMeta ( FIELD_NAME [, DEFAULT_VALUE ] )
291              
292             This Method retrieves the value of the B with name C for this object.
293              
294             B
295              
296             =over 4
297              
298             =item C
299              
300             The name of the Field which value it must return.
301              
302             =item C
303              
304             The default value to return if the Field does not exist.
305             (Otherwise it would return C)
306              
307             =back
308              
309             =cut
310              
311             sub getMeta {
312 201     201 1 447 my ( $self, $sfieldname, $sdefault ) = @_;
313 201         286 my $srs = $sdefault;
314              
315 201 50 33     820 if ( defined $sfieldname
316             && $sfieldname ne '' )
317             {
318             $srs = $self->[LIST_META_DATA]{$sfieldname}
319 201 100       707 if ( exists $self->[LIST_META_DATA]{$sfieldname} );
320              
321             }
322              
323 201         449 return $srs;
324             }
325              
326             =head3 getIndexField ()
327              
328             This method retrieves the Name of the B with the object will be indexed.
329              
330             The name of the B is a B which is stored separately.
331              
332             B The Name of the B or an empty String if the Field is not set.
333              
334             =cut
335              
336             sub getIndexField {
337 23     23 1 4388 return Object::Meta::getMeta( $_[0], 'indexfield', '' );
338             }
339              
340             =head3 getIndexValue ()
341              
342             This method retrieves the Value of the B by which the object will be indexed.
343              
344             =cut
345              
346             sub getIndexValue {
347 17     17 1 1192 my $sindexfield = Object::Meta::getIndexField $_[0];
348              
349             #print "idx fld: '$sindexfield'; idx vl: '" . $_[0]->get($sindexfield) . "'\n";
350              
351 17         50 return Object::Meta::get( $_[0], $sindexfield );
352             }
353              
354             return 1;