File Coverage

blib/lib/App/RecordStream/Aggregator/Mode.pm
Criterion Covered Total %
statement 32 34 94.1
branch 2 2 100.0
condition n/a
subroutine 8 10 80.0
pod 0 5 0.0
total 42 51 82.3


line stmt bran cond sub pod time code
1             package App::RecordStream::Aggregator::Mode;
2              
3             our $VERSION = "4.0.25";
4              
5 6     6   3289 use strict;
  6         13  
  6         155  
6 6     6   27 use warnings;
  6         9  
  6         172  
7              
8 6     6   441 use App::RecordStream::Aggregator::InjectInto::Field;
  6         26  
  6         105  
9 6     6   23 use App::RecordStream::DomainLanguage::Registry;
  6         11  
  6         140  
10              
11 6     6   26 use base qw(App::RecordStream::Aggregator::InjectInto::Field);
  6         22  
  6         1675  
12              
13             #sub new -- passed through
14              
15             #sub new_from_valuation -- passed through
16              
17             sub initial {
18 1     1 0 3 return {};
19             }
20              
21             sub combine_field
22             {
23 9     9 0 13 my $this = shift;
24 9         11 my $cookie = shift;
25 9         13 my $value = shift;
26              
27 9         17 $cookie->{$value}++;
28 9         28 return $cookie;
29             }
30              
31             sub squish {
32 1     1 0 5 my $this = shift;
33 1         3 my $cookie = shift;
34              
35 1         4 my @keys = keys %$cookie;
36 1         2 my $max_key = shift @keys;
37 1         2 my $max_value = $cookie->{$max_key};
38              
39 1         3 foreach my $key ( @keys ) {
40 5         8 my $value = $cookie->{$key};
41 5 100       9 if ( $max_value < $value ) {
42 1         2 $max_key = $key;
43 1         2 $max_value = $value;
44             }
45             }
46              
47 1         3 return $max_key;
48             }
49              
50             sub short_usage
51             {
52 0     0 0   return "most common value for a field";
53             }
54              
55             sub long_usage
56             {
57 0     0 0   print <
58             Usage: mode,
59             Finds the most common value for a field and returns it.
60             Will load all values into memory.
61             EOF
62             }
63              
64             App::RecordStream::Aggregator->register_implementation('mode', __PACKAGE__);
65              
66             App::RecordStream::DomainLanguage::Registry::register_vfn(__PACKAGE__, 'new_from_valuation', 'mode', 'VALUATION');
67              
68             1;