File Coverage

lib/Object/Meta/Named.pm
Criterion Covered Total %
statement 26 26 100.0
branch 7 8 87.5
condition 1 3 33.3
subroutine 6 6 100.0
pod 4 4 100.0
total 44 47 93.6


line stmt bran cond sub pod time code
1             # @author Bodo (Hugo) Barwich
2             # @version 2026-01-25
3             # @package Indexed List by Name
4             # @subpackage classes_metanames.pm
5              
6             # This Module defines Classes to manage Data indexed in a List by its Name
7             #
8             #---------------------------------
9             # Requirements:
10             # - The Perl Package "perl-Digest-MD5" must be installed
11             # - The Perl Package "perl-Data-Dump" must be installed
12             #
13             #---------------------------------
14             # Features:
15             #
16              
17             #==============================================================================
18             # The Object::Meta::Named Package
19              
20             =head1 NAME
21              
22             Object::Meta::Named - Library to recognise a special C field from the raw data
23              
24             =cut
25              
26             package Object::Meta::Named;
27              
28             #----------------------------------------------------------------------------
29             #Dependencies
30              
31 5     5   159670 use parent 'Object::Meta';
  5         369  
  5         55  
32              
33 5     5   401 use Digest::MD5 qw(md5_hex);
  5         9  
  5         2571  
34              
35             =head1 DESCRIPTION
36              
37             C implements a class which adds a C field to the raw data which
38             can be used to index the C entries.
39              
40             Additionally a C meta data field will be created for indexation and lookup.
41              
42             The C meta data field becomes the index field.
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             It creates a new object from B which is passed in a hash key / value pairs.
57              
58             B
59              
60             =over 4
61              
62             =item C
63              
64             The B which is passed in a hash key / value pairs.
65              
66             If a C field is present it will be used to index the entry.
67              
68             =back
69              
70             =cut
71              
72             sub new {
73 16   33 16 1 303606 my $class = ref( $_[0] ) || $_[0];
74              
75             #Take the Method Parameters
76 16         64 my %hshprms = @_[ 1 .. $#_ ];
77              
78 16         102 my $self = $class->SUPER::new(%hshprms);
79              
80             #Set the Primary Index Field
81 16         52 Object::Meta::setIndexField( $self, 'hash' );
82              
83 16 100       54 if ( defined $hshprms{'name'} ) {
84 10         34 Object::Meta::Named::setName( $self, $hshprms{'name'} );
85             }
86             else {
87              
88             # Create empty 'name' and 'hash' fields
89 6         26 Object::Meta::Named::setName $self;
90             }
91              
92 16         59 return $self;
93             }
94              
95             #----------------------------------------------------------------------------
96             #Administration Methods
97              
98             =head2 Administration Methods
99              
100             =head3 set ( DATA )
101              
102             This overrides the base method C to recognize the C field.
103              
104             See L|Object::Meta/"set ( DATA )">
105              
106             =cut
107              
108             sub set {
109 2     2 1 1088 my ( $self, %hshprms ) = @_;
110              
111 2 50       8 if ( defined $hshprms{'name'} ) {
112 2         6 Object::Meta::Named::setName( $self, delete $hshprms{'name'} );
113             }
114              
115 2         8 Object::Meta::set( $self, %hshprms );
116             }
117              
118             =head3 setName ( [ NAME ] )
119              
120             This will create a C field in the raw data and index the object by the hash of it.
121              
122             B
123              
124             =over 4
125              
126             =item C
127              
128             The string value for the name of the object.
129              
130             If a C is empty or is undefined it will empty the C field and
131             the C meta data field.
132              
133             =back
134              
135             =cut
136              
137             sub setName {
138 20     20 1 1862 my $self = $_[0];
139              
140 20 100       47 if ( scalar(@_) > 1 ) {
141 14         35 Object::Meta::set( $self, 'name', $_[1] );
142             }
143             else {
144 6         17 Object::Meta::set( $self, 'name', '' );
145             }
146              
147 20 100       53 if ( $self->[Object::Meta::LIST_DATA]{'name'} ne '' ) {
148 14         88 Object::Meta::set( $self, 'hash', md5_hex( $self->[Object::Meta::LIST_DATA]{'name'} ) );
149             }
150             else {
151 6         22 Object::Meta::set( $self, 'hash', '' );
152             }
153             }
154              
155             #----------------------------------------------------------------------------
156             #Consultation Methods
157              
158             =head2 Consultation Methods
159              
160             =head3 getName ()
161              
162             Returns the content of the C field.
163              
164             =cut
165              
166             sub getName {
167 10     10 1 2510 return $_[0]->[Object::Meta::LIST_DATA]{'name'};
168             }
169              
170             return 1;