File Coverage

blib/lib/App/RecordStream/Aggregator/Internal/ForField.pm
Criterion Covered Total %
statement 42 42 100.0
branch 4 4 100.0
condition n/a
subroutine 9 9 100.0
pod 0 4 0.0
total 55 59 93.2


line stmt bran cond sub pod time code
1             package App::RecordStream::Aggregator::Internal::ForField;
2              
3 5     5   2486 use strict;
  5         13  
  5         132  
4 5     5   28 use warnings;
  5         12  
  5         143  
5              
6 5     5   29 use App::RecordStream::Aggregator::Aggregation;
  5         12  
  5         110  
7 5     5   27 use App::RecordStream::DomainLanguage::Registry;
  5         11  
  5         123  
8              
9 5     5   29 use base 'App::RecordStream::Aggregator::Aggregation';
  5         11  
  5         1743  
10              
11             sub new
12             {
13 6     6 0 21 my $class = shift;
14 6         17 my $regex = shift;
15 6         15 my $snippet = shift;
16              
17 6         39 my $this =
18             {
19             'REGEX' => $regex,
20             'SNIPPET' => $snippet,
21             };
22              
23 6         17 bless $this, $class;
24              
25 6         24 return $this;
26             }
27              
28             sub initial
29             {
30 2     2 0 53 return {};
31             }
32              
33             sub combine
34             {
35 8     8 0 28 my $this = shift;
36 8         19 my $cookie = shift;
37 8         20 my $record = shift;
38              
39 8         35 for my $field (keys(%$record))
40             {
41 16 100       141 next unless($field =~ $this->{'REGEX'});
42              
43 8 100       30 if(!exists($cookie->{$field}))
44             {
45 4         32 my $agg = $this->{'SNIPPET'}->evaluate_as('AGGREGATOR', {'$f' => $field});
46 4         43 $cookie->{$field} = [$agg, $agg->initial()];
47             }
48              
49 8         21 my ($agg, $sub_cookie) = @{$cookie->{$field}};
  8         31  
50              
51 8         44 $sub_cookie = $agg->combine($sub_cookie, $record);
52              
53 8         40 $cookie->{$field}->[1] = $sub_cookie;
54             }
55              
56 8         47 return $cookie;
57             }
58              
59             sub squish
60             {
61 2     2 0 20 my $this = shift;
62 2         8 my $cookie = shift;
63              
64 2         10 for my $field (keys(%$cookie))
65             {
66 4         11 my ($agg, $sub_cookie) = @{$cookie->{$field}};
  4         13  
67 4         27 $cookie->{$field} = $agg->squish($sub_cookie);
68             }
69              
70 2         9 return $cookie;
71             }
72              
73             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new', 'for_field', 'SCALAR', 'SNIPPET');
74              
75             1;