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