blib/lib/App/RecordStream/Record.pm

filter: Statementstmt 98.5% 139 / 141 Branchbran 88.8% 32 / 36 Conditioncond 75.0% 9 / 12 MC/DCmcdc 37.5% 3 / 8 Subroutinesub 96.7% 30 / 31 Podpod 82.6% 19 / 23 totaltotal 92.4% 232 / 251 CC 27 SCAR 12.8
CC
27
Cov
95%
__ANON__
10.1
get_comparator_and_field
5.0
AUTOLOAD
3.1
CRAP 3.6
« blib/lib/App/RecordStream/Operation/annotate.pm ↑ Summary blib/lib/App/RecordStream/BaseRegistry.pm »
1 package App::RecordStream::Record;
2  
3 =head1 NAME
4  
5 App::RecordStream::Record
6  
7 =head1 AUTHOR
8  
9 Benjamin Bernard <perlhacker@benjaminbernard.com>
10 Keith Amling <keith.amling@gmail.com>
11  
12 =head1 DESCRIPTION
13  
14 An object representing a single record of recs input/output. This is a
15 glorified hash with some helper methods.
16  
17 =head1 SYNOPSIS
18  
19 use App::RecordStream::Record;
20 my $record = App::RecordStream::Record->new("name" => "John Smith", "age" => 39);
21  
22 =head1 CONSTRUCTOR
23  
24 =over 4
25  
26 =item App::RecordStream::Record->new(%hash);
27  
28 Construct a new record with provided keys and values. Can take a single
29 argument which is a hash ref. If this form is used, it will bless that hash
30 and use it, so that hash ref now belongs to this object. This avoids memory
31 copies
32  
33 =back
34  
35 =head1 METHODS
36  
37 =over 4
38  
39 =item @keys = $this->keys();
40  
41 Returns an array of field names.
42  
43 =item $boolean = $this->exists($key);
44  
45 Determine whether or not this field exists in the record.
46  
47 =item $value = $this->get($key);
48  
49 Retrieve a field from the record, returns undef if there is no such field.
50  
51 =item $old_value = $this->get_XXX();
52  
53 Calls $this->get("XXX");
54  
55 =item $old_value = $this->set($key, $value);
56  
57 Set a field in the record, returns the old value, or undef if there was no such
58 field.
59  
60 =item $old_value = $this->set_XXX($value);
61  
62 Calls $this->set("XXX", $value);
63  
64 =item @old_values = $this->remove(@keys);
65  
66 Remove fields from the record, returns the old values (or undef for each missing).
67  
68 =item $this->prune_to(@keys);
69  
70 Removes fields whose names are not among those provided.
71  
72 =item $this->rename($old_key, $new_key);
73  
74 Rename a field. If the field did not exists a new field with value undef is
75 created.
76  
77 =item %hash = $this->as_hash();
78  
79 Marshall a record into hash format.
80  
81 =item $hashref = $this->as_hashref();
82  
83 Marshall a record into hash format, returning a reference. The caller may
84 modify this hash (the changes will not be reflected in the record itself).
85  
86 =item $cmp = $this->cmp($that, @keys);
87  
88 Compare this record to another, using comparators derived from @keys (see
89 get_comparators). Returns -1, 0, or 1 for $this before $that, $this same as
90 $that, and $this after $that, respectively.
91  
92 =item $value_ref = $this->guess_key_from_spec($keyspec, $no_vivify = 0, $throw_error = 0)
93  
94 Get the reference for a key spec. Commonly used like:
95  
96 ${$r->guess_key_from_spec('foo/bar')} eq 'zip'
97 ${$r->guess_key_from_spec('foo/bar')} = 'boo'
98  
99 (the assign back gets back into the record)
100  
101 no_vivify and no_error are optional, and control behavior in the absence of the
102 specified key. throw_error will cause a 'NoSuchKey' exception to be thrown.
103  
104 See 'man recs' for more info on key specs
105  
106 =item $boolean = $this->has_key_spec($spec)
107  
108 Returns a boolean indicating the presence of the key spec in the record. Will
109 not have side effects in the record.
110  
111 =item $ARRAY_REF = $this->get_key_list_for_spec($spec)
112  
113 Returns a list of keys that the spec expanded out to. Arrrays will still be
114 #NUM, hash keys will be fully expanded to the keys present in the record.
115  
116 =item $keyspecs_array_ref = $this->get_keys_for_group($key_group, $rerun)
117  
118 Will create a App::RecordStream::KeyGroups (if necessary) and return the keyspecs that match
119 the given group. See --help-keyspecs or App::RecordStream::KeyGroups for more information.
120  
121 Setting rerun to true will cause every record this is called on to re-do
122 keygroup calculation
123  
124 =item $values_array_ref = $this->get_group_values($key_group, $rerun)
125  
126 Returns the values in this record for a key group. Will rerun keygroup parsing
127 if $rerun is passed
128  
129 =item $comparators_ref = App::RecordStream::Record::get_comparators(@specs)
130  
131 Calls get_comparator for each element of @specs and returns the results
132 together in an array reference.
133  
134 =item $comparator = App::RecordStream::Record::get_comparator($spec)
135  
136 Produces a comparator function (which takes two records and returns similarly
137 to <=> or cmp) from the provided $spec. $spec should be like "<field>" for
138 lexical sort, or "<field>=<sign><type><star>" where <sign> is "+" or "" for
139 ascending or "-" for descending and type is one of the known types and <star>
140 is "*" for sorting "ALL" to the end or "" for normal behaviour. Type include
141 "", "l", "lex", or "lexical" for lexical sort (using cmp), and "n", "num" or
142 "numeric" for numeric sort (using <=>).
143  
144 =item @sorted_records = App::RecordStream::Record::sort($records_ref, @specs)
145  
146 Sorts an array ref of records using the provided specs, returns an array of
147 records.
148  
149 =back
150  
151 =cut
152  
153 116 our $VERSION = "4.0.25";
154  
155 116 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
156 116 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
157  
158 116 ▶use App::RecordStream::KeyGroups;
Subroutine BEGIN: called CC=1 SCAR=0.0
159 116 ▶use App::RecordStream::KeySpec;
Subroutine BEGIN: called CC=1 SCAR=0.0
160  
161 116 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
162  
163 ### Utility cruft
164  
165 116 my %comparators =
166 (
167 "" => \&cmp_lex,
168 "l" => \&cmp_lex,
169 "lex" => \&cmp_lex,
170 "lexical" => \&cmp_lex,
171  
172 # Ugh, "natural" is really the wrong name for numeric, I would expect
173 # natural to handle any sequence of text and numbers. Unfortunately, this
174 # alias is sort of grandfathered in.
175 "n" => \&cmp_nat,
176 "nat" => \&cmp_nat,
177 "natural" => \&cmp_nat,
178  
179 # add more accurate "numeric" alias
180 "num" => \&cmp_nat,
181 "numeric" => \&cmp_nat,
182 );
183  
184 sub cmp_lex
185 60 ▶{
Subroutine cmp_lex: called CC=1 SCAR=0.0
Pod undocumented
186 60 my ($this, $that) = @_;
187 60 return ($this cmp $that);
188 }
189  
190 sub cmp_nat
191 85 ▶{
Subroutine cmp_nat: called CC=1 SCAR=0.0
Pod undocumented
192 85 my ($this, $that) = @_;
193 85 return ($this <=> $that);
194 }
195  
196 sub get_comparators
197 98 ▶{
Subroutine get_comparators: called CC=1 SCAR=0.0
Pod documented
198 98 return [map { get_comparator($_) } @_];
199 }
200  
201 {
202 sub get_comparator
203 123 ▶ {
Subroutine get_comparator: called CC=1 SCAR=0.0
Pod documented
204 123 my ($comparator, $field) = get_comparator_and_field(@_);
205  
206 123 return $comparator;
207 }
208  
209 sub get_comparator_and_field
210 125 ▶ {
Subroutine get_comparator_and_field: called CC=5 SCAR=16.1
Pod undocumented
211 125 my $spec = shift;
212  
213 125 my ($field, $direction, $comparator_name, $all_hack);
214  
215 125 ▶ if ( $spec =~ m/=/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /=/) { } 110 15 125
216 {
217 110 ($field, $direction, $comparator_name, $all_hack) = $spec =~ /^(.*)=([-+]?)(.*?)(\*?)$/;
218 }
219 else
220 {
221 15 ($field, $direction, $comparator_name, $all_hack) = ($spec, undef, 'lexical', '');
222 }
223  
224 125 ▶ $direction = '+' unless ( $direction );
Branch: 1 branch
BranchTrueFalseTotal
unless $direction 55 70 125
225 125 ▶ $all_hack = $all_hack ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$all_hack ? : 2 123 125
226  
227 125 my $func = $comparators{$comparator_name};
228 125 ▶ die "Not a valid comparator: $comparator_name" unless ( $func );
Branch: 1 branch
BranchTrueFalseTotal
unless $func 0 125 125
229  
230 151 ▶ my $comparator = sub {
Subroutine __ANON__: called CC=10 SCAR=23.1
231 151 my ($this, $that) = @_;
232  
233 151 my $val = undef;
234  
235 151 ▶ if ( $all_hack )
Branch: 1 branch
BranchTrueFalseTotal
if ($all_hack) 41 110 151
236 {
237 41 my $this_value = ${$this->guess_key_from_spec($field)};
238 41 my $that_value = ${$that->guess_key_from_spec($field)};
239 41 ▶ if ( $this_value eq 'ALL' && $that_value ne 'ALL' )
Branch: 1 branch
BranchTrueFalseTotal
if ($this_value eq "ALL" and $that_value ne "ALL") 2 39 41
Condition66%2/3
$this_value eq "ALL" and $that_value ne "ALL"
!ll&&!rl&&r
3902
MC/DC50%1/2
$this_value eq "ALL" and $that_value ne "ALL"
$this_value eq "ALL" $that_value ne "ALL"
Truth table66%2/3
A and B
A= $this_value eq "ALL"B= $that_value ne "ALL"
ABresult
0X0
100
111
240 {
241 2 $val = 1;
242 }
243 41 ▶ if ( $this_value ne 'ALL' && $that_value eq 'ALL' )
Branch: 1 branch
BranchTrueFalseTotal
if ($this_value ne "ALL" and $that_value eq "ALL") 4 37 41
Condition100%3/3
$this_value ne "ALL" and $that_value eq "ALL"
!ll&&!rl&&r
2354
MC/DC100%2/2
$this_value ne "ALL" and $that_value eq "ALL"
$this_value ne "ALL" $that_value eq "ALL"
Truth table100%3/3
A and B
A= $this_value ne "ALL"B= $that_value eq "ALL"
ABresult
0X0
100
111
244 {
245 4 $val = -1;
246 }
247 41 ▶ if ( $this_value eq 'ALL' && $that_value eq 'ALL' )
Branch: 1 branch
BranchTrueFalseTotal
if ($this_value eq "ALL" and $that_value eq "ALL") 0 41 41
Condition66%2/3
$this_value eq "ALL" and $that_value eq "ALL"
!ll&&!rl&&r
3920
MC/DC0%0/2
$this_value eq "ALL" and $that_value eq "ALL"
$this_value eq "ALL" $that_value eq "ALL"
Truth table66%2/3
A and B
A= $this_value eq "ALL"B= $that_value eq "ALL"
ABresult
0X0
100
111
248 {
249 0 return 0;
250 }
251 }
252  
253 151 ▶ if ( ! defined $val )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 145 6 151
254 {
255 145 $val = $func->(${$this->guess_key_from_spec($field)}, ${$that->guess_key_from_spec($field)});
256 }
257  
258 151 ▶ if ( $direction eq '-' )
Branch: 1 branch
BranchTrueFalseTotal
if ($direction eq "-") 70 81 151
259 {
260 70 return -$val;
261 }
262  
263 81 return $val;
264 125 };
265  
266 125 return ($comparator, $field);
267 }
268 }
269  
270 sub sort
271 116 ▶{
Subroutine sort: called CC=2 SCAR=7.0
Pod documented
272 6 my $records = shift;
273 6 my @specs = @_;
274  
275 6 ▶ return map { $records->[$_] } CORE::sort { $records->[$a]->cmp($records->[$b], @specs) || ($a <=> $b) } (0..(@$records - 1));
Condition66%2/3
$records->[$a]->cmp($records->[$b], @specs) || $a <=> $b
l!l&&r!l&&!r
7020
MC/DC0%0/2
$records->[$a]->cmp($records->[$b], @specs) || $a <=> $b
$records->[$a]->cmp($records->[$b], @specs) $a <=> $b
Truth table66%2/3
A || B
A= $records->[$a]->cmp($records->[$b], @specs)B= $a <=> $b
ABresult
1X1
011
000
276 }
277  
278 ### Actual class
279  
280 116 our $AUTOLOAD;
281  
282 sub new
283 1573 ▶{
Subroutine new: called CC=3 SCAR=11.0
Pod documented
284 1573 my $class = shift;
285  
286 1573 ▶ if ( scalar @_ == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 148 1425 1573
287 148 my $arg = $_[0];
288 148 ▶ if ( UNIVERSAL::isa($arg, 'HASH') ) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($arg, "HASH")) 148 0 148
289 148 bless $arg, $class;
290 148 return $arg;
291 }
292 }
293  
294 1425 my $this = { @_ };
295 1425 bless $this, $class;
296  
297 1425 return $this;
298 }
299  
300 sub keys
301 28 ▶{
Subroutine keys: called CC=1 SCAR=0.0
Pod documented
302 28 my ($this) = @_;
303 28 return CORE::keys(%$this);
304 }
305  
306 sub exists
307 3 ▶{
Subroutine exists: called CC=1 SCAR=0.0
Pod documented
308 3 my ($this, $field) = @_;
309 3 return exists($this->{$field});
310 }
311  
312 sub get
313 53 ▶{
Subroutine get: called CC=1 SCAR=0.0
Pod documented
314 53 my ($this, $field) = @_;
315 53 return $this->{$field};
316 }
317  
318 sub set
319 71 ▶{
Subroutine set: called CC=1 SCAR=0.0
Pod documented
320 71 my ($this, $field, $val) = @_;
321  
322 71 my $old = $this->{$field};
323 71 $this->{$field} = $val;
324  
325 71 return $old;
326 }
327  
328 sub remove
329 31 ▶{
Subroutine remove: called CC=2 SCAR=6.9
Pod documented
330 31 my ($this, @fields) = @_;
331  
332 31 my @old;
333 31 for my $field (@fields)
334 {
335 31 push @old, delete $this->{$field};
336 }
337  
338 31 return @old;
339 }
340  
341 sub prune_to
342 1 ▶{
Subroutine prune_to: called CC=3 SCAR=11.0
Pod documented
343 1 my ($this, @ok) = @_;
344  
345 1 my %ok = map { ($_ => 1) } @ok;
346 1 for my $field (CORE::keys(%$this))
347 {
348 4 ▶ if(!exists($ok{$field}))
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $ok{$field}) 2 2 4
349 {
350 2 delete $this->{$field};
351 }
352 }
353 }
354  
355 sub rename
356 25 ▶{
Subroutine rename: called CC=1 SCAR=0.0
Pod documented
357 25 my ($this, $old, $new) = @_;
358  
359 25 $this->set($new, $this->get($old));
360 25 $this->remove($old);
361 }
362  
363 sub as_hash
364 17 ▶{
Subroutine as_hash: called CC=1 SCAR=0.0
Pod documented
365 17 my ($this) = @_;
366 17 return %$this;
367 }
368  
369 sub as_hashref
370 21 ▶{
Subroutine as_hashref: called CC=1 SCAR=0.0
Pod documented
371 21 my ($this) = @_;
372 21 return {%$this};
373 }
374  
375 16 ▶sub TO_JSON {
Subroutine TO_JSON: called CC=1 SCAR=0.0
Pod undocumented
376 16 my ($this) = @_;
377 16 return $this->as_hashref();
378 }
379  
380 2697 ▶sub has_key_spec {
Subroutine has_key_spec: called CC=1 SCAR=0.0
Pod documented
381 2697 my ($this, $spec) = @_;
382 2697 my $spec_obj = App::RecordStream::KeySpec->new($spec);
383 2697 return $spec_obj->has_key_spec($this);
384 }
385  
386 4047 ▶sub guess_key_from_spec {
Subroutine guess_key_from_spec: called CC=1 SCAR=0.0
Pod documented
387 4047 return App::RecordStream::KeySpec::find_key(@_);
388 }
389  
390 2058 ▶sub get_key_list_for_spec {
Subroutine get_key_list_for_spec: called CC=1 SCAR=0.0
Pod documented
391 2058 my ($this, $spec) = @_;
392  
393 2058 my $spec_obj = App::RecordStream::KeySpec->new($spec);
394 2058 return $spec_obj->get_key_list_for_spec($this);
395 }
396  
397 {
398 116 my $key_groups = {};
399 4 ▶ sub get_keys_for_group {
Subroutine get_keys_for_group: called CC=3 SCAR=11.0
Pod documented
400 4 my ($this, $group_string, $rerun) = @_;
401  
402 4 my $group = $key_groups->{$group_string};
403 4 ▶ if ( ! $group ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($group) 1 3 4
404 1 my $new_group = App::RecordStream::KeyGroups->new($group_string);
405 1 $key_groups->{$group_string} = $new_group;
406 1 $group = $new_group;
407 }
408  
409 4 ▶ if ( $rerun ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($rerun) { } 2 2 4
410 2 return $group->get_keyspecs_for_record($this);
411 }
412 else {
413 2 return $group->get_keyspecs($this);
414 }
415 }
416 }
417  
418 116 ▶sub get_group_values {
Subroutine get_group_values: called CC=2 SCAR=6.9
Pod documented
419 1 my ($this, $group, $rerun) = @_;
420  
421 1 my $specs = $this->get_keys_for_group($group, $rerun);
422 1 my $values = [];
423  
424 1 foreach my $spec (@$specs) {
425 3 push @$values, ${$this->guess_key_from_spec($spec)};
426 }
427  
428 1 return $values;
429 }
430  
431 sub cmp
432 98 ▶{
Subroutine cmp: called CC=3 SCAR=11.0
Pod documented
433 98 my ($this, $that, @keys) = @_;
434  
435 98 my $comparators = get_comparators(@keys);
436  
437 98 foreach my $comparator (@$comparators) {
438 110 my $val = $comparator->($this, $that);
439 110 ▶ return $val if ( $val != 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $val != 0 94 16 110
440 }
441  
442 4 return 0;
443 }
444  
445 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=1 SCAR=0.0
446 }
447  
448 sub AUTOLOAD
449 7 ▶{
Subroutine AUTOLOAD: called CC=3 SCAR=11.2
450 7 my $this = shift;
451  
452 7 $AUTOLOAD =~ s/^.*://;
453  
454 7 ▶ if($AUTOLOAD =~ /^get_(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($App::RecordStream::Record::AUTOLOAD =~ /^get_(.*)$/) 5 2 7
455 {
456 5 return get($this, $1, @_);
457 }
458  
459 2 ▶ if($AUTOLOAD =~ /^set_(.*)$/)
Branch: 1 branch
BranchTrueFalseTotal
if ($App::RecordStream::Record::AUTOLOAD =~ /^set_(.*)$/) 2 0 2
460 {
461 2 return set($this, $1, @_);
462 }
463  
464 0 die "No such method " . $AUTOLOAD . " for " . ref($this) . "\n";
465 }
466  
467 116 1;
« blib/lib/App/RecordStream/Operation/annotate.pm ↑ Summary blib/lib/App/RecordStream/BaseRegistry.pm »