| 1 | package EntityModel::Array; | ||||||||||||
| 2 | 5 | $EntityModel::Array::VERSION = '0.016'; | |||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | |||||||||||||
| 6 | 5 | ▶ | use parent qw(Mixin::Event::Dispatch); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 5 | ▶ | use EntityModel::Log ':all'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | =head1 NAME | ||||||||||||
| 11 | |||||||||||||
| 12 | EntityModel::Array - wrapper object for dealing with arrayrefs | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 VERSION | ||||||||||||
| 15 | |||||||||||||
| 16 | Version 0.016 | ||||||||||||
| 17 | |||||||||||||
| 18 | =head1 DESCRIPTION | ||||||||||||
| 19 | |||||||||||||
| 20 | Primarily intended as an abstract interface for use with L<EntityModel> backend storage. | ||||||||||||
| 21 | |||||||||||||
| 22 | =head1 METHODS | ||||||||||||
| 23 | |||||||||||||
| 24 | =cut | ||||||||||||
| 25 | |||||||||||||
| 26 | use overload | ||||||||||||
| 27 | 5 | ▶ | '@{}' => sub { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.6
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 28 | 0 | my $self = shift; | |||||||||||
| 29 | 0 | return $self->{data}; | |||||||||||
| 30 | }, | ||||||||||||
| 31 | 5 | fallback => 1; | |||||||||||
| 32 | |||||||||||||
| 33 | =head2 new | ||||||||||||
| 34 | |||||||||||||
| 35 | Instantiates with the given arrayref | ||||||||||||
| 36 | |||||||||||||
| 37 | =cut | ||||||||||||
| 38 | |||||||||||||
| 39 | 4 | ▶ | sub new { | ||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 40 | 4 | my ($class, $data, %opt) = @_; | |||||||||||
| 41 | 4 | ▶ | bless { | ||||||||||
|
Condition100%2/2
$data || []
MC/DC100%1/1
$data || []
$data
Truth table100%2/2
A
A= $data
| |||||||||||||
| 42 | %opt, | ||||||||||||
| 43 | data => ($data || [ ]), | ||||||||||||
| 44 | }, $class; | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | =head2 count | ||||||||||||
| 48 | |||||||||||||
| 49 | Returns the number of items in the arrayref if called | ||||||||||||
| 50 | without parameters. This is the recommended usage. | ||||||||||||
| 51 | |||||||||||||
| 52 | If passed a coderef, will call that coderef with the count, | ||||||||||||
| 53 | and return $self instead. | ||||||||||||
| 54 | |||||||||||||
| 55 | =cut | ||||||||||||
| 56 | |||||||||||||
| 57 | 5 | ▶ | sub count { | ||||||||||
|
Subroutine
count: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||
| 58 | 5 | my $self = shift; | |||||||||||
| 59 | 5 | my $count = scalar @{$self->{data}}; | |||||||||||
| 60 | 5 | ▶ | if(@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 61 | 0 | $_->($count) for @_; | |||||||||||
| 62 | 0 | return $self; | |||||||||||
| 63 | } | ||||||||||||
| 64 | 5 | return $count; | |||||||||||
| 65 | } | ||||||||||||
| 66 | |||||||||||||
| 67 | =head2 list | ||||||||||||
| 68 | |||||||||||||
| 69 | Returns all items from the arrayref. | ||||||||||||
| 70 | |||||||||||||
| 71 | =cut | ||||||||||||
| 72 | |||||||||||||
| 73 | 1 | ▶ | sub list { | ||||||||||
|
Subroutine
list: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 74 | 1 | my $self = shift; | |||||||||||
| 75 | 1 | ▶ | return unless $self->{data}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 76 | 1 | return @{$self->{data}}; | |||||||||||
| 77 | } | ||||||||||||
| 78 | |||||||||||||
| 79 | =head2 push | ||||||||||||
| 80 | |||||||||||||
| 81 | Push the requested value onto the end of the arrayref. | ||||||||||||
| 82 | |||||||||||||
| 83 | =cut | ||||||||||||
| 84 | |||||||||||||
| 85 | 5 | ▶ | sub push : method { | ||||||||||
|
Subroutine
push: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||
| 86 | 5 | my $self = shift; | |||||||||||
| 87 | 5 | push @{$self->{data}}, @_; | |||||||||||
| 88 | 5 | $self->invoke_event(push => @_); | |||||||||||
| 89 | 5 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 90 | 5 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 91 | 5 | $w->(add => $_) foreach @_; | |||||||||||
| 92 | } | ||||||||||||
| 93 | } | ||||||||||||
| 94 | 5 | return $self; | |||||||||||
| 95 | } | ||||||||||||
| 96 | |||||||||||||
| 97 | =head2 splice | ||||||||||||
| 98 | |||||||||||||
| 99 | Support for the L<perlfunc/splice> operation. | ||||||||||||
| 100 | |||||||||||||
| 101 | Takes an offset, length and zero or more items, splices those into the array, | ||||||||||||
| 102 | invokes the C< splice > event, then returns $self. | ||||||||||||
| 103 | |||||||||||||
| 104 | =cut | ||||||||||||
| 105 | |||||||||||||
| 106 | 0 | ▶ | sub splice : method { | ||||||||||
|
Subroutine
splice: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 107 | 0 | my $self = shift; | |||||||||||
| 108 | 0 | my $offset = shift; | |||||||||||
| 109 | 0 | my $length = shift; | |||||||||||
| 110 | 0 | splice @{$self->{data}}, $offset, $length, @_; | |||||||||||
| 111 | 0 | $self->invoke_event(splice => $offset, $length, @_); | |||||||||||
| 112 | 0 | $self | |||||||||||
| 113 | } | ||||||||||||
| 114 | |||||||||||||
| 115 | =head2 add_watch | ||||||||||||
| 116 | |||||||||||||
| 117 | Add a coderef to be called when the array changes. | ||||||||||||
| 118 | |||||||||||||
| 119 | =cut | ||||||||||||
| 120 | |||||||||||||
| 121 | 0 | ▶ | sub add_watch : method { | ||||||||||
|
Subroutine
add_watch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||
| 123 | 0 | ▶ | $self->{onchange} ||= []; | ||||||||||
|
Condition0%0/2
$self->{"onchange"} ||= []
MC/DC0%0/1
$self->{"onchange"} ||= []
$self->{"onchange"}
Truth table0%0/2
A
A= $self->{"onchange"}
| |||||||||||||
| 124 | 0 | push @{$self->{onchange}}, @_; | |||||||||||
| 125 | 0 | return $self; | |||||||||||
| 126 | } | ||||||||||||
| 127 | |||||||||||||
| 128 | =head2 remove_watch : method | ||||||||||||
| 129 | |||||||||||||
| 130 | Removes a watch from this array. | ||||||||||||
| 131 | |||||||||||||
| 132 | Returns $self. | ||||||||||||
| 133 | |||||||||||||
| 134 | =cut | ||||||||||||
| 135 | |||||||||||||
| 136 | 0 | ▶ | sub remove_watch : method { | ||||||||||
|
Subroutine
remove_watch: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 137 | 0 | my $self = shift; | |||||||||||
| 138 | 0 | ▶ | return $self unless $self->{onchange}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 0 | foreach my $code (@_) { | |||||||||||
| 140 | 0 | @{ $self->{onchange} } = grep { $_ != $code } @{ $self->{onchange} }; | |||||||||||
| 141 | } | ||||||||||||
| 142 | 0 | return $self; | |||||||||||
| 143 | } | ||||||||||||
| 144 | |||||||||||||
| 145 | =head2 shift | ||||||||||||
| 146 | |||||||||||||
| 147 | Shift the first value out of the arrayref. | ||||||||||||
| 148 | |||||||||||||
| 149 | =cut | ||||||||||||
| 150 | |||||||||||||
| 151 | 0 | ▶ | sub shift : method { | ||||||||||
|
Subroutine
shift: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 152 | 0 | my $self = shift; | |||||||||||
| 153 | 0 | my $v = shift(@{$self->{data}}); | |||||||||||
| 154 | 0 | $self->invoke_event(shift => $v); | |||||||||||
| 155 | 0 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 156 | 0 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 157 | 0 | $w->(drop => $v); | |||||||||||
| 158 | } | ||||||||||||
| 159 | } | ||||||||||||
| 160 | 0 | return $v; | |||||||||||
| 161 | } | ||||||||||||
| 162 | |||||||||||||
| 163 | =head2 pop | ||||||||||||
| 164 | |||||||||||||
| 165 | Pops the last value from the arrayref. | ||||||||||||
| 166 | |||||||||||||
| 167 | =cut | ||||||||||||
| 168 | |||||||||||||
| 169 | 2 | ▶ | sub pop : method { | ||||||||||
|
Subroutine
pop: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||
| 170 | 2 | my $self = shift; | |||||||||||
| 171 | 2 | my $v = pop(@{$self->{data}}); | |||||||||||
| 172 | 2 | $self->invoke_event(pop => $v); | |||||||||||
| 173 | 2 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 174 | 2 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 175 | 2 | $w->(drop => $v); | |||||||||||
| 176 | } | ||||||||||||
| 177 | } | ||||||||||||
| 178 | 2 | return $v; | |||||||||||
| 179 | } | ||||||||||||
| 180 | |||||||||||||
| 181 | =head2 unshift | ||||||||||||
| 182 | |||||||||||||
| 183 | Unshifts a value onto the start of the arrayref. | ||||||||||||
| 184 | |||||||||||||
| 185 | =cut | ||||||||||||
| 186 | |||||||||||||
| 187 | 0 | ▶ | sub unshift : method { | ||||||||||
|
Subroutine
unshift: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||
| 188 | 0 | my $self = shift; | |||||||||||
| 189 | 0 | my $v = unshift @{$self->{data}}, @_; | |||||||||||
| 190 | 0 | $self->invoke_event(unshift => @_); | |||||||||||
| 191 | 0 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 192 | 0 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 193 | 0 | $w->(add => $_) foreach @_; | |||||||||||
| 194 | } | ||||||||||||
| 195 | } | ||||||||||||
| 196 | 0 | return $self; | |||||||||||
| 197 | } | ||||||||||||
| 198 | |||||||||||||
| 199 | =head2 join | ||||||||||||
| 200 | |||||||||||||
| 201 | Joins the entries in the arrayref using the given value and returns as a scalar. | ||||||||||||
| 202 | |||||||||||||
| 203 | =cut | ||||||||||||
| 204 | |||||||||||||
| 205 | 4 | ▶ | sub join : method { | ||||||||||
|
Subroutine
join: called CC=3 SCAR=11.8
Pod
documented
| |||||||||||||
| 206 | 4 | my $self = shift; | |||||||||||
| 207 | 4 | my $sep = shift; | |||||||||||
| 208 | 4 | my $joined = join($sep, @{$self->{data}}); | |||||||||||
| 209 | 4 | ▶ | if(@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 210 | 0 | $_->($joined) for @_; | |||||||||||
| 211 | 0 | return $self; | |||||||||||
| 212 | } | ||||||||||||
| 213 | 4 | return $joined; | |||||||||||
| 214 | } | ||||||||||||
| 215 | |||||||||||||
| 216 | =head2 each | ||||||||||||
| 217 | |||||||||||||
| 218 | Perform coderef on each entry in the arrayref. | ||||||||||||
| 219 | |||||||||||||
| 220 | =cut | ||||||||||||
| 221 | |||||||||||||
| 222 | 0 | ▶ | sub each : method { | ||||||||||
|
Subroutine
each: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 223 | 0 | my ($self, $code) = @_; | |||||||||||
| 224 | 0 | $code->($_) for @{$self->{data}}; | |||||||||||
| 225 | 0 | return $self; | |||||||||||
| 226 | } | ||||||||||||
| 227 | |||||||||||||
| 228 | =head2 first | ||||||||||||
| 229 | |||||||||||||
| 230 | Returns the first entry in the arrayref. | ||||||||||||
| 231 | |||||||||||||
| 232 | =cut | ||||||||||||
| 233 | |||||||||||||
| 234 | 0 | ▶ | sub first { | ||||||||||
|
Subroutine
first: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 235 | 0 | my $self = shift; | |||||||||||
| 236 | 0 | ▶ | if(@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 237 | 0 | $_->($self->{data}[0]) for @_; | |||||||||||
| 238 | 0 | return $self; | |||||||||||
| 239 | } | ||||||||||||
| 240 | 0 | return $self->{data}[0]; | |||||||||||
| 241 | } | ||||||||||||
| 242 | |||||||||||||
| 243 | =head2 last | ||||||||||||
| 244 | |||||||||||||
| 245 | Returns the last entry in the arrayref. | ||||||||||||
| 246 | |||||||||||||
| 247 | =cut | ||||||||||||
| 248 | |||||||||||||
| 249 | 0 | ▶ | sub last { | ||||||||||
|
Subroutine
last: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 250 | 0 | my $self = shift; | |||||||||||
| 251 | 0 | ▶ | if(@_) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 252 | 0 | $_->($self->{data}[-1]) for @_; | |||||||||||
| 253 | 0 | return $self; | |||||||||||
| 254 | } | ||||||||||||
| 255 | 0 | return $self->{data}[-1]; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | =head2 grep | ||||||||||||
| 259 | |||||||||||||
| 260 | Calls the coderef on each entry in the arrayref and returns the entries for which it returns true. | ||||||||||||
| 261 | |||||||||||||
| 262 | =cut | ||||||||||||
| 263 | |||||||||||||
| 264 | 0 | ▶ | sub grep : method { | ||||||||||
|
Subroutine
grep: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 265 | 0 | my ($self, $match) = @_; | |||||||||||
| 266 | 0 | return ref($self)->new([ grep { $match->($_) } @{$self->{data}} ]); | |||||||||||
| 267 | } | ||||||||||||
| 268 | |||||||||||||
| 269 | =head2 remove | ||||||||||||
| 270 | |||||||||||||
| 271 | Remove entries from the array. | ||||||||||||
| 272 | |||||||||||||
| 273 | Avoid rebuilding the array in case we have weak refs, just splice out the values | ||||||||||||
| 274 | indicated. | ||||||||||||
| 275 | |||||||||||||
| 276 | =cut | ||||||||||||
| 277 | |||||||||||||
| 278 | 2 | ▶ | sub remove : method { | ||||||||||
|
Subroutine
remove: called CC=6 SCAR=18.2
Pod
documented
| |||||||||||||
| 279 | 2 | my ($self, $check) = @_; | |||||||||||
| 280 | 2 | my $idx = 0; | |||||||||||
| 281 | 2 | ▶ | while($idx < scalar @{$self->{data}}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 282 | 9 | my $match; | |||||||||||
| 283 | 9 | ▶ | if(ref $check eq 'CODE') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 284 | 4 | $match = $check->($self->{data}->[$idx]); | |||||||||||
| 285 | } else { | ||||||||||||
| 286 | 5 | $match = $self->{data}[$idx] eq $check; | |||||||||||
| 287 | } | ||||||||||||
| 288 | 9 | ▶ | if($match) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 289 | 2 | my ($el) = splice @{$self->{data}}, $idx, 1; | |||||||||||
| 290 | 2 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 291 | 0 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 292 | 0 | $w->(drop => $el); | |||||||||||
| 293 | } | ||||||||||||
| 294 | } | ||||||||||||
| 295 | } else { | ||||||||||||
| 296 | 7 | ++$idx; | |||||||||||
| 297 | } | ||||||||||||
| 298 | } | ||||||||||||
| 299 | 2 | return $self; | |||||||||||
| 300 | } | ||||||||||||
| 301 | |||||||||||||
| 302 | =head2 clear | ||||||||||||
| 303 | |||||||||||||
| 304 | Empty the arrayref. | ||||||||||||
| 305 | |||||||||||||
| 306 | =cut | ||||||||||||
| 307 | |||||||||||||
| 308 | 1 | ▶ | sub clear : method { | ||||||||||
|
Subroutine
clear: called CC=4 SCAR=18.9
Pod
documented
| |||||||||||||
| 309 | 1 | my $self = shift; | |||||||||||
| 310 | 1 | ▶ | if($self->{onchange}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 311 | 0 | my @el = @{ $self->{data} }; | |||||||||||
| 312 | 0 | foreach my $w (@{$self->{onchange}}) { | |||||||||||
| 313 | 0 | $w->(drop => $_) for @el; | |||||||||||
| 314 | } | ||||||||||||
| 315 | } | ||||||||||||
| 316 | 1 | $self->{data} = [ ]; | |||||||||||
| 317 | 1 | return $self; | |||||||||||
| 318 | } | ||||||||||||
| 319 | |||||||||||||
| 320 | =head2 arrayref | ||||||||||||
| 321 | |||||||||||||
| 322 | Returns the arrayref directly. | ||||||||||||
| 323 | |||||||||||||
| 324 | =cut | ||||||||||||
| 325 | |||||||||||||
| 326 | 0 | ▶ | sub arrayref { | ||||||||||
|
Subroutine
arrayref: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 327 | 0 | my ($self) = @_; | |||||||||||
| 328 | 0 | return $self->{data}; | |||||||||||
| 329 | } | ||||||||||||
| 330 | |||||||||||||
| 331 | =head2 is_empty | ||||||||||||
| 332 | |||||||||||||
| 333 | Returns true if there's nothing in the arrayref. | ||||||||||||
| 334 | |||||||||||||
| 335 | =cut | ||||||||||||
| 336 | |||||||||||||
| 337 | 0 | ▶ | sub is_empty { | ||||||||||
|
Subroutine
is_empty: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 338 | 0 | my $self = shift; | |||||||||||
| 339 | 0 | return !$self->count; | |||||||||||
| 340 | } | ||||||||||||
| 341 | |||||||||||||
| 342 | 5 | 1; | |||||||||||
| 343 | |||||||||||||
| 344 | __END__ | ||||||||||||