Meta/Named.pm

filter: Statementstmt 100% 27 / 27 Branchbran 87.5% 7 / 8 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 6 / 6 Podpod 100% 4 / 4 totaltotal 90.0% 45 / 50 CC 6 SCAR 8.3
CC
6
Cov
92%
new
3.0
setName
3.0
set
2.0
CRAP 2.3
« Meta/File.pm ↑ Summary
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<name> field from the raw data
23  
24 =cut
25  
26 package Object::Meta::Named;
27  
28 #----------------------------------------------------------------------------
29 #Dependencies
30  
31 5 ▶use parent 'Object::Meta';
Subroutine BEGIN: called CC=1 SCAR=0.0
32  
33 5 ▶use Digest::MD5 qw(md5_hex);
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 =head1 DESCRIPTION
36  
37 C<Object::Meta::Named> implements a class which adds a C<name> field to the raw data which
38 can be used to index the C<Object::Meta> entries.
39  
40 Additionally a C<hash> meta data field will be created for indexation and lookup.
41  
42 The C<hash> 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::Meta::Named> object.
56 It creates a new object from B<raw data> which is passed in a hash key / value pairs.
57  
58 B<Parameters:>
59  
60 =over 4
61  
62 =item C<DATA>
63  
64 The B<raw data> which is passed in a hash key / value pairs.
65  
66 If a C<name> field is present it will be used to index the entry.
67  
68 =back
69  
70 =cut
71  
72 16 ▶sub new {
Subroutine new: called CC=3 SCAR=11.1
Pod documented
73 16 ▶ my $class = ref( $_[0] ) || $_[0];
Condition33%1/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
0160
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
74  
75 #Take the Method Parameters
76 16 my %hshprms = @_[ 1 .. $#_ ];
77  
78 16 my $self = $class->SUPER::new(%hshprms);
79  
80 #Set the Primary Index Field
81 16 Object::Meta::setIndexField( $self, 'hash' );
82  
83 16 ▶ if ( defined $hshprms{'name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hshprms{'name'}) { } 10 6 16
84 10 Object::Meta::Named::setName( $self, $hshprms{'name'} );
85 }
86 else {
87  
88 # Create empty 'name' and 'hash' fields
89 6 Object::Meta::Named::setName $self;
90 }
91  
92 16 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<Object::Meta::set()> to recognize the C<name> field.
103  
104 See L<Method C<Object::Meta::set()>|Object::Meta/"set ( DATA )">
105  
106 =cut
107  
108 2 ▶sub set {
Subroutine set: called CC=2 SCAR=7.0
Pod documented
109 2 my ( $self, %hshprms ) = @_;
110  
111 2 ▶ if ( defined $hshprms{'name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $hshprms{"name"}) 2 0 2
112 2 Object::Meta::Named::setName( $self, delete $hshprms{'name'} );
113 }
114  
115 2 Object::Meta::set( $self, %hshprms );
116 }
117  
118 =head3 setName ( [ NAME ] )
119  
120 This will create a C<name> field in the raw data and index the object by the hash of it.
121  
122 B<Parameters:>
123  
124 =over 4
125  
126 =item C<NAME>
127  
128 The string value for the name of the object.
129  
130 If a C<NAME> is empty or is undefined it will empty the C<name> field and
131 the C<hash> meta data field.
132  
133 =back
134  
135 =cut
136  
137 20 ▶sub setName {
Subroutine setName: called CC=3 SCAR=11.0
Pod documented
138 20 my $self = $_[0];
139  
140 20 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 14 6 20
141 14 Object::Meta::set( $self, 'name', $_[1] );
142 }
143 else {
144 6 Object::Meta::set( $self, 'name', '' );
145 }
146  
147 20 ▶ if ( $self->[Object::Meta::LIST_DATA]{'name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->[0]{'name'} ne '') { } 14 6 20
148 14 Object::Meta::set( $self, 'hash', md5_hex( $self->[Object::Meta::LIST_DATA]{'name'} ) );
149 }
150 else {
151 6 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<name> field.
163  
164 =cut
165  
166 10 ▶sub getName {
Subroutine getName: called CC=1 SCAR=0.0
Pod documented
167 10 return $_[0]->[Object::Meta::LIST_DATA]{'name'};
168 }
169  
170 5 return 1;
« Meta/File.pm ↑ Summary