| 1 | package Farly::Object::Aggregate; | ||||||||||
| 2 | 16 | ▶ | use 5.008008; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 16 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 16 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 16 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 16 | require Exporter; | |||||||||
| 7 | 16 | require Farly::Object::List; | |||||||||
| 8 | |||||||||||
| 9 | 16 | our @ISA = qw(Exporter); | |||||||||
| 10 | 16 | our @EXPORT_OK = qw(NEXTVAL); | |||||||||
| 11 | |||||||||||
| 12 | 16 | our $VERSION = '0.26'; | |||||||||
| 13 | |||||||||||
| 14 | 8 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||
| 15 | 8 | my ( $class, $container ) = @_; | |||||||||
| 16 | |||||||||||
| 17 | 8 | ▶ | confess "container object required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | unless ( defined($container) ); | ||||||||||
| 19 | |||||||||||
| 20 | 8 | ▶ | confess "Farly::Object::List object required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | unless ( $container->isa('Farly::Object::List') ); | ||||||||||
| 22 | |||||||||||
| 23 | 8 | my $self = { | |||||||||
| 24 | CONTAINER => $container, # input | ||||||||||
| 25 | GROUPED => undef, # @array of objects grouped by identity | ||||||||||
| 26 | }; | ||||||||||
| 27 | |||||||||||
| 28 | 8 | bless $self, $class; | |||||||||
| 29 | |||||||||||
| 30 | 8 | return $self; | |||||||||
| 31 | } | ||||||||||
| 32 | |||||||||||
| 33 | 10 | ▶ | sub container { | ||||||||
|
Subroutine
container: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | 10 | return $_[0]->{'CONTAINER'}; | |||||||||
| 35 | } | ||||||||||
| 36 | |||||||||||
| 37 | 70 | ▶ | sub iter { | ||||||||
|
Subroutine
iter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 38 | 70 | return @{ $_[0]->{'GROUPED'} }; | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | 11 | ▶ | sub NEXTVAL { $_[0]->() } | ||||||||
|
Subroutine
NEXTVAL: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 42 | |||||||||||
| 43 | # iterator over the aggregate identities | ||||||||||
| 44 | 2 | ▶ | sub id_iterator { | ||||||||
|
Subroutine
id_iterator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 45 | 2 | my ($self) = @_; | |||||||||
| 46 | |||||||||||
| 47 | 2 | my @arr = $self->iter(); | |||||||||
| 48 | 2 | my $i = 0; | |||||||||
| 49 | |||||||||||
| 50 | # the iterator code ref | ||||||||||
| 51 | 7 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||
| 52 | 7 | ▶ | return undef if ( $i == scalar(@arr) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 53 | |||||||||||
| 54 | 5 | my $object = Farly::Object->new(); | |||||||||
| 55 | |||||||||||
| 56 | 5 | foreach my $property ( $arr[$i]->get_keys() ) { | |||||||||
| 57 | 14 | ▶ | if ( $property ne '__AGG__' ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 58 | 9 | $object->set( $property, $arr[$i]->get($property) ); | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 5 | $i++; | |||||||||
| 63 | |||||||||||
| 64 | 5 | return $object; | |||||||||
| 65 | } | ||||||||||
| 66 | 2 | } | |||||||||
| 67 | |||||||||||
| 68 | 1 | ▶ | sub list_iterator { | ||||||||
|
Subroutine
list_iterator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 69 | 1 | my ($self) = @_; | |||||||||
| 70 | |||||||||||
| 71 | 1 | my @arr = $self->iter(); | |||||||||
| 72 | 1 | my $i = 0; | |||||||||
| 73 | |||||||||||
| 74 | # the iterator code ref | ||||||||||
| 75 | 4 | ▶ | return sub { | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||
| 76 | 4 | ▶ | return undef if ( $i == scalar(@arr) ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | 3 | my $set = $arr[$i]->get('__AGG__'); | |||||||||
| 78 | 3 | $i++; | |||||||||
| 79 | 3 | return $set; | |||||||||
| 80 | } | ||||||||||
| 81 | 1 | } | |||||||||
| 82 | |||||||||||
| 83 | # CONTAINER objects which have defined all keys | ||||||||||
| 84 | # return \@array | ||||||||||
| 85 | 10 | ▶ | sub _has_defined_keys { | ||||||||
|
Subroutine
_has_defined_keys: called CC=6 SCAR=17.9
| |||||||||||
| 86 | 10 | my ( $self, $keys ) = @_; | |||||||||
| 87 | |||||||||||
| 88 | 10 | my @r; | |||||||||
| 89 | |||||||||||
| 90 | 10 | foreach my $obj ( $self->container->iter() ) { | |||||||||
| 91 | |||||||||||
| 92 | 357 | my $all_keys_defined = 1; | |||||||||
| 93 | |||||||||||
| 94 | 357 | foreach my $key (@$keys) { | |||||||||
| 95 | 735 | ▶ | if ( !$obj->has_defined($key) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 14 | $all_keys_defined = undef; | |||||||||
| 97 | 14 | last; | |||||||||
| 98 | } | ||||||||||
| 99 | 721 | ▶ | if ( !$obj->get($key)->can('compare') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | |||||||||||
| 101 | #warn "$self skipped ", $obj->dump(), " in groupby\n"; | ||||||||||
| 102 | 5 | $all_keys_defined = undef; | |||||||||
| 103 | 5 | last; | |||||||||
| 104 | } | ||||||||||
| 105 | } | ||||||||||
| 106 | |||||||||||
| 107 | 357 | ▶ | if ($all_keys_defined) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 108 | 338 | push @r, $obj; | |||||||||
| 109 | } | ||||||||||
| 110 | } | ||||||||||
| 111 | |||||||||||
| 112 | 10 | return \@r; | |||||||||
| 113 | } | ||||||||||
| 114 | |||||||||||
| 115 | # [ { KEY1 => value object, | ||||||||||
| 116 | # KEY2 => value object, | ||||||||||
| 117 | # __AGG__ => Farly::Object::List }, ] | ||||||||||
| 118 | # __AGG__ is a set of all objects sharing the | ||||||||||
| 119 | # common identity formed by KEY1 and KEY2, | ||||||||||
| 120 | # i.e $obj1->{KEY1} equals $obj2->{KEY1} | ||||||||||
| 121 | # and $obj1->{KEY2} equals $obj2->{KEY2} | ||||||||||
| 122 | # for all objects in __AGG__ | ||||||||||
| 123 | |||||||||||
| 124 | 10 | ▶ | sub groupby { | ||||||||
|
Subroutine
groupby: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 125 | 10 | my ($self) = shift; | |||||||||
| 126 | 10 | my @keys = @_; | |||||||||
| 127 | |||||||||||
| 128 | 10 | ▶ | confess "a list of keys is required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | unless ( scalar(@keys) > 0 ); | ||||||||||
| 130 | |||||||||||
| 131 | # $list will include objects that have defined all @keys | ||||||||||
| 132 | 10 | my $list = $self->_has_defined_keys( \@keys ); | |||||||||
| 133 | |||||||||||
| 134 | # check list size? | ||||||||||
| 135 | |||||||||||
| 136 | my @sorted = sort { | ||||||||||
| 137 | |||||||||||
| 138 | 10 | my $r; | |||||||||
| 139 | 979 | foreach my $key (@keys) { | |||||||||
| 140 | 1724 | $r = $a->get($key)->compare( $b->get($key) ); | |||||||||
| 141 | 1724 | ▶ | return $r if ( $r != 0 ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | } | ||||||||||
| 143 | 301 | return $r; | |||||||||
| 144 | |||||||||||
| 145 | } @$list; | ||||||||||
| 146 | |||||||||||
| 147 | 10 | my @grouped; | |||||||||
| 148 | |||||||||||
| 149 | 10 | ▶ | for ( my $i = 0 ; $i != scalar(@sorted) ; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 150 | |||||||||||
| 151 | 138 | my $root = Farly::Object->new(); | |||||||||
| 152 | |||||||||||
| 153 | 138 | foreach my $key (@keys) { | |||||||||
| 154 | 290 | $root->set( $key, $sorted[$i]->get($key) ); | |||||||||
| 155 | } | ||||||||||
| 156 | |||||||||||
| 157 | 138 | my $result = Farly::Object::List->new(); | |||||||||
| 158 | |||||||||||
| 159 | 138 | my $j = $i; | |||||||||
| 160 | |||||||||||
| 161 | 138 | ▶ | while ( $sorted[$j]->matches($root) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 162 | |||||||||||
| 163 | 338 | $result->add( $sorted[$j] ); | |||||||||
| 164 | |||||||||||
| 165 | 338 | $j++; | |||||||||
| 166 | |||||||||||
| 167 | 338 | ▶ | last() if $j == scalar(@sorted); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 138 | $i = $j - 1; | |||||||||
| 171 | |||||||||||
| 172 | 138 | $root->set( '__AGG__', $result ); | |||||||||
| 173 | |||||||||||
| 174 | 138 | push @grouped, $root; | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | 10 | $self->{'GROUPED'} = \@grouped; | |||||||||
| 178 | } | ||||||||||
| 179 | |||||||||||
| 180 | # input = search object | ||||||||||
| 181 | # return the __AGG__ object on first match | ||||||||||
| 182 | 62 | ▶ | sub matches { | ||||||||
|
Subroutine
matches: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 183 | 62 | my ( $self, $search ) = @_; | |||||||||
| 184 | |||||||||||
| 185 | 62 | foreach my $object ( $self->iter() ) { | |||||||||
| 186 | 516 | ▶ | if ( $object->matches($search) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 187 | 61 | return $object->get('__AGG__'); | |||||||||
| 188 | } | ||||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | #return an empty List on no match | ||||||||||
| 192 | 1 | return Farly::Object::List->new(); | |||||||||
| 193 | } | ||||||||||
| 194 | |||||||||||
| 195 | # input = search object and new __AGG__ | ||||||||||
| 196 | 2 | ▶ | sub update { | ||||||||
|
Subroutine
update: called CC=5 SCAR=16.6
Pod
documented
| |||||||||||
| 197 | 2 | my ( $self, $search, $list ) = @_; | |||||||||
| 198 | |||||||||||
| 199 | 2 | ▶ | confess "Farly::Object::List required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 200 | unless defined($list); | ||||||||||
| 201 | |||||||||||
| 202 | 2 | ▶ | confess "Farly::Object::List required" | ||||||||
|
Branch: 1 branch
| |||||||||||
| 203 | unless $list->isa('Farly::Object::List'); | ||||||||||
| 204 | |||||||||||
| 205 | 2 | foreach my $object ( $self->iter() ) { | |||||||||
| 206 | 11 | ▶ | if ( $object->matches($search) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 207 | 2 | $object->set( '__AGG__', $list ); | |||||||||
| 208 | 2 | return; | |||||||||
| 209 | } | ||||||||||
| 210 | } | ||||||||||
| 211 | |||||||||||
| 212 | 0 | confess $search->dump(), " not found"; | |||||||||
| 213 | } | ||||||||||
| 214 | |||||||||||
| 215 | 16 | 1; | |||||||||
| 216 | __END__ | ||||||||||