File Coverage

blib/lib/App/RecordStream/Aggregator/Internal/ForField2.pm
Criterion Covered Total %
statement 48 48 100.0
branch 6 6 100.0
condition n/a
subroutine 9 9 100.0
pod 0 4 0.0
total 63 67 94.0


line stmt bran cond sub pod time code
1             package App::RecordStream::Aggregator::Internal::ForField2;
2              
3 5     5   3611 use strict;
  5         11  
  5         120  
4 5     5   23 use warnings;
  5         11  
  5         105  
5              
6 5     5   24 use App::RecordStream::Aggregator::Aggregation;
  5         9  
  5         85  
7 5     5   21 use App::RecordStream::DomainLanguage::Registry;
  5         11  
  5         88  
8              
9 5     5   21 use base 'App::RecordStream::Aggregator::Aggregation';
  5         8  
  5         1727  
10              
11             sub new
12             {
13 12     12 0 28 my $class = shift;
14 12         23 my $regex1 = shift;
15 12         18 my $regex2 = shift;
16 12         22 my $snippet = shift;
17              
18 12         49 my $this =
19             {
20             'REGEX1' => $regex1,
21             'REGEX2' => $regex2,
22             'SNIPPET' => $snippet,
23             };
24              
25 12         29 bless $this, $class;
26              
27 12         38 return $this;
28             }
29              
30             sub initial
31             {
32 4     4 0 66 return {};
33             }
34              
35             sub combine
36             {
37 40     40 0 73 my $this = shift;
38 40         72 my $cookie = shift;
39 40         61 my $record = shift;
40              
41 40         67 my @field1;
42             my @field2;
43 40         121 for my $field (keys(%$record))
44             {
45 132 100       476 push @field1, $field if($field =~ $this->{'REGEX1'});
46 132 100       485 push @field2, $field if($field =~ $this->{'REGEX2'});
47             }
48              
49 40         87 for my $field1 (@field1)
50             {
51 64         113 for my $field2 (@field2)
52             {
53 112         233 my $fieldc = "$field1,$field2";
54              
55 112 100       264 if(!exists($cookie->{$fieldc}))
56             {
57 16         79 my $agg = $this->{'SNIPPET'}->evaluate_as('AGGREGATOR', {'$f1' => $field1, '$f2' => $field2});
58 16         91 $cookie->{$fieldc} = [$agg, $agg->initial()];
59             }
60              
61 112         187 my ($agg, $sub_cookie) = @{$cookie->{$fieldc}};
  112         235  
62              
63 112         333 $sub_cookie = $agg->combine($sub_cookie, $record);
64              
65 112         294 $cookie->{$fieldc}->[1] = $sub_cookie;
66             }
67             }
68              
69 40         117 return $cookie;
70             }
71              
72             sub squish
73             {
74 4     4 0 28 my $this = shift;
75 4         8 my $cookie = shift;
76              
77 4         16 for my $fieldc (keys(%$cookie))
78             {
79 16         30 my ($agg, $sub_cookie) = @{$cookie->{$fieldc}};
  16         37  
80 16         54 $cookie->{$fieldc} = $agg->squish($sub_cookie);
81             }
82              
83 4         14 return $cookie;
84             }
85              
86             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new', 'for_field', 'SCALAR', 'SCALAR', 'SNIPPET');
87              
88             1;