Toolkit/Entry.pm

filter: Statementstmt 91.0% 183 / 201 Branchbran 66.4% 109 / 164 Conditioncond 50.0% 6 / 12 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 90.4% 19 / 21 Podpod 92.8% 13 / 14 totaltotal 78.8% 331 / 420 CC 89 SCAR 22.7
CC
89
Cov
79%
comparator
20.4
add
12.5
attributes
12.0
CRAP 9.7
« Toolkit.pm ↑ Summary Toolkit/Connector/CSV.pm »
1 #!/usr/bin/perl -w
2 #
3 # Data::Toolkit::Entry
4 #
5 # Andrew Findlay
6 # Nov 2006
7 # andrew.findlay@skills-1st.co.uk
8 #
9 # $Id: Entry.pm 388 2013-08-30 15:19:23Z remotesvn $
10  
11 package Data::Toolkit::Entry;
12  
13 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶use Data::MultiValuedHash;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =head1 NAME
20  
21 Data::Toolkit::Entry
22  
23 =head1 DESCRIPTION
24  
25 Data::Toolkit::Entry objects store attribute-value data.
26 Attributes can have zero or more values.
27 By default, attribute names are case-insensitive and are always
28 returned in lower case.
29  
30 Each attribute can have zero or more values.
31 The list of values is kept sorted, and by default only one copy of each value
32 is permitted. The sort order is selectable.
33  
34 Data::Toolkit::Entry objects are ideal for carrying entries in
35 directory synchronisation systems and other data-pump applications.
36  
37 =head1 DEPENDENCIES
38  
39 Carp
40 Clone
41 Data::Dumper
42 Data::MultiValuedHash
43  
44 =head1 SYNOPSIS
45  
46 my $entry = Data::Toolkit::Entry->new();
47  
48 $count = $entry->set("surname", [ "Findlay" ]);
49 $count = $entry->set("cn", [ "Andrew Findlay", "A J Findlay" ]);
50 $count = $entry->set("mobile", []);
51  
52 $count = $entry->add("newAttribute", [ "Apples" ]);
53 $count = $entry->add("newAttribute", [ "Pears", "Oranges" ]);
54  
55 $arrayRef = $entry->get("myAttribute");
56 print (join ":", $arrayref), "\n";
57  
58 $arrayRef = $entry->attributes();
59  
60 $result = $entry->attribute_match( 'attribname', ['value1','value2'] );
61  
62 $newEntry = $entry->map($thisMap);
63 $newEntry = $entry->map($thisMap,$entry2,$entry3,...);
64  
65 $result = $entry->delete('thisAttribute','thisValue');
66  
67 $result = $entry->delete('thisAttribute');
68  
69 my $currentDebugLevel = Data::Toolkit::Entry->debug();
70 my $newDebugLevel = Data::Toolkit::Entry(1);
71  
72 my $string = $entry->dump();
73  
74 =cut
75  
76 ########################################################################
77 # Package globals
78 ########################################################################
79  
80 6 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 6 $VERSION = '1.0';
82  
83 # Set this non-zero for debug logging
84 #
85 6 my $debug = 0;
86  
87 ########################################################################
88 # Constructors and destructors
89 ########################################################################
90  
91 =head1 Constructor
92  
93 =head2 new
94  
95 my $entry = Data::Toolkit::Entry->new();
96 my $entry = Data::Toolkit::Entry->new( {configAttrib => value, ....} );
97  
98 Creates an object of type Data::Toolkit::Entry
99  
100 Optionally accepts a hash of configuration items chosen from this list:
101  
102 =over
103  
104 =item caseSensitiveNames
105  
106 If this is defined with a true value then attribute names are case-sensitive.
107 By default they are not, so "Surname", "surname", and "SurName" are all the same attribute.
108  
109 =item defaultValueComparator
110  
111 If this is defined its value sets the default method of comparing values
112 in all attributes. See I<comparator> below for details.
113  
114 =item defaultUniqueValues
115  
116 If this is defined its value is the default for each new attribute's
117 uniqueValues flag.
118  
119 =back
120  
121 =cut
122  
123 25 ▶sub new {
Subroutine new: called CC=7 SCAR=19.5
Pod documented
124 25 my $class = shift;
125 25 my $configParam = shift;
126  
127 25 my $self = {};
128  
129 # Take a copy of the config hash
130 # - we don't want to store a ref to the one we were given
131 # in case it is part of another object
132 #
133 25 ▶ if (defined($configParam)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $configParam) { } 7 18 25
134 7 ▶ if ((ref $configParam) ne 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $configParam ne "HASH") 0 7 7
135 0 croak "Data::Toolkit::Entry->new expects a hash ref but was given something else"
136 }
137  
138 7 $self->{config} = clone($configParam);
139 }
140 else {
141 # Start with empty config
142 18 $self->{config} = {};
143 18 $self->{config}->{uniqueValues} = {};
144 18 $self->{config}->{comparator} = {};
145 }
146  
147 # Default value comparison method
148 25 ▶ $self->{config}->{defaultValueComparator} = 'caseInsensitive' unless $self->{config}->{defaultValueComparator};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"defaultValueComparator"} 20 5 25
149  
150 # Default true for uniqueValues flags
151 25 ▶ $self->{config}->{defaultUniqueValues} = 1 unless $self->{config}->{defaultUniqueValues};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"defaultUniqueValues"} 20 5 25
152  
153 # Attribute names are not case-sensitive by default.
154 # Check to see whether this should be changed.
155 25 my $ignoreCase = 1;
156 25 ▶ $ignoreCase = 0 if $self->{config}->{caseSensitiveNames};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"config"}{"caseSensitiveNames"} 1 24 25
157  
158 # We use Data::MultiValuedHash to handle attribute-value storage
159 25 $self->{data} = Data::MultiValuedHash->new($ignoreCase);
160  
161 25 bless ($self, $class);
162  
163 25 ▶ carp "Creating $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 25 25
164 25 return $self;
165 }
166  
167 25 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.2
168 25 my $self = shift;
169 25 ▶ carp "Destroying $self" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 25 25
170 }
171  
172 ########################################################################
173 # Methods
174 ########################################################################
175  
176 =head1 Methods
177  
178 =cut
179  
180 ########################################
181  
182 =head2 set
183  
184 Set the value of an attribute, overriding what was there before.
185 Creates the attribute if necessary.
186  
187 Passing an empty list of values creates an empty attribute
188 (this is different from the attribute not existing at all).
189  
190 Passing an undef list of values deletes the attribute and returns undef.
191  
192 $count = $entry->set("surname", [ "Findlay" ]);
193 $count = $entry->set("cn", [ "Andrew Findlay", "A J Findlay" ]);
194 $count = $entry->set("mobile", []);
195  
196 The method returns the number of values that the attribute has, so in the
197 examples above, $count would be 1, 2, and 0 respectively.
198  
199 =cut
200  
201 48 ▶sub set {
Subroutine set: called CC=6 SCAR=18.4
Pod documented
202 48 my $self = shift;
203 48 my $attrib = shift;
204 48 my $values = shift;
205  
206 48 ▶ croak "set requires an attribute name" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 48 48
207  
208 # Lower-case the attribute name if necessary
209 48 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 48 0 48
210  
211 48 ▶ carp "Data::Template::Entry->set attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 48 48
212  
213 # Delete any existing values
214 48 $self->{data}->delete($attrib);
215  
216 # undefined list?
217 48 ▶ return undef if !defined($values);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 1 47 48
218  
219 47 ▶ croak "Second parameter to Data::Template::Entry->set must be an array" if ((ref $values) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $values ne "ARRAY" 0 47 47
220  
221 # Pass the rest of the job to the add method
222 47 return $self->add($attrib, $values);
223 }
224  
225  
226 ########################################
227  
228 =head2 add
229  
230 Add one or more values to an attribute.
231 Creates the attribute if necessary.
232  
233 Passing an undef list of values does nothing.
234  
235 Passing an empty list of values creates an empty attribute
236 or leaves an existing one unchanged.
237  
238 $count = $entry->add("newAttribute", [ "Apples" ]);
239 $count = $entry->add("newAttribute", [ "Pears", "Oranges" ]);
240 $count = $entry->add("anotherAttribute", []);
241 $count = $entry->add("anotherAttribute", undef);
242  
243 The method returns the number of values that the attribute has after
244 the add operation has completed. If an undef list is added to a
245 non-existant attribute then the return will be undef.
246  
247 =cut
248  
249 89 ▶sub add {
Subroutine add: called CC=12 SCAR=25.3
Pod documented
250 89 my $self = shift;
251 89 my $attrib = shift;
252 89 my $values = shift;
253  
254 89 ▶ croak "add requires an attribute name" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 89 89
255  
256 # Lower-case the attribute name if necessary
257 89 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 87 2 89
258  
259 89 ▶ carp "Data::Template::Entry->add attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 89 89
260  
261 # If we were given an undef value list then
262 # we dont want to modify the entry at all.
263 #
264 89 ▶ return $self->{data}->count($attrib) if !defined($values);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $values 1 88 89
265  
266 88 ▶ carp "Data::Template::Entry->add $attrib: " . ref $values if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 88 88
267 88 ▶ croak "Second parameter to Data::Template::Entry->add must be an array ref" if ((ref $values) ne "ARRAY");
Branch: 1 branch
BranchTrueFalseTotal
if ref $values ne "ARRAY" 0 88 88
268  
269 # Set the comparator type if it has not been done already
270 88 ▶ if (!defined($self->{config}->{comparator}->{$attrib})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"comparator"}{$attrib}) 61 27 88
271 61 ▶ carp "Data::Template::Entry->add setting attribute '$attrib' default comparator" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 61 61
272 61 $self->{config}->{comparator}->{$attrib} = $self->{config}->{defaultValueComparator};
273 }
274  
275 # Set the uniqueValues flag if it has not been done already
276 88 ▶ if (!defined($self->{config}->{uniqueValues}->{$attrib})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"config"}{"uniqueValues"}{$attrib}) 62 26 88
277 62 ▶ carp "Data::Template::Entry->add setting attribute '$attrib' default uniqueValues flag" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 62 62
278 62 $self->{config}->{uniqueValues}->{$attrib} = $self->{config}->{defaultUniqueValues};
279 }
280  
281 # If we get this far we should at least create the attribute
282 88 $self->{data}->push($attrib);
283  
284 # Add each value from the list
285 88 foreach my $val (@$values) {
286 101 $self->addOne( $attrib, $val );
287 }
288  
289 # Return the new number of values
290 88 return $self->{data}->count($attrib);
291 }
292  
293  
294 ########################################
295  
296 =head2 attrCmp
297  
298 Compare two values of a specific attribute using the defined comparator for that attribute.
299 Returns negative, zero, or positive.
300  
301 $result = $entry->attrCmp( 'attributename', 'value1', 'value2' );
302  
303 =cut
304  
305 43 ▶sub attrCmp {
Subroutine attrCmp: called CC=8 SCAR=21.0
Pod documented
306 43 my $self = shift;
307 43 my $attrib = shift;
308 43 my $val1 = shift;
309 43 my $val2 = shift;
310  
311 43 ▶ croak "Data::Template::Entry->attrCmp needs an attribute name" if !$attrib;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 43 43
312  
313 # Lower-case the attribute name if necessary
314 43 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 41 2 43
315  
316 # Find the comparator for this attribute
317 43 my $comparator = $self->{config}->{comparator}->{$attrib};
318 43 ▶ carp "Data::Template::Entry->attrCmp comparator: $comparator" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 43 43
319  
320 43 ▶ if ($comparator eq 'caseSensitive') {
Branch: 4 branches
BranchTrueFalseTotal
if ($comparator eq 'caseSensitive') { } 6 37 43
elsif ($comparator eq 'caseInsensitive') { } 27 10 37
elsif ($comparator eq 'integer') { } 8 2 10
elsif (ref $comparator eq 'CODE') { } 2 0 2
321 6 return ($val1 cmp $val2);
322 }
323 elsif ($comparator eq 'caseInsensitive') {
324 27 return ("\L$val1" cmp "\L$val2");
325 }
326 elsif ($comparator eq 'integer') {
327 8 return ($val1 <=> $val2);
328 }
329 elsif ((ref $comparator) eq 'CODE') {
330 2 return (&$comparator($val1, $val2));
331 }
332 else {
333 0 croak "comparator $comparator not implemented";
334 }
335 }
336  
337 # Internal method: add one value to an attribute, preserving the sort order and uniqueness
338 #
339 101 ▶sub addOne {
Subroutine addOne: called CC=7 SCAR=19.7
Pod undocumented
340 101 my $self = shift;
341 101 my $attrib = shift;
342 101 my $value = shift;
343  
344 101 ▶ carp "Data::Template::Entry->addOne( $attrib, $value )" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 101 101
345  
346 # Get the current list of values
347 101 my @list = $self->{data}->fetch($attrib);
348  
349 # Find the uniqueness flag for this attribute
350 101 my $uniq = $self->{config}->{uniqueValues}->{$attrib};
351 101 ▶ carp "Data::Template::Entry->addOne uniqueValues: $uniq" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 101 101
352  
353 # Work out where to put our new one
354 101 my $splicePosition = 0;
355 101 ▶ while (defined(my $thisVal = $list[$splicePosition])) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $thisVal = $list[$splicePosition])) 38 89 127
356 38 my $cmp = attrCmp( $self, $attrib, $value, $thisVal );
357  
358 # Not there yet
359 38 ▶ next if $cmp > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cmp > 0 26 12 38
360  
361 # Duplicate - return now if we are preserving uniqueness
362 12 ▶ return if (($cmp == 0) and $uniq);
Branch: 1 branch
BranchTrueFalseTotal
if $cmp == 0 and $uniq 0 12 12
Condition66%2/3
$cmp == 0 and $uniq
!ll&&!rl&&r
1110
MC/DC0%0/2
$cmp == 0 and $uniq
$cmp == 0 $uniq
Truth table66%2/3
A and B
A= $cmp == 0B= $uniq
ABresult
0X0
100
111
363  
364 # Insert here
365 12 last;
366 }
367 continue {
368 26 $splicePosition++;
369 }
370  
371 # Insert the value
372 101 $self->{data}->splice($attrib,$splicePosition,0,$value);
373 }
374  
375 ########################################
376  
377 =head2 get
378  
379 Get the list of values for an attribute.
380  
381 Returns an empty list if the attribute exists but has no values.
382  
383 Returns undef if the attribute does not exist.
384  
385 $arrayRef = $entry->get("myAttribute");
386  
387 =cut
388  
389 97 ▶sub get {
Subroutine get: called CC=6 SCAR=18.0
Pod documented
390 97 my $self = shift;
391 97 my $attrib = shift;
392  
393 97 ▶ croak "get requires an attribute name" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 97 97
394  
395 # Lower-case the attribute name if necessary
396 97 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 95 2 97
397  
398 97 ▶ carp "Data::Template::Entry->get attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 97 97
399 97 my $valref = $self->{data}->fetch($attrib);
400 97 ▶ return undef if !$valref;
Branch: 1 branch
BranchTrueFalseTotal
unless $valref 19 78 97
401 78 my @values = @$valref;
402 78 ▶ return ( wantarray ? @values : \@values );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 19 59 78
403 }
404  
405 ########################################
406  
407 =head2 attributes
408  
409 Get the list of attributes in an entry.
410  
411 Returns an empty list if there are no attributes.
412  
413 Note that attributes can exist but not have values.
414  
415 $arrayRef = $entry->attributes();
416  
417 =cut
418  
419 0 ▶sub attributes {
Subroutine attributes: not called CC=3 SCAR=24.8
Pod documented
420 0 my $self = shift;
421  
422 0 my @attrs = $self->{data}->keys();
423 0 ▶ carp "Data::Template::Entry->attributes are: " . (join ',',@attrs) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
424 0 ▶ return ( wantarray ? @attrs : \@attrs );
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
425 }
426  
427  
428 ########################################
429  
430 =head2 attribute_match
431  
432 Return true or false depending on whether the named attribute contains
433 a list of values exactly matching the one supplied.
434  
435 $result = $entry->attribute_match( 'attribname', ['value1','value2'] );
436  
437 The supplied list must be sorted into the same order that Data::Toolkit::Entry uses.
438 This will automatically be done in the common case of comparing an attribute
439 in two entries:
440  
441 $result = $entry->attribute_match( 'name', $secondEntry->get('name') );
442  
443 An undef list is treated as if it were an empty list.
444  
445 =cut
446  
447 4 ▶sub attribute_match {
Subroutine attribute_match: called CC=10 SCAR=24.0
Pod documented
448 4 my $self = shift;
449 4 my $attrib = shift;
450 4 my $list = shift;
451  
452 4 ▶ croak "Data::Template::Entry->attribute_match needs an attribute name" if !$attrib;
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 4 4
453 4 ▶ carp "Data::Template::Entry->attribute_match $attrib" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 4 4
454  
455 # Lower-case the attribute name if necessary
456 4 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 4 0 4
457  
458 # Undef list of values is equivalent to an empty list
459 4 ▶ $list = [] if !$list;
Branch: 1 branch
BranchTrueFalseTotal
unless $list 1 3 4
460 4 my @supplied = @$list;
461 4 my @mine = $self->{data}->fetch($attrib);
462  
463 # Step through the lists comparing values
464 4 my $suppVal = shift @supplied;
465 4 my $myVal = shift @mine;
466 4 ▶ while ($suppVal and $myVal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($suppVal and $myVal) 5 3 8
Condition66%2/3
$suppVal and $myVal
!ll&&!rl&&r
305
MC/DC50%1/2
$suppVal and $myVal
$suppVal $myVal
Truth table66%2/3
A and B
A= $suppValB= $myVal
ABresult
0X0
100
111
467 5 ▶ return 0 if (attrCmp($self, $attrib, $suppVal, $myVal) != 0);
Branch: 1 branch
BranchTrueFalseTotal
if attrCmp($self, $attrib, $suppVal, $myVal) != 0 1 4 5
468  
469 # Match so far - get the next pair of values
470 4 $suppVal = shift @supplied;
471 4 $myVal = shift @mine;
472 }
473 # Match is good if no values left
474 3 ▶ return 1 if (!$suppVal and !$myVal);
Branch: 1 branch
BranchTrueFalseTotal
unless $suppVal or $myVal 3 0 3
Condition33%1/3
$suppVal or $myVal
l!l&&r!l&&!r
003
MC/DC0%0/2
$suppVal or $myVal
$suppVal $myVal
Truth table33%1/3
A or B
A= $suppValB= $myVal
ABresult
1X1
011
000
475 # One list still has a value so match is not made
476 0 return 0;
477 }
478  
479 ########################################
480  
481 =head2 uniqueValues
482  
483 Define whether an attribute should have unique values.
484  
485 By default, values are unique: an attribute will not store more than one copy
486 of a given value, which is compared using the I<comparator> method set for the
487 attribute.
488  
489 $uniqVal = $entry->uniqueValues( 'attributeName', 1 );
490 $uniqVal = $entry->uniqueValues( 'attributeName', 0 );
491 $uniqVal = $entry->uniqueValues( 'attributeName' );
492  
493 Setting an undefined value has no effect other than to return the current setting.
494  
495 Returns the setting of the uniqueValues flag.
496  
497 Note that changing this flag on an attribute which already has values
498 does I<not> affect those values.
499  
500 Passing a hash reference causes all existing uniqueValues flags to be replaced
501 by the values specified in the hash:
502  
503 $hashRef = $entry->uniqueValues( \%mySettings );
504  
505 =cut
506  
507 4 ▶sub uniqueValues {
Subroutine uniqueValues: called CC=7 SCAR=20.9
Pod documented
508 4 my $self = shift;
509 4 my $attrib = shift;
510 4 my $uniq = shift;
511  
512 4 ▶ croak "uniqueValues requires an attribute name or hash" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 4 4
513  
514 4 ▶ if ((ref $attrib) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrib eq "HASH") 0 4 4
515 # we have been given a complete config to override what we had before
516 0 my %newUV = %$attrib;
517 0 $self->{config}->{uniqueValues} = \%newUV;
518 }
519  
520 # Lower-case the attribute name if necessary
521 4 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 4 0 4
522  
523  
524 4 ▶ if (!defined($uniq)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $uniq) 2 2 4
525 2 ▶ carp "Data::Template::Entry->uniqueValues attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
526 2 return $self->{config}->{uniqueValues}->{$attrib};
527 }
528  
529 2 ▶ carp "Data::Template::Entry->uniqueValues attribute '$attrib': $uniq" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
530 2 return $self->{config}->{uniqueValues}->{$attrib} = $uniq;
531 }
532  
533 ########################################
534  
535 =head2 comparator
536  
537 Define how values should be compared for a particular attribute.
538  
539 By default, values are treated as case-insensitive text strings.
540  
541 $func = $entry->comparator( 'attributeName', 'caseIgnore' );
542 $func = $entry->comparator( 'attributeName', 'caseSensitive' );
543 $func = $entry->comparator( 'attributeName', 'integer' );
544 $func = $entry->comparator( 'attributeName', \&myComparatorFunction );
545 $func = $entry->comparator( 'attributeName' );
546  
547 If supplying a function of your own, it should be suitable for use in
548 Perl's "sort" operation: it should return an integer less than, equal to,
549 or greater than zero depending on whether its first argument is less than, equal to,
550 or greater than its second argument. Note that sort's $a,$b convention
551 should I<not> be used.
552  
553 Returns the name of the comparison method or a reference to a function
554 as appropriate.
555  
556 Note that changing this flag on an attribute which already has values
557 does I<not> affect those values.
558  
559 Passing a hash reference causes all existing comparator flags to be replaced
560 by the values specified in the hash:
561  
562 $hashRef = $entry->comparator( \%myHash );
563  
564 =cut
565  
566 5 ▶sub comparator {
Subroutine comparator: called CC=14 SCAR=30.2
Pod documented
567 5 my $self = shift;
568 5 my $attrib = shift;
569 5 my $comp = shift;
570  
571 5 ▶ croak "comparator requires an attribute name or hash" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 5 5
572  
573 5 ▶ if ((ref $attrib) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $attrib eq "HASH") 0 5 5
574 # we have been given a complete config to override what we had before
575 0 my %newCMP = %$attrib;
576 0 $self->{config}->{comparator} = \%newCMP;
577 }
578  
579 # Lower-case the attribute name if necessary
580 5 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 5 0 5
581  
582  
583 5 ▶ if (!defined($comp)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $comp) 2 3 5
584 2 ▶ carp "Data::Template::Entry->comparator attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
585 2 return $self->{config}->{comparator}->{$attrib};
586 }
587  
588 3 ▶ if ((ref $comp) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $comp eq "CODE") 1 2 3
589 # We have a procedure to register
590 1 ▶ carp "Data::Template::Entry->comparator attribute '$attrib' (CODE)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
591 1 return $self->{config}->{comparator}->{$attrib} = $comp;
592 }
593  
594 2 ▶ if ("\L$comp" eq 'caseignore') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $comp eq "caseignore") 0 2 2
595 0 ▶ carp "Data::Template::Entry->comparator attribute '$attrib' (caseIgnore)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 0 0
596 0 return $self->{config}->{comparator}->{$attrib} = 'caseIgnore';
597 }
598  
599 2 ▶ if ("\L$comp" eq 'casesensitive') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $comp eq "casesensitive") 1 1 2
600 1 ▶ carp "Data::Template::Entry->comparator attribute '$attrib' (caseSensitive)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
601 1 return $self->{config}->{comparator}->{$attrib} = 'caseSensitive';
602 }
603  
604 1 ▶ if ("\L$comp" eq 'integer') {
Branch: 1 branch
BranchTrueFalseTotal
if (lc $comp eq "integer") 1 0 1
605 1 ▶ carp "Data::Template::Entry->comparator attribute '$attrib' (integer)" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 1 1
606 1 return $self->{config}->{comparator}->{$attrib} = 'integer';
607 }
608  
609 # Hmm - something odd here
610 0 croak "Unknown comparator type";
611 }
612  
613 ########################################
614  
615 =head2 map
616  
617 Create a new entry object by applying a map to the current one.
618 Further entries can also be specified. They will be passed to the Data::Toolkit::Map
619 generate method.
620  
621 $newEntry = $entry->map($thisMap);
622 $newEntry = $entry->map($thisMap,$entry2,$entry3,...);
623  
624 The map is a Data::Toolkit::Map object.
625  
626 =cut
627  
628 5 ▶sub map {
Subroutine map: called CC=7 SCAR=19.9
Pod documented
629 5 my $self = shift;
630 5 my $map = shift;
631  
632 5 ▶ croak "map requires a map object" if (!$map);
Branch: 1 branch
BranchTrueFalseTotal
unless $map 0 5 5
633 5 ▶ croak "map object must be of type Data::Toolkit::Map" if (!$map->isa('Data::Toolkit::Map'));
Branch: 1 branch
BranchTrueFalseTotal
unless $map->isa("Data::Toolkit::Map") 0 5 5
634 5 ▶ carp "Data::Template::Entry->map" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 5 5
635  
636 # Create a new entry with the same setup as this one
637 5 my $newEntry = Data::Toolkit::Entry->new($self->{config});
638  
639 # Get the list of output attributes from the map
640 5 my $mapOutputs = $map->outputs();
641  
642 # Step through that list creating attributes in the new entry
643 # Do not create an attribute if given an undef arrayref
644 #
645 5 foreach my $attr (@$mapOutputs) {
646 28 my $vals = $map->generate($attr,$self,@_);
647 28 ▶ warn "Data::Template::Entry->map $attr: " . (join ':',@$vals) if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 28 28
648 28 ▶ $newEntry->add($attr, $vals) if $vals;
Branch: 1 branch
BranchTrueFalseTotal
if $vals 20 8 28
649 }
650  
651 5 return $newEntry;
652 }
653  
654  
655 ########################################
656  
657 =head2 delete
658  
659 Delete a value from an attribute:
660  
661 $result = $entry->delete('thisAttribute','thisValue');
662  
663 Delete an attribute and all its values:
664  
665 $result = $entry->delete('thisAttribute');
666  
667 In both cases, returns a list containing any values that it deleted.
668 If nothing was deleted, returns false.
669  
670 Note that deleting an attribute does not delete setting such as the
671 comparator for that attribute.
672  
673 =cut
674  
675 4 ▶sub delete {
Subroutine delete: called CC=10 SCAR=23.9
Pod documented
676 4 my $self = shift;
677 4 my $attrib = shift;
678 4 my $value = shift;
679  
680 4 ▶ croak "delete requires an attribute name" if (!$attrib);
Branch: 1 branch
BranchTrueFalseTotal
unless $attrib 0 4 4
681  
682 # Lower-case the attribute name if necessary
683 4 ▶ $attrib = "\L$attrib" if (!$self->{config}->{caseSensitiveNames});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"config"}{"caseSensitiveNames"} 4 0 4
684  
685  
686 4 ▶ if (defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 2 2 4
687 # We are deleting a single value
688 2 ▶ carp "Data::Template::Entry->delete '$value' from attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
689 2 my $allValues = $self->{data}->fetch($attrib);
690 # Is there anything there at all?
691 2 ▶ return undef if (!defined($allValues) or !defined($allValues->[0]));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $allValues and defined $allValues->[0] 0 2 2
Condition33%1/3
defined $allValues and defined $allValues->[0]
!ll&&!rl&&r
002
MC/DC0%0/2
defined $allValues and defined $allValues->[0]
defined $allValues defined $allValues->[0]
Truth table33%1/3
A and B
A= defined $allValuesB= defined $allValues->[0]
ABresult
0X0
100
111
692 # print "NEED TO DELETE $value\n";
693 2 ▶ for (my $count=0; defined($allValues->[$count]); $count++) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $allValues->[$count]) 10 1 11
694 10 ▶ if ($value eq $allValues->[$count]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value eq $allValues->[$count]) 1 9 10
695 1 return $self->{data}->splice($attrib,$count,1);
696 }
697 }
698 # Not found
699 1 return undef;
700 }
701 else {
702 # We are deleting the whole attribute
703 2 ▶ carp "Data::Template::Entry->delete attribute '$attrib'" if $debug;
Branch: 1 branch
BranchTrueFalseTotal
if $debug 0 2 2
704 2 return $self->{data}->delete($attrib);
705 }
706 }
707  
708  
709 ########################################################################
710 # Debugging methods
711 ########################################################################
712  
713 =head1 Debugging methods
714  
715 =head2 debug
716  
717 Set and/or get the debug level for Data::Toolkit::Entry
718  
719 my $currentDebugLevel = Data::Toolkit::Entry->debug();
720 my $newDebugLevel = Data::Toolkit::Entry(1);
721  
722 Any non-zero debug level causes the module to print copious debugging information.
723  
724 Note that this is a package method, not an object method. It should always be
725 called exactly as shown above.
726  
727 All debug information is reported using "carp" from the Carp module, so if
728 you want a full stack backtrace included you can run your program like this:
729  
730 perl -MCarp=verbose myProg
731  
732 =cut
733  
734 # Class method to set and/or get debug level
735 #
736 4 ▶sub debug {
Subroutine debug: called CC=3 SCAR=11.3
Pod documented
737 4 my $class = shift;
738 0 ▶ if (ref $class) { croak "Class method 'debug' called as object method" }
Branch: 1 branch
BranchTrueFalseTotal
if (ref $class) 0 4 4
739 # print "DEBUG: ", (join '/', @_), "\n";
740 4 ▶ $debug = shift if (@_ == 1);
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 3 1 4
741 4 return $debug
742 }
743  
744 ########################################
745  
746 =head2 dump
747  
748 Returns a text representation of the entry.
749  
750 my $string = $entry->dump();
751  
752 =cut
753  
754  
755 0 ▶sub dump {
Subroutine dump: not called CC=1 SCAR=6.9
Pod documented
756 0 my $self = shift;
757  
758 0 my %hash = $self->{data}->fetch_all();
759 0 return Dumper(\%hash);
760 }
761  
762 ########################################################################
763 ########################################################################
764  
765 =head1 Error handling
766  
767 If you miss out an essential parameter, the module will throw an exception
768 using "croak" from the Carp module. These exceptions represent programming
769 errors in most cases so there is little point in trapping them with "eval".
770  
771 =head1 Author
772  
773 Andrew Findlay
774  
775 Skills 1st Ltd
776  
777 andrew.findlay@skills-1st.co.uk
778  
779 http://www.skills-1st.co.uk/
780  
781 =cut
782  
783 ########################################################################
784 ########################################################################
785 6 1;
« Toolkit.pm ↑ Summary Toolkit/Connector/CSV.pm »