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   3507 use strict;
  5         12  
  5         137  
4 5     5   30 use warnings;
  5         11  
  5         151  
5              
6 5     5   34 use App::RecordStream::Aggregator::Aggregation;
  5         12  
  5         125  
7 5     5   30 use App::RecordStream::DomainLanguage::Registry;
  5         12  
  5         151  
8              
9 5     5   34 use base 'App::RecordStream::Aggregator::Aggregation';
  5         16  
  5         2005  
10              
11             sub new
12             {
13 12     12 0 32 my $class = shift;
14 12         26 my $regex1 = shift;
15 12         23 my $regex2 = shift;
16 12         23 my $snippet = shift;
17              
18 12         55 my $this =
19             {
20             'REGEX1' => $regex1,
21             'REGEX2' => $regex2,
22             'SNIPPET' => $snippet,
23             };
24              
25 12         28 bless $this, $class;
26              
27 12         31 return $this;
28             }
29              
30             sub initial
31             {
32 4     4 0 75 return {};
33             }
34              
35             sub combine
36             {
37 40     40 0 70 my $this = shift;
38 40         64 my $cookie = shift;
39 40         48 my $record = shift;
40              
41 40         63 my @field1;
42             my @field2;
43 40         117 for my $field (keys(%$record))
44             {
45 132 100       735 push @field1, $field if($field =~ $this->{'REGEX1'});
46 132 100       475 push @field2, $field if($field =~ $this->{'REGEX2'});
47             }
48              
49 40         89 for my $field1 (@field1)
50             {
51 64         103 for my $field2 (@field2)
52             {
53 112         215 my $fieldc = "$field1,$field2";
54              
55 112 100       207 if(!exists($cookie->{$fieldc}))
56             {
57 16         83 my $agg = $this->{'SNIPPET'}->evaluate_as('AGGREGATOR', {'$f1' => $field1, '$f2' => $field2});
58 16         80 $cookie->{$fieldc} = [$agg, $agg->initial()];
59             }
60              
61 112         174 my ($agg, $sub_cookie) = @{$cookie->{$fieldc}};
  112         214  
62              
63 112         279 $sub_cookie = $agg->combine($sub_cookie, $record);
64              
65 112         267 $cookie->{$fieldc}->[1] = $sub_cookie;
66             }
67             }
68              
69 40         126 return $cookie;
70             }
71              
72             sub squish
73             {
74 4     4 0 30 my $this = shift;
75 4         14 my $cookie = shift;
76              
77 4         20 for my $fieldc (keys(%$cookie))
78             {
79 16         25 my ($agg, $sub_cookie) = @{$cookie->{$fieldc}};
  16         35  
80 16         52 $cookie->{$fieldc} = $agg->squish($sub_cookie);
81             }
82              
83 4         15 return $cookie;
84             }
85              
86             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new', 'for_field', 'SCALAR', 'SCALAR', 'SNIPPET');
87              
88             1;