File Coverage

blib/lib/App/RecordStream/Operation/delta.pm
Criterion Covered Total %
statement 34 40 85.0
branch 4 6 66.6
condition 1 3 33.3
subroutine 6 7 85.7
pod 0 4 0.0
total 45 60 75.0


line stmt bran cond sub pod time code
1             package App::RecordStream::Operation::delta;
2              
3             our $VERSION = "4.0.25";
4              
5 2     2   912 use strict;
  2         4  
  2         47  
6              
7 2     2   7 use base qw(App::RecordStream::Operation);
  2         2  
  2         693  
8              
9             sub init
10             {
11 2     2 0 3 my $this = shift;
12 2         3 my $args = shift;
13              
14 2         11 my $key_groups = App::RecordStream::KeyGroups->new();
15             my $spec = {
16 2     2   1203 "key|k=s" => sub { $key_groups->add_groups($_[1]); },
17 2         10 };
18              
19 2         9 $this->parse_options($args, $spec);
20              
21 2 50       8 die "Must specify --key\n" unless $key_groups->has_any_group();
22              
23 2         19 $this->{'KEY_GROUPS'} = $key_groups;
24             }
25              
26             sub accept_record
27             {
28 10     10 0 12 my $this = shift;
29 10         12 my $record = shift;
30 10         17 my $last_record = $this->{'LAST_RECORD'};
31 10 100       16 if ( $last_record ) {
32 8         9 foreach my $key (@{$this->{'KEY_GROUPS'}->get_keyspecs($last_record)})
  8         20  
33             {
34 8 50 33     9 if ( ${$record->guess_key_from_spec($key)} and ${$last_record->guess_key_from_spec($key)} )
  8         17  
  8         18  
35             {
36 8         12 ${$last_record->guess_key_from_spec($key)} = ${$record->guess_key_from_spec($key)} - ${$last_record->guess_key_from_spec($key)};
  8         17  
  8         15  
  8         17  
37             }
38             else
39             {
40 0         0 ${$last_record->guess_key_from_spec($key)} = undef;
  0         0  
41             }
42             }
43 8         33 $this->push_record($last_record);
44             }
45              
46 10         17 $this->{'LAST_RECORD'} = $record;
47              
48 10         34 return 1;
49             }
50              
51             sub add_help_types {
52 2     2 0 4 my $this = shift;
53 2         9 $this->use_help_type('keyspecs');
54 2         7 $this->use_help_type('keygroups');
55 2         6 $this->use_help_type('keys');
56             }
57              
58             sub usage
59             {
60 0     0 0   my $this = shift;
61              
62 0           my $options = [
63             [ 'key|-k ', 'Comma separated list of the fields that should be transformed. Fields not in this list will be passed through unchanged, using the *first* record of each delta pair. This may be a keyspec or a keygroup, see "--help-keyspecs" for more information'],
64             ];
65              
66 0           my $args_string = $this->options_string($options);
67              
68 0           return <
69             Usage: recs-delta []
70             __FORMAT_TEXT__
71             Transforms absolute values into deltas between adjacent records.
72             __FORMAT_TEXT__
73              
74             Arguments:
75             $args_string
76              
77             Examples:
78             Transforms a cumulative counter of errors into a count of errors per record.
79             recs-delta --key=errors
80             USAGE
81             }
82              
83             1;