| 1 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||||||||||||||||||||||||||||
| 2 | # contributor license agreements. See the NOTICE file distributed with | ||||||||||||||||||||||||||||
| 3 | # this work for additional information regarding copyright ownership. | ||||||||||||||||||||||||||||
| 4 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||||||||||||||||||||||||||||
| 5 | # (the "License"); you may not use this file except in compliance with | ||||||||||||||||||||||||||||
| 6 | # the License. You may obtain a copy of the License at | ||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Unless required by applicable law or agreed to in writing, software | ||||||||||||||||||||||||||||
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, | ||||||||||||||||||||||||||||
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||||||||||||||||||||||||||||
| 13 | # See the License for the specific language governing permissions and | ||||||||||||||||||||||||||||
| 14 | # limitations under the License. | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | package Usergrid::Collection; | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 10 | ▶ | use namespace::autoclean; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 10 | with ( | |||||||||||||||||||||||||||
| 22 | 'Usergrid::Request', | ||||||||||||||||||||||||||||
| 23 | ); | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 10 | my @stack; | |||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | =head1 NAME | ||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | Usergrid::Collection - a Usergrid collection | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | Encapsulates Usergrid collections and provides methods for iterating and paging | ||||||||||||||||||||||||||||
| 34 | through them. | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | =head1 ATTRIBUTES | ||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | =over 4 | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | =item object | ||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | A hash reference with the collection data | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | =item uri | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | The URI from which this collection was retrieved | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | =item auto_page | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | When set, the collection will automatically move to the next page when iterating | ||||||||||||||||||||||||||||
| 51 | through the collection | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | =back | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | =cut | ||||||||||||||||||||||||||||
| 56 | 10 | has 'object' => ( is => 'rw', required => 1 ); | |||||||||||||||||||||||||||
| 57 | 10 | has 'uri' => ( is => 'rw', required => 1 ); | |||||||||||||||||||||||||||
| 58 | 10 | has 'auto_page' => ( is => 'rw', isa => 'Bool' ); | |||||||||||||||||||||||||||
| 59 | 0 | ▶ | has 'iterator' => ( is => 'rw', isa => 'Int', default => sub { -1 } ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | =head1 METHODS | ||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | =over 4 | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | =item has_next_entity | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | Returns true if there's another entity available during iteration. | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | =cut | ||||||||||||||||||||||||||||
| 70 | 0 | ▶ | sub has_next_entity { | ||||||||||||||||||||||||||
|
Subroutine
has_next_entity: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 72 | 0 | my $next = $self->iterator + 1; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ( $self->auto_page && $next >= $self->count()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$self->auto_page and $next >= $self->count
MC/DC0%0/2
$self->auto_page and $next >= $self->count
$self->auto_page
$next >= $self->count
Truth table0%0/3
A and B
A= $self->auto_pageB= $next >= $self->count
| |||||||||||||||||||||||||||||
| 74 | 0 | return $self->get_next_page(); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | 0 | ▶ | return ($next >= 0 && $next < $self->count()); | ||||||||||||||||||||||||||
|
Condition0%0/3
$next >= 0 && $next < $self->count
MC/DC0%0/2
$next >= 0 && $next < $self->count
$next >= 0
$next < $self->count
Truth table0%0/3
A && B
A= $next >= 0B= $next < $self->count
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | =item get_next_entity | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | Returns the next available L<Usergrid::Entity>. If there's no entity available | ||||||||||||||||||||||||||||
| 82 | to return, it returns a FALSE. If the auto_page attribute it set, the next page | ||||||||||||||||||||||||||||
| 83 | is automatically fetched and the next entity is returned. | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | =cut | ||||||||||||||||||||||||||||
| 86 | 0 | ▶ | sub get_next_entity { | ||||||||||||||||||||||||||
|
Subroutine
get_next_entity: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 87 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 88 | 0 | ▶ | if ($self->has_next_entity()) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 89 | 0 | $self->iterator ($self->iterator + 1); | |||||||||||||||||||||||||||
| 90 | 0 | return Usergrid::Entity->new ( object => $self->object->{'entities'}[$self->iterator] ); | |||||||||||||||||||||||||||
| 91 | } | ||||||||||||||||||||||||||||
| 92 | 0 | return undef; | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | =item count | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | Returns the count of the items in the collection. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | =cut | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub count { | ||||||||||||||||||||||||||
|
Subroutine
count: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 102 | 0 | return scalar @{$self->object->{'entities'}}; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item reset_iterator | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Rewinds the iterator back to the beginning. | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =cut | ||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub reset_iterator { | ||||||||||||||||||||||||||
|
Subroutine
reset_iterator: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 111 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 112 | 0 | $self->iterator (-1); | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item get_first_entity | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | Returns the first entity in the collection. This is only applicable for the | ||||||||||||||||||||||||||||
| 118 | current page of the collection. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =cut | ||||||||||||||||||||||||||||
| 121 | 0 | ▶ | sub get_first_entity { | ||||||||||||||||||||||||||
|
Subroutine
get_first_entity: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 122 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 123 | return ($self->count() > 0) ? Usergrid::Entity->new ( | ||||||||||||||||||||||||||||
| 124 | 0 | ▶ | object => $self->object->{'entities'}[0] ) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =item get_last_entity | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | Returns the last entity in the collection. This is only applicable for the | ||||||||||||||||||||||||||||
| 130 | current page of the collection. | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | =cut | ||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub get_last_entity { | ||||||||||||||||||||||||||
|
Subroutine
get_last_entity: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 134 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 135 | return ($self->count() > 0) ? Usergrid::Entity->new ( | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | object => $self->object->{'entities'}[$self->count() - 1] ) : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | =item get_next_page | ||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | Fetches the next page in the collection. Returns false when there are no more reults. | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | =cut | ||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub get_next_page { | ||||||||||||||||||||||||||
|
Subroutine
get_next_page: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 146 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 0 | my $csr = $self->object->{'cursor'}; | |||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | my $object = $self->GET($self->uri . "&cursor=". $csr); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 0 | ▶ | if ($object->{'count'} > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 153 | 0 | ▶ | push @stack, "1" if (scalar @stack == 0); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 0 | push @stack, $csr; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | 0 | $self->object( $object ); | |||||||||||||||||||||||||||
| 157 | 0 | $self->reset_iterator(); | |||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | return $self; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 0 | 0; | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | =item get_prev_page | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | Fetches the previous page in the collection. Returns false when there are no more reults. | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub get_prev_page { | ||||||||||||||||||||||||||
|
Subroutine
get_prev_page: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 171 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 172 | 0 | my $object; | |||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (scalar @stack > 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 175 | 0 | my $csr = pop @stack; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 0 | ▶ | if ($csr eq "1") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 178 | 0 | $object = $self->GET($self->uri); | |||||||||||||||||||||||||||
| 179 | } else { | ||||||||||||||||||||||||||||
| 180 | 0 | $object = $self->GET($self->uri . "&cursor=" . $csr); | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | $self->object( $object ); | |||||||||||||||||||||||||||
| 184 | 0 | $self->reset_iterator(); | |||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 0 | return $self; | |||||||||||||||||||||||||||
| 187 | } | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | 0 | 0; | |||||||||||||||||||||||||||
| 190 | } | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | 10 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 10 | 1; | |||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | __END__ | ||||||||||||||||||||||||||||