| 1 | package Mojo::Util::Collection; | ||||||||||||||||||||||||||||
| 2 | 32 | ▶ | use Mojo::Base -base; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 32 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 32 | ▶ | use Mojo::Util::Model; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 32 | our @EXPORT_OK = qw( | |||||||||||||||||||||||||||
| 8 | collect | ||||||||||||||||||||||||||||
| 9 | ); | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 32 | our $VERSION = '0.0.19'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 32 | ▶ | use List::Util; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 32 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 32 | ▶ | use Mojo::Util::Collection::Comparator; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 32 | ▶ | use Mojo::Util::Collection::Formatter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 32 | ▶ | use Mojo::Util::Model; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 8 | ▶ | has 'comparator' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 8 | return Mojo::Util::Collection::Comparator->new; | |||||||||||||||||||||||||||
| 23 | 32 | }; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 4 | ▶ | has 'formatter' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | 4 | return Mojo::Util::Collection::Formatter->new; | |||||||||||||||||||||||||||
| 27 | 32 | }; | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 32 | has 'index' => 0; | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 0 | ▶ | has 'items' => sub { [] }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 32 | has 'limit' => 10; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 1 | ▶ | has 'model' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 1 | return Mojo::Util::Model->new; | |||||||||||||||||||||||||||
| 37 | 32 | }; | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 33 | ▶ | has 'objects' => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.2
| |||||||||||||||||||||||||||||
| 40 | 33 | my $self = shift; | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 33 | ▶ | my @objects = map { $self->newObject(%{$_ || {}}) } @{ $self->items || [] }; | ||||||||||||||||||||||||||
|
Condition50%2/4
$_ || {}
$self->items || []
MC/DC0%0/1
$_ || {}
$_
MC/DC0%0/1
$self->items || []
$self->items
Truth table50%1/2
A
A= $_
Truth table50%1/2
A
A= $self->items
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 33 | return \@objects; | |||||||||||||||||||||||||||
| 45 | 32 | }; | |||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | 0 | ▶ | has 'pager' => sub { {} }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | =head2 add | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | Add an object | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | Returns: | ||||||||||||||||||||||||||||
| 55 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | =cut | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 6 | ▶ | sub add { | ||||||||||||||||||||||||||
|
Subroutine
add: called CC=7 SCAR=19.7
Pod
documented
| |||||||||||||||||||||||||||||
| 60 | 6 | my ($self, $object) = @_; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 6 | ▶ | if (ref($object) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 1 | $self->add($_) for (@$object); | |||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1 | return $self; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | 5 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 5 | ▶ | if (ref($object) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 71 | 3 | push(@objects, $self->newObject(%$object)); | |||||||||||||||||||||||||||
| 72 | } elsif (blessed($object)) { | ||||||||||||||||||||||||||||
| 73 | 2 | ▶ | if ($object->isa('Mojo::Util::Collection')) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 74 | 1 | push(@objects, @{ $object->objects }); | |||||||||||||||||||||||||||
| 75 | } elsif ($object->isa('Mojo::Util::Model')) { | ||||||||||||||||||||||||||||
| 76 | 1 | push(@objects, $object); | |||||||||||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||||||||||
| 78 | 0 | warn "Can't add object of type " . ref($object); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | } else { | ||||||||||||||||||||||||||||
| 81 | 0 | warn "Can't add object of type " . ref($object); | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 5 | $self->objects(\@objects); | |||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 5 | return $self; | |||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =head2 as | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Set model for collection | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | =cut | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 34 | ▶ | sub as { | ||||||||||||||||||||||||||
|
Subroutine
as: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 96 | 34 | my ($self, $as) = @_; | |||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | 34 | $self->model($as); | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 34 | return $self; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | =head2 asOptions | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | Return an array ref containing [{ value => $value, label => $label }, ...] | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | =cut | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 2 | ▶ | sub asOptions { | ||||||||||||||||||||||||||
|
Subroutine
asOptions: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 2 | return $self->formatter->asOptions($self->objects, @_); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =head2 avg | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Return the average value for given $field | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | =cut | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub avg { | ||||||||||||||||||||||||||
|
Subroutine
avg: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 123 | 0 | my $values = $self->lists($field); | |||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | 0 | return List::Util::sum(@$values) / scalar(@$values); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | =head2 collect | ||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | Instantiate a new collection | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | sub collect { | ||||||||||||||||||||||||||
|
Subroutine
collect: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 135 | 2 | my $items = shift; | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 2 | return Mojo::Util::Collection->new({ items => $items }); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =head2 count | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Return the size of objects | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 40 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 147 | 40 | return (scalar(@{ shift->objects })); | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | =head2 each | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | Map through each object and call the callback | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 1 | ▶ | sub each { | ||||||||||||||||||||||||||
|
Subroutine
each: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 157 | 1 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 1 | $callback->($_) for (@{ $self->objects }); | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | =head2 exclude | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | Exclude objects that doesn't match criteria | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | Returns: | ||||||||||||||||||||||||||||
| 167 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | 5 | ▶ | sub exclude { | ||||||||||||||||||||||||||
|
Subroutine
exclude: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 172 | 5 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 19 | ▶ | my $collection = $self->filter(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 175 | 19 | my $object = shift; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 19 | ▶ | return List::Util::notall { $self->comparator->verify($object->get($_), $args->{ $_ }) } keys(%$args); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 178 | 5 | }); | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 5 | return $collection; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | =head2 filter | ||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||
| 185 | Filter objects by callback | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | Returns: | ||||||||||||||||||||||||||||
| 188 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | =cut | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 29 | ▶ | sub filter { | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 193 | 29 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||
| 195 | 29 | my @objects = grep { $callback->($_) } @{ $self->objects }; | |||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 29 | return $self->new({ objects => \@objects })->as($self->model); | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | =head2 find | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | Find object by primary key, of if args is a hash ref then find first object matching given args | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | Returns: | ||||||||||||||||||||||||||||
| 205 | C<Model> object | ||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||
| 207 | =cut | ||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 8 | ▶ | sub find { | ||||||||||||||||||||||||||
|
Subroutine
find: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 210 | 8 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 8 | ▶ | if (ref($args) eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 213 | 6 | ▶ | return $self->__first(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 214 | 6 | my $object = shift; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 6 | ▶ | return List::Util::all { $self->comparator->verify($object->get($_), $args->{ $_ }) } keys(%$args); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 217 | 3 | }); | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | 11 | ▶ | my $object = $self->__first(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 221 | 11 | my $object = shift; | |||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||
| 223 | 11 | return ($object->pk eq $args); | |||||||||||||||||||||||||||
| 224 | 5 | }); | |||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 5 | return $object; | |||||||||||||||||||||||||||
| 227 | } | ||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||
| 229 | =head2 findOrNew | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | Find object or create a new instance | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | Returns: | ||||||||||||||||||||||||||||
| 234 | C<Model> object | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | =cut | ||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||
| 238 | 2 | ▶ | sub findOrNew { | ||||||||||||||||||||||||||
|
Subroutine
findOrNew: called CC=4 SCAR=14.5
Pod
documented
| |||||||||||||||||||||||||||||
| 239 | 2 | my ($self, $args, $extra) = @_; | |||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | 2 | ▶ | my %new_args = (%{ $args || {} }, %{ $extra || {} }); | ||||||||||||||||||||||||||
|
Condition50%2/4
$args || {}
$extra || {}
MC/DC0%0/1
$args || {}
$args
MC/DC0%0/1
$extra || {}
$extra
Truth table50%1/2
A
A= $args
Truth table50%1/2
A
A= $extra
| |||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | 2 | ▶ | return $self->find($args) || $self->newObject(%new_args); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->find($args) || $self->newObject(%new_args)
MC/DC0%0/2
$self->find($args) || $self->newObject(%new_args)
$self->find($args)
$self->newObject(%new_args)
Truth table66%2/3
A || B
A= $self->find($args)B= $self->newObject(%new_args)
| |||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | =head2 first | ||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | Get first object | ||||||||||||||||||||||||||||
| 249 | |||||||||||||||||||||||||||||
| 250 | Returns: | ||||||||||||||||||||||||||||
| 251 | C<Model> object | ||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||
| 253 | =cut | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 8 | ▶ | sub first { | ||||||||||||||||||||||||||
|
Subroutine
first: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 256 | 8 | return shift->get(0); | |||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | =head2 firstOrNew | ||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||
| 261 | Get first object if exists, otherwise create one | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | Returns: | ||||||||||||||||||||||||||||
| 264 | C<Model> object | ||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||
| 266 | =cut | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 2 | ▶ | sub firstOrNew { | ||||||||||||||||||||||||||
|
Subroutine
firstOrNew: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||
| 269 | 2 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 2 | ▶ | return $self->first || $self->newObject(%$args); | ||||||||||||||||||||||||||
|
Condition66%2/3
$self->first || $self->newObject(%$args)
MC/DC0%0/2
$self->first || $self->newObject(%$args)
$self->first
$self->newObject(%$args)
Truth table66%2/3
A || B
A= $self->firstB= $self->newObject(%$args)
| |||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | =head2 get | ||||||||||||||||||||||||||||
| 275 | |||||||||||||||||||||||||||||
| 276 | Get object by index | ||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||
| 278 | Returns: | ||||||||||||||||||||||||||||
| 279 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||
| 281 | =cut | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | 29 | ▶ | sub get { | ||||||||||||||||||||||||||
|
Subroutine
get: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 284 | 29 | my ($self, $index) = @_; | |||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||
| 286 | 29 | return $self->objects->[$index]; | |||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||
| 289 | =head2 indexOf | ||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||
| 291 | Get the index of an object | ||||||||||||||||||||||||||||
| 292 | |||||||||||||||||||||||||||||
| 293 | Returns: | ||||||||||||||||||||||||||||
| 294 | Integer index | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | =cut | ||||||||||||||||||||||||||||
| 297 | |||||||||||||||||||||||||||||
| 298 | 4 | ▶ | sub indexOf { | ||||||||||||||||||||||||||
|
Subroutine
indexOf: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 299 | 4 | my ($self, $object) = @_; | |||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||
| 301 | 4 | my $index = -1; | |||||||||||||||||||||||||||
| 302 | 4 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 4 | ▶ | if (ref($object) ne ref($self->model)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 305 | 1 | $object = $self->newObject(%$object); | |||||||||||||||||||||||||||
| 306 | } | ||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||
| 308 | 4 | ▶ | for (my $i = 0; $i < scalar(@objects); $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 309 | 6 | ▶ | if ($objects[$i]->pk eq $object->pk) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | 4 | $index = $i; | |||||||||||||||||||||||||||
| 311 | 4 | last; | |||||||||||||||||||||||||||
| 312 | } | ||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||
| 315 | 4 | return $index; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | =head2 intersect | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | Return a new collection containing only items that exist in both collections. | ||||||||||||||||||||||||||||
| 321 | Uses $key to compare objects (defaults to primary_key). | ||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | Returns: | ||||||||||||||||||||||||||||
| 324 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | =cut | ||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | 0 | ▶ | sub intersect { | ||||||||||||||||||||||||||
|
Subroutine
intersect: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 329 | 0 | my ($self, $other_collection, $key) = @_; | |||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||
| 331 | 0 | ▶ | $key ||= $self->model->primary_key; | ||||||||||||||||||||||||||
|
Condition0%0/3
$key ||= $self->model->primary_key
MC/DC0%0/2
$key ||= $self->model->primary_key
$key
$self->model->primary_key
Truth table0%0/3
A ||= B
A= $keyB= $self->model->primary_key
| |||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||
| 333 | 0 | return $self->search({ | |||||||||||||||||||||||||||
| 334 | $key => $other_collection->lists($key), | ||||||||||||||||||||||||||||
| 335 | }); | ||||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||
| 338 | =head2 last | ||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | Get last object | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | Returns: | ||||||||||||||||||||||||||||
| 343 | C<Model> object | ||||||||||||||||||||||||||||
| 344 | |||||||||||||||||||||||||||||
| 345 | =cut | ||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||
| 347 | 7 | ▶ | sub last { | ||||||||||||||||||||||||||
|
Subroutine
last: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 348 | 7 | my $self= shift; | |||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | 7 | return $self->get(scalar(@{ $self->objects }) - 1); | |||||||||||||||||||||||||||
| 351 | } | ||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||
| 353 | =head2 lists | ||||||||||||||||||||||||||||
| 354 | |||||||||||||||||||||||||||||
| 355 | Return an array ref containing all the fields from all objects for the given $field. | ||||||||||||||||||||||||||||
| 356 | Return a hash ref containing $key_field => $value_field if both are given. | ||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | =cut | ||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||
| 360 | 7 | ▶ | sub lists { | ||||||||||||||||||||||||||
|
Subroutine
lists: called CC=3 SCAR=13.6
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | 7 | my ($self, $key_field, $value_field) = @_; | |||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||
| 363 | 7 | ▶ | if ($key_field && $value_field) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$key_field and $value_field
MC/DC0%0/2
$key_field and $value_field
$key_field
$value_field
Truth table33%1/3
A and B
A= $key_fieldB= $value_field
| |||||||||||||||||||||||||||||
| 364 | 0 | my %list = map { $_->get($key_field) => $_->get($value_field) } @{ $self->objects }; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 0 | return \%list; | |||||||||||||||||||||||||||
| 367 | } | ||||||||||||||||||||||||||||
| 368 | |||||||||||||||||||||||||||||
| 369 | 7 | my @fields = map { $self->__list($_->get($key_field)) } @{ $self->objects }; | |||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||
| 371 | 7 | return \@fields; | |||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | =head2 max | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | Return the maximum value for given $field | ||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | =cut | ||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | ▶ | sub max { | ||||||||||||||||||||||||||
|
Subroutine
max: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 381 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 382 | 0 | my $values = $self->lists($field); | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | return List::Util::max(@$values); | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||
| 387 | =head2 min | ||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||
| 389 | Return the minimum value for given $field | ||||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | =cut | ||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||
| 393 | 0 | ▶ | sub min { | ||||||||||||||||||||||||||
|
Subroutine
min: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 394 | 0 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 395 | 0 | my $values = $self->lists($field); | |||||||||||||||||||||||||||
| 396 | |||||||||||||||||||||||||||||
| 397 | 0 | return List::Util::min(@$values); | |||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | =head2 missing | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | Return a new collection containing only items that exist in this collection | ||||||||||||||||||||||||||||
| 403 | but not in the other collection. | ||||||||||||||||||||||||||||
| 404 | Uses $key to compare objects (defaults to primary_key). | ||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||
| 406 | Returns: | ||||||||||||||||||||||||||||
| 407 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||
| 409 | =cut | ||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||
| 411 | 0 | ▶ | sub missing { | ||||||||||||||||||||||||||
|
Subroutine
missing: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 412 | 0 | my ($self, $other_collection, $key) = @_; | |||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||
| 414 | 0 | ▶ | $key ||= $self->model->primary_key; | ||||||||||||||||||||||||||
|
Condition0%0/3
$key ||= $self->model->primary_key
MC/DC0%0/2
$key ||= $self->model->primary_key
$key
$self->model->primary_key
Truth table0%0/3
A ||= B
A= $keyB= $self->model->primary_key
| |||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | return $self->exclude({ | |||||||||||||||||||||||||||
| 417 | $key => $other_collection->lists($key), | ||||||||||||||||||||||||||||
| 418 | }); | ||||||||||||||||||||||||||||
| 419 | } | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =head2 newObject | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | Instantiate new object | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | Returns: | ||||||||||||||||||||||||||||
| 426 | C<Model> object | ||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||
| 428 | =cut | ||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | 139 | ▶ | sub newObject { | ||||||||||||||||||||||||||
|
Subroutine
newObject: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 431 | 139 | my $self = shift; | |||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | 139 | ▶ | if (ref($self->model) eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 434 | 6 | return $self->model->(@_); | |||||||||||||||||||||||||||
| 435 | } | ||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||
| 437 | 133 | return $self->model->new(@_); | |||||||||||||||||||||||||||
| 438 | } | ||||||||||||||||||||||||||||
| 439 | |||||||||||||||||||||||||||||
| 440 | =head2 next | ||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||
| 442 | Get next object | ||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||
| 444 | Returns: | ||||||||||||||||||||||||||||
| 445 | C<Model> object | ||||||||||||||||||||||||||||
| 446 | |||||||||||||||||||||||||||||
| 447 | =cut | ||||||||||||||||||||||||||||
| 448 | |||||||||||||||||||||||||||||
| 449 | 5 | ▶ | sub next { | ||||||||||||||||||||||||||
|
Subroutine
next: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 450 | 5 | my $self = shift; | |||||||||||||||||||||||||||
| 451 | 5 | my $index = $self->index; | |||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||
| 453 | 5 | $self->index($index + 1); | |||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 5 | my $object = $self->get($index); | |||||||||||||||||||||||||||
| 456 | |||||||||||||||||||||||||||||
| 457 | # Auto reset | ||||||||||||||||||||||||||||
| 458 | 5 | ▶ | if (! $object) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 459 | 1 | $self->reset; | |||||||||||||||||||||||||||
| 460 | } | ||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||
| 462 | 5 | return $object; | |||||||||||||||||||||||||||
| 463 | } | ||||||||||||||||||||||||||||
| 464 | |||||||||||||||||||||||||||||
| 465 | =head2 only | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | Return an array ref containing only given @keys | ||||||||||||||||||||||||||||
| 468 | |||||||||||||||||||||||||||||
| 469 | =cut | ||||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | 10 | ▶ | sub only { | ||||||||||||||||||||||||||
|
Subroutine
only: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 472 | 10 | my ($self, @keys) = @_; | |||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||
| 474 | 10 | my $only = []; | |||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||
| 476 | 10 | foreach my $object (@{ $self->objects }) { | |||||||||||||||||||||||||||
| 477 | 28 | push @$only, { map { $_ => $object->get($_) } @keys }; | |||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 10 | return $only; | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | =head2 orderBy | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | Order collection | ||||||||||||||||||||||||||||
| 486 | |||||||||||||||||||||||||||||
| 487 | =cut | ||||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | 2 | ▶ | sub orderBy { | ||||||||||||||||||||||||||
|
Subroutine
orderBy: called CC=6 SCAR=18.7
Pod
documented
| |||||||||||||||||||||||||||||
| 490 | 2 | my ($self, $field, $direction, $string) = @_; | |||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||
| 492 | 2 | ▶ | $direction ||= 'asc'; | ||||||||||||||||||||||||||
|
Condition100%2/2
$direction ||= "asc"
MC/DC100%1/1
$direction ||= "asc"
$direction
Truth table100%2/2
A
A= $direction
| |||||||||||||||||||||||||||||
| 493 | 2 | ▶ | $string = 1 unless defined $string; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 494 | |||||||||||||||||||||||||||||
| 495 | 2 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 496 | 2 | my @sorted; | |||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 2 | ▶ | if ($direction eq 'asc') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 499 | 1 | ▶ | if ($string) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 500 | 1 | @sorted = sort { $a->get($field, '') cmp $b->get($field, '') } @objects; | |||||||||||||||||||||||||||
| 501 | } | ||||||||||||||||||||||||||||
| 502 | else { | ||||||||||||||||||||||||||||
| 503 | 0 | @sorted = sort { $a->get($field, '') <=> $b->get($field, '') } @objects; | |||||||||||||||||||||||||||
| 504 | } | ||||||||||||||||||||||||||||
| 505 | } else { | ||||||||||||||||||||||||||||
| 506 | 1 | ▶ | if ($string) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 507 | 1 | @sorted = sort { $b->get($field, '') cmp $a->get($field, '')} @objects; | |||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||
| 509 | else { | ||||||||||||||||||||||||||||
| 510 | 0 | @sorted = sort { $b->get($field, '') <=> $a->get($field, '')} @objects; | |||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||
| 512 | } | ||||||||||||||||||||||||||||
| 513 | |||||||||||||||||||||||||||||
| 514 | 2 | $self->objects(\@sorted); | |||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 2 | return $self; | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | =head2 page | ||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||
| 521 | Take a collection containing only the results from a given page | ||||||||||||||||||||||||||||
| 522 | |||||||||||||||||||||||||||||
| 523 | Returns: | ||||||||||||||||||||||||||||
| 524 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 525 | |||||||||||||||||||||||||||||
| 526 | =cut | ||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||
| 528 | 3 | ▶ | sub page { | ||||||||||||||||||||||||||
|
Subroutine
page: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||
| 529 | 3 | my ($self, $page) = @_; | |||||||||||||||||||||||||||
| 530 | 3 | ▶ | $page ||= 1; | ||||||||||||||||||||||||||
|
Condition50%1/2
$page ||= 1
MC/DC0%0/1
$page ||= 1
$page
Truth table50%1/2
A
A= $page
| |||||||||||||||||||||||||||||
| 531 | |||||||||||||||||||||||||||||
| 532 | 3 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||
| 534 | 3 | my $start = ($page - 1) * $self->limit; | |||||||||||||||||||||||||||
| 535 | 3 | my $end = List::Util::min(($page * $self->limit), scalar(@objects)) - 1; | |||||||||||||||||||||||||||
| 536 | |||||||||||||||||||||||||||||
| 537 | 3 | my $last_page = int((scalar(@objects) + $self->limit - 1) / $self->limit); | |||||||||||||||||||||||||||
| 538 | |||||||||||||||||||||||||||||
| 539 | 3 | ▶ | my $pager = { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 540 | count => $self->count, | ||||||||||||||||||||||||||||
| 541 | limit => $self->limit, | ||||||||||||||||||||||||||||
| 542 | prev_page => ($page > 1) ? $page - 1 : 1, | ||||||||||||||||||||||||||||
| 543 | first_page => 1, | ||||||||||||||||||||||||||||
| 544 | page => $page, | ||||||||||||||||||||||||||||
| 545 | next_page => ($page < $last_page) ? $page + 1 : $last_page, | ||||||||||||||||||||||||||||
| 546 | last_page => $last_page, | ||||||||||||||||||||||||||||
| 547 | start => $start + 1, | ||||||||||||||||||||||||||||
| 548 | end => $end + 1, | ||||||||||||||||||||||||||||
| 549 | }; | ||||||||||||||||||||||||||||
| 550 | |||||||||||||||||||||||||||||
| 551 | 3 | my $next = $self->slice($start, $end); | |||||||||||||||||||||||||||
| 552 | |||||||||||||||||||||||||||||
| 553 | 3 | $next->pager($pager); | |||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||
| 555 | 3 | return $next; | |||||||||||||||||||||||||||
| 556 | } | ||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||
| 558 | =head2 remove | ||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||
| 560 | Remove an object | ||||||||||||||||||||||||||||
| 561 | |||||||||||||||||||||||||||||
| 562 | =cut | ||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||
| 564 | 1 | ▶ | sub remove { | ||||||||||||||||||||||||||
|
Subroutine
remove: called CC=4 SCAR=16.6
Pod
documented
| |||||||||||||||||||||||||||||
| 565 | 1 | my ($self, $object) = @_; | |||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||
| 567 | 1 | ▶ | if (ref($object) eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 568 | 0 | $self->remove($_) for (@{ $object }); | |||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||
| 570 | 0 | return $self; | |||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||
| 573 | 1 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||
| 575 | # Remove only if exists | ||||||||||||||||||||||||||||
| 576 | 1 | ▶ | if ($self->indexOf($object) >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 577 | 1 | return $self->splice($self->indexOf($object), 1); | |||||||||||||||||||||||||||
| 578 | } | ||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||
| 580 | 0 | return $self; | |||||||||||||||||||||||||||
| 581 | } | ||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||
| 583 | =head2 reset | ||||||||||||||||||||||||||||
| 584 | |||||||||||||||||||||||||||||
| 585 | Reset index | ||||||||||||||||||||||||||||
| 586 | |||||||||||||||||||||||||||||
| 587 | =cut | ||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||
| 589 | 1 | ▶ | sub reset { | ||||||||||||||||||||||||||
|
Subroutine
reset: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 590 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 591 | |||||||||||||||||||||||||||||
| 592 | 1 | $self->index(0); | |||||||||||||||||||||||||||
| 593 | |||||||||||||||||||||||||||||
| 594 | 1 | return $self; | |||||||||||||||||||||||||||
| 595 | } | ||||||||||||||||||||||||||||
| 596 | |||||||||||||||||||||||||||||
| 597 | =head2 search | ||||||||||||||||||||||||||||
| 598 | |||||||||||||||||||||||||||||
| 599 | Search objects by given args | ||||||||||||||||||||||||||||
| 600 | |||||||||||||||||||||||||||||
| 601 | Returns: | ||||||||||||||||||||||||||||
| 602 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 603 | |||||||||||||||||||||||||||||
| 604 | =cut | ||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||
| 606 | 22 | ▶ | sub search { | ||||||||||||||||||||||||||
|
Subroutine
search: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 607 | 22 | my ($self, $args) = @_; | |||||||||||||||||||||||||||
| 608 | |||||||||||||||||||||||||||||
| 609 | 88 | ▶ | my $collection = $self->filter(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 610 | 88 | my $object = shift; | |||||||||||||||||||||||||||
| 611 | |||||||||||||||||||||||||||||
| 612 | 88 | ▶ | return List::Util::all { $self->comparator->verify($object->get($_), $args->{ $_ }) } keys(%$args); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 613 | 22 | }); | |||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||
| 615 | 22 | return $collection; | |||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||
| 618 | =head2 slice | ||||||||||||||||||||||||||||
| 619 | |||||||||||||||||||||||||||||
| 620 | Take a slice from the collection | ||||||||||||||||||||||||||||
| 621 | |||||||||||||||||||||||||||||
| 622 | Returns: | ||||||||||||||||||||||||||||
| 623 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 624 | |||||||||||||||||||||||||||||
| 625 | =cut | ||||||||||||||||||||||||||||
| 626 | |||||||||||||||||||||||||||||
| 627 | 4 | ▶ | sub slice { | ||||||||||||||||||||||||||
|
Subroutine
slice: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||||||||||||
| 628 | 4 | my ($self, $start, $end) = @_; | |||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||
| 630 | 4 | ▶ | $start //= 0; | ||||||||||||||||||||||||||
|
Condition50%1/2
$start //= 0
MC/DC0%0/1
$start //= 0
$start
Truth table50%1/2
A
A= $start
| |||||||||||||||||||||||||||||
| 631 | 4 | ▶ | $end //= $self->count - $start; | ||||||||||||||||||||||||||
|
Condition50%1/2
$end //= $self->count - $start
MC/DC0%0/1
$end //= $self->count - $start
$end
Truth table50%1/2
A
A= $end
| |||||||||||||||||||||||||||||
| 632 | |||||||||||||||||||||||||||||
| 633 | 4 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 634 | 4 | my @partition = @objects[$start .. $end]; | |||||||||||||||||||||||||||
| 635 | |||||||||||||||||||||||||||||
| 636 | 4 | return $self->new({ objects => \@partition })->as($self->model); | |||||||||||||||||||||||||||
| 637 | } | ||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||
| 639 | =head2 splice | ||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||
| 641 | Splice objects | ||||||||||||||||||||||||||||
| 642 | |||||||||||||||||||||||||||||
| 643 | =cut | ||||||||||||||||||||||||||||
| 644 | |||||||||||||||||||||||||||||
| 645 | 4 | ▶ | sub splice { | ||||||||||||||||||||||||||
|
Subroutine
splice: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 646 | 4 | my ($self, $offset, $length) = @_; | |||||||||||||||||||||||||||
| 647 | |||||||||||||||||||||||||||||
| 648 | 4 | ▶ | if (! $length) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 649 | 2 | ▶ | $length = $offset || 1; | ||||||||||||||||||||||||||
|
Condition100%2/2
$offset || 1
MC/DC100%1/1
$offset || 1
$offset
Truth table100%2/2
A
A= $offset
| |||||||||||||||||||||||||||||
| 650 | 2 | $offset = 0; | |||||||||||||||||||||||||||
| 651 | } | ||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||
| 653 | 4 | my @objects = @{ $self->objects }; | |||||||||||||||||||||||||||
| 654 | |||||||||||||||||||||||||||||
| 655 | 4 | splice(@objects, $offset, $length); | |||||||||||||||||||||||||||
| 656 | |||||||||||||||||||||||||||||
| 657 | 4 | $self->objects(\@objects); | |||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||
| 659 | 4 | return $self; | |||||||||||||||||||||||||||
| 660 | } | ||||||||||||||||||||||||||||
| 661 | |||||||||||||||||||||||||||||
| 662 | =head2 sum | ||||||||||||||||||||||||||||
| 663 | |||||||||||||||||||||||||||||
| 664 | Sum by $field | ||||||||||||||||||||||||||||
| 665 | |||||||||||||||||||||||||||||
| 666 | =cut | ||||||||||||||||||||||||||||
| 667 | |||||||||||||||||||||||||||||
| 668 | 1 | ▶ | sub sum { | ||||||||||||||||||||||||||
|
Subroutine
sum: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 669 | 1 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 670 | 1 | my $values = $self->lists($field); | |||||||||||||||||||||||||||
| 671 | |||||||||||||||||||||||||||||
| 672 | 1 | return List::Util::sum(@$values); | |||||||||||||||||||||||||||
| 673 | } | ||||||||||||||||||||||||||||
| 674 | |||||||||||||||||||||||||||||
| 675 | =head2 toArray | ||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||
| 677 | Convert collection to array ref | ||||||||||||||||||||||||||||
| 678 | |||||||||||||||||||||||||||||
| 679 | =cut | ||||||||||||||||||||||||||||
| 680 | |||||||||||||||||||||||||||||
| 681 | 2 | ▶ | sub toArray { | ||||||||||||||||||||||||||
|
Subroutine
toArray: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 682 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 683 | |||||||||||||||||||||||||||||
| 684 | 2 | return $self->formatter->toArray($self->objects, @_); | |||||||||||||||||||||||||||
| 685 | } | ||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||
| 687 | =head2 toCsv | ||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||
| 689 | Convert collection to CSV string | ||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||
| 691 | =cut | ||||||||||||||||||||||||||||
| 692 | |||||||||||||||||||||||||||||
| 693 | 1 | ▶ | sub toCsv { | ||||||||||||||||||||||||||
|
Subroutine
toCsv: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 694 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||
| 696 | 1 | return $self->formatter->toCsv($self->objects, @_); | |||||||||||||||||||||||||||
| 697 | } | ||||||||||||||||||||||||||||
| 698 | |||||||||||||||||||||||||||||
| 699 | =head2 toJson | ||||||||||||||||||||||||||||
| 700 | |||||||||||||||||||||||||||||
| 701 | Convert collection to JSON string | ||||||||||||||||||||||||||||
| 702 | |||||||||||||||||||||||||||||
| 703 | =cut | ||||||||||||||||||||||||||||
| 704 | |||||||||||||||||||||||||||||
| 705 | 2 | ▶ | sub toJson { | ||||||||||||||||||||||||||
|
Subroutine
toJson: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 706 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 707 | |||||||||||||||||||||||||||||
| 708 | 2 | return $self->formatter->toJson($self->objects, @_); | |||||||||||||||||||||||||||
| 709 | } | ||||||||||||||||||||||||||||
| 710 | |||||||||||||||||||||||||||||
| 711 | =head2 touch | ||||||||||||||||||||||||||||
| 712 | |||||||||||||||||||||||||||||
| 713 | Touch collections model | ||||||||||||||||||||||||||||
| 714 | |||||||||||||||||||||||||||||
| 715 | =cut | ||||||||||||||||||||||||||||
| 716 | |||||||||||||||||||||||||||||
| 717 | 0 | ▶ | sub touch { | ||||||||||||||||||||||||||
|
Subroutine
touch: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 718 | 0 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 719 | |||||||||||||||||||||||||||||
| 720 | 0 | my $objects = $self->objects; | |||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||
| 722 | 0 | ▶ | for (my $i = 0; $i < $self->count; $i++) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 723 | 0 | $callback->($objects->[$i]); | |||||||||||||||||||||||||||
| 724 | } | ||||||||||||||||||||||||||||
| 725 | |||||||||||||||||||||||||||||
| 726 | 0 | $self->objects($objects); | |||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||
| 728 | 0 | return $self; | |||||||||||||||||||||||||||
| 729 | } | ||||||||||||||||||||||||||||
| 730 | |||||||||||||||||||||||||||||
| 731 | =head2 unique | ||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||
| 733 | Return an array ref containing only unique values for given $field | ||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||
| 735 | =cut | ||||||||||||||||||||||||||||
| 736 | |||||||||||||||||||||||||||||
| 737 | 1 | ▶ | sub unique { | ||||||||||||||||||||||||||
|
Subroutine
unique: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 738 | 1 | my ($self, $field) = @_; | |||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||
| 740 | 1 | my %fields = map { $_->get($field) => 1 } @{ $self->objects }; | |||||||||||||||||||||||||||
| 741 | |||||||||||||||||||||||||||||
| 742 | 1 | my @unique = keys(%fields); | |||||||||||||||||||||||||||
| 743 | |||||||||||||||||||||||||||||
| 744 | 1 | return \@unique; | |||||||||||||||||||||||||||
| 745 | } | ||||||||||||||||||||||||||||
| 746 | |||||||||||||||||||||||||||||
| 747 | =head2 where | ||||||||||||||||||||||||||||
| 748 | |||||||||||||||||||||||||||||
| 749 | Search objects where field is equal to value | ||||||||||||||||||||||||||||
| 750 | |||||||||||||||||||||||||||||
| 751 | Returns: | ||||||||||||||||||||||||||||
| 752 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 753 | |||||||||||||||||||||||||||||
| 754 | =cut | ||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||
| 756 | 0 | ▶ | sub where { | ||||||||||||||||||||||||||
|
Subroutine
where: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 757 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 758 | |||||||||||||||||||||||||||||
| 759 | 0 | return $self->search(@_); | |||||||||||||||||||||||||||
| 760 | } | ||||||||||||||||||||||||||||
| 761 | |||||||||||||||||||||||||||||
| 762 | =head2 whereIn | ||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||
| 764 | Search objects where field is in $array | ||||||||||||||||||||||||||||
| 765 | |||||||||||||||||||||||||||||
| 766 | Returns: | ||||||||||||||||||||||||||||
| 767 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 768 | |||||||||||||||||||||||||||||
| 769 | =cut | ||||||||||||||||||||||||||||
| 770 | |||||||||||||||||||||||||||||
| 771 | 1 | ▶ | sub whereIn { | ||||||||||||||||||||||||||
|
Subroutine
whereIn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 772 | 1 | my ($self, $field, $array) = @_; | |||||||||||||||||||||||||||
| 773 | |||||||||||||||||||||||||||||
| 774 | 4 | ▶ | my $collection = $self->filter(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 775 | 4 | my $object = shift; | |||||||||||||||||||||||||||
| 776 | |||||||||||||||||||||||||||||
| 777 | 4 | return grep { $object->get($field) eq $_ } @$array; | |||||||||||||||||||||||||||
| 778 | 1 | }); | |||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||
| 780 | 1 | return $collection; | |||||||||||||||||||||||||||
| 781 | } | ||||||||||||||||||||||||||||
| 782 | |||||||||||||||||||||||||||||
| 783 | =head2 whereNotIn | ||||||||||||||||||||||||||||
| 784 | |||||||||||||||||||||||||||||
| 785 | Search objects where field is not in $array | ||||||||||||||||||||||||||||
| 786 | |||||||||||||||||||||||||||||
| 787 | Returns: | ||||||||||||||||||||||||||||
| 788 | C<Collection> of C<Model> objects | ||||||||||||||||||||||||||||
| 789 | |||||||||||||||||||||||||||||
| 790 | =cut | ||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||
| 792 | 1 | ▶ | sub whereNotIn { | ||||||||||||||||||||||||||
|
Subroutine
whereNotIn: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 793 | 1 | my ($self, $field, $array) = @_; | |||||||||||||||||||||||||||
| 794 | |||||||||||||||||||||||||||||
| 795 | 1 | return $self->exclude({ $field => $array }); | |||||||||||||||||||||||||||
| 796 | } | ||||||||||||||||||||||||||||
| 797 | |||||||||||||||||||||||||||||
| 798 | =head2 __first | ||||||||||||||||||||||||||||
| 799 | |||||||||||||||||||||||||||||
| 800 | Find first object that match $callback | ||||||||||||||||||||||||||||
| 801 | |||||||||||||||||||||||||||||
| 802 | Returns: | ||||||||||||||||||||||||||||
| 803 | C<Model> object | ||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||
| 805 | =cut | ||||||||||||||||||||||||||||
| 806 | |||||||||||||||||||||||||||||
| 807 | 8 | ▶ | sub __first { | ||||||||||||||||||||||||||
|
Subroutine
__first: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 808 | 8 | my ($self, $callback) = @_; | |||||||||||||||||||||||||||
| 809 | |||||||||||||||||||||||||||||
| 810 | 8 | ▶ | return List::Util::first { $callback->($_) } @{ $self->objects }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 811 | } | ||||||||||||||||||||||||||||
| 812 | |||||||||||||||||||||||||||||
| 813 | =head2 __list | ||||||||||||||||||||||||||||
| 814 | |||||||||||||||||||||||||||||
| 815 | Return a scalar, or an array if the value is a collection | ||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||
| 817 | =cut | ||||||||||||||||||||||||||||
| 818 | |||||||||||||||||||||||||||||
| 819 | 27 | ▶ | sub __list { | ||||||||||||||||||||||||||
|
Subroutine
__list: called CC=3 SCAR=14.2
| |||||||||||||||||||||||||||||
| 820 | 27 | my ($self, $value) = @_; | |||||||||||||||||||||||||||
| 821 | |||||||||||||||||||||||||||||
| 822 | 27 | ▶ | if (blessed($value) && $value->isa('Mojo::Util::Collection')) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($value) and $value->isa("Mojo::Util::Collection")
MC/DC0%0/2
builtin::blessed($value) and $value->isa("Mojo::Util::Collection")
builtin::blessed($value)
$value->isa("Mojo::Util::Collection")
Truth table33%1/3
A and B
A= builtin::blessed($value)B= $value->isa("Mojo::Util::Collection")
| |||||||||||||||||||||||||||||
| 823 | 0 | return @{ $value->objects }; | |||||||||||||||||||||||||||
| 824 | } | ||||||||||||||||||||||||||||
| 825 | |||||||||||||||||||||||||||||
| 826 | 27 | return $value; | |||||||||||||||||||||||||||
| 827 | } | ||||||||||||||||||||||||||||
| 828 | |||||||||||||||||||||||||||||
| 829 | 32 | 1; | |||||||||||||||||||||||||||