| 1 | package Zing::Cursor; | ||
| 2 | |||
| 3 | use 5.014; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use registry 'Zing::Types'; | ||
| 9 | use routines; | ||
| 10 | |||
| 11 | use Data::Object::Class; | ||
| 12 | use Data::Object::ClassHas; | ||
| 13 | |||
| 14 | extends 'Zing::Entity'; | ||
| 15 | |||
| 16 | our $VERSION = '0.26'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has 'position' => ( | ||
| 21 | is => 'rw', | ||
| 22 | isa => 'Maybe[Str]', | ||
| 23 | opt => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | has 'lookup' => ( | ||
| 27 | is => 'ro', | ||
| 28 | isa => 'Lookup', | ||
| 29 | req => 1, | ||
| 30 | ); | ||
| 31 | |||
| 32 | # METHODS | ||
| 33 | |||
| 34 | method count() { | ||
| 35 | return int keys %{$self->lookup->state}; | ||
| 36 | } | ||
| 37 | |||
| 38 | method fetch(Int $size = 1) { | ||
| 39 | my $results = []; | ||
| 40 | |||
| 41 | for (1..$size) { | ||
| 42 | if (my $domain = $self->next) { | ||
| 43 | push @$results, $domain; | ||
| 44 | } | ||
| 45 | } | ||
| 46 | |||
| 47 | return $results; | ||
| 48 | } | ||
| 49 | |||
| 50 | method first() { | ||
| 51 | my $tail = $self->lookup->tail or return undef; | ||
| 52 | |||
| 53 | my $reference = $self->lookup->state->{$tail} or return undef; | ||
| 54 | |||
| 55 | return $self->app->domain(name => $reference->{name}); | ||
| 56 | |||
| 57 | } | ||
| 58 | |||
| 59 | method last() { | ||
| 60 | my $head = $self->lookup->head or return undef; | ||
| 61 | |||
| 62 | my $reference = $self->lookup->state->{$head} or return undef; | ||
| 63 | |||
| 64 | return $self->app->domain(name => $reference->{name}); | ||
| 65 | } | ||
| 66 | |||
| 67 | method next() { | ||
| 68 | my $position = $self->position; | ||
| 69 | |||
| 70 | if ($self->{prev_null} || (!$position && !$self->{initial})) { | ||
| 71 | if (!$position) { | ||
| 72 | $position = $self->lookup->tail; | ||
| 73 | } | ||
| 74 | |||
| 75 | if ($self->{initial}) { | ||
| 76 | $self->{initial} ||= $position; | ||
| 77 | } | ||
| 78 | |||
| 79 | delete $self->{prev_null}; | ||
| 80 | |||
| 81 | if (!$position) { | ||
| 82 | return undef; | ||
| 83 | } | ||
| 84 | |||
| 85 | my $current = $self->lookup->state->{$position} or return undef; | ||
| 86 | |||
| 87 | $self->position($position); | ||
| 88 | |||
| 89 | return $self->app->domain(name => $current->{name}); | ||
| 90 | } | ||
| 91 | |||
| 92 | if (!$position) { | ||
| 93 | return undef; | ||
| 94 | } | ||
| 95 | |||
| 96 | my $current = $self->lookup->state->{$position} or return undef; | ||
| 97 | |||
| 98 | if (!$current->{next}) { | ||
| 99 | $self->{next_null} = 1; | ||
| 100 | |||
| 101 | return undef; | ||
| 102 | } | ||
| 103 | |||
| 104 | $self->position($current->{next}); | ||
| 105 | |||
| 106 | my $endpoint = $self->lookup->state->{$current->{next}} or return undef; | ||
| 107 | |||
| 108 | return $self->app->domain(name => $endpoint->{name}); | ||
| 109 | } | ||
| 110 | |||
| 111 | method prev() { | ||
| 112 | my $position = $self->position; | ||
| 113 | |||
| 114 | if ($self->{next_null} || (!$position && !$self->{initial})) { | ||
| 115 | if (!$position) { | ||
| 116 | $position = $self->lookup->head; | ||
| 117 | } | ||
| 118 | |||
| 119 | if ($self->{initial}) { | ||
| 120 | $self->{initial} ||= $position; | ||
| 121 | } | ||
| 122 | |||
| 123 | delete $self->{next_null}; | ||
| 124 | |||
| 125 | if (!$position) { | ||
| 126 | return undef; | ||
| 127 | } | ||
| 128 | |||
| 129 | my $current = $self->lookup->state->{$position} or return undef; | ||
| 130 | |||
| 131 | $self->position($position); | ||
| 132 | |||
| 133 | return $self->app->domain(name => $current->{name}); | ||
| 134 | } | ||
| 135 | |||
| 136 | if (!$position) { | ||
| 137 | return undef; | ||
| 138 | } | ||
| 139 | |||
| 140 | my $current = $self->lookup->state->{$position} or return undef; | ||
| 141 | |||
| 142 | if (!$current->{prev}) { | ||
| 143 | $self->{prev_null} = 1; | ||
| 144 | |||
| 145 | return undef; | ||
| 146 | } | ||
| 147 | |||
| 148 | $self->position($current->{prev}); | ||
| 149 | |||
| 150 | my $endpoint = $self->lookup->state->{$current->{prev}} or return undef; | ||
| 151 | |||
| 152 | return $self->app->domain(name => $endpoint->{name}); | ||
| 153 | } | ||
| 154 | |||
| 155 | method reset() { | ||
| 156 | $self->position($self->{initial}) if !$self->{initial}; | ||
| 157 | |||
| 158 | return $self; | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | |||
| 163 | =encoding utf8 | ||
| 164 | |||
| 165 | =head1 NAME | ||
| 166 | |||
| 167 | Zing::Cursor - Lookup Table Traversal | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | =head1 ABSTRACT | ||
| 172 | |||
| 173 | Lookup Table Traversal Construct | ||
| 174 | |||
| 175 | =cut | ||
| 176 | |||
| 177 | =head1 SYNOPSIS | ||
| 178 | |||
| 179 | use Zing::Lookup; | ||
| 180 | use Zing::Cursor; | ||
| 181 | |||
| 182 | my $lookup = Zing::Lookup->new(name => 'users'); | ||
| 183 | |||
| 184 | $lookup->set('user-12345')->set(username => 'u12345'); | ||
| 185 | $lookup->set('user-12346')->set(username => 'u12346'); | ||
| 186 | $lookup->set('user-12347')->set(username => 'u12347'); | ||
| 187 | |||
| 188 | my $cursor = Zing::Cursor->new(lookup => $lookup); | ||
| 189 | |||
| 190 | # $cursor->count; | ||
| 191 | |||
| 192 | =cut | ||
| 193 | |||
| 194 | =head1 DESCRIPTION | ||
| 195 | |||
| 196 | This package provides a cursor for traversing L<Zing::Lookup> indices and | ||
| 197 | supports forward and backwards traversal as well as token-based pagination. | ||
| 198 | |||
| 199 | =cut | ||
| 200 | |||
| 201 | =head1 LIBRARIES | ||
| 202 | |||
| 203 | This package uses type constraints from: | ||
| 204 | |||
| 205 | L<Zing::Types> | ||
| 206 | |||
| 207 | =cut | ||
| 208 | |||
| 209 | =head1 ATTRIBUTES | ||
| 210 | |||
| 211 | This package has the following attributes: | ||
| 212 | |||
| 213 | =cut | ||
| 214 | |||
| 215 | =head2 lookup | ||
| 216 | |||
| 217 | lookup(Lookup) | ||
| 218 | |||
| 219 | This attribute is read-only, accepts C<(Lookup)> values, and is required. | ||
| 220 | |||
| 221 | =cut | ||
| 222 | |||
| 223 | =head2 position | ||
| 224 | |||
| 225 | position(Maybe[Str]) | ||
| 226 | |||
| 227 | This attribute is read-write, accepts C<(Maybe[Str])> values, and is optional. | ||
| 228 | |||
| 229 | =cut | ||
| 230 | |||
| 231 | =head1 METHODS | ||
| 232 | |||
| 233 | This package implements the following methods: | ||
| 234 | |||
| 235 | =cut | ||
| 236 | |||
| 237 | =head2 count | ||
| 238 | |||
| 239 | count() : Int | ||
| 240 | |||
| 241 | The count method returns the number of L<Zing::Domain> objects in the lookup | ||
| 242 | table. | ||
| 243 | |||
| 244 | =over 4 | ||
| 245 | |||
| 246 | =item count example #1 | ||
| 247 | |||
| 248 | # given: synopsis | ||
| 249 | |||
| 250 | $cursor->count; | ||
| 251 | |||
| 252 | =back | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | =head2 fetch | ||
| 257 | |||
| 258 | fetch(Int $size = 1) : ArrayRef[Domain] | ||
| 259 | |||
| 260 | The fetch method returns the next C<n> L<Zing::Domain> objects from the lookup | ||
| 261 | table. | ||
| 262 | |||
| 263 | =over 4 | ||
| 264 | |||
| 265 | =item fetch example #1 | ||
| 266 | |||
| 267 | # given: synopsis | ||
| 268 | |||
| 269 | $cursor->fetch; | ||
| 270 | |||
| 271 | =back | ||
| 272 | |||
| 273 | =over 4 | ||
| 274 | |||
| 275 | =item fetch example #2 | ||
| 276 | |||
| 277 | # given: synopsis | ||
| 278 | |||
| 279 | $cursor->fetch(5); | ||
| 280 | |||
| 281 | =back | ||
| 282 | |||
| 283 | =cut | ||
| 284 | |||
| 285 | =head2 first | ||
| 286 | |||
| 287 | first() : Maybe[Domain] | ||
| 288 | |||
| 289 | The first method returns the first L<Zing::Domain> object created in the lookup | ||
| 290 | table. | ||
| 291 | |||
| 292 | =over 4 | ||
| 293 | |||
| 294 | =item first example #1 | ||
| 295 | |||
| 296 | # given: synopsis | ||
| 297 | |||
| 298 | $cursor->first; | ||
| 299 | |||
| 300 | =back | ||
| 301 | |||
| 302 | =cut | ||
| 303 | |||
| 304 | =head2 last | ||
| 305 | |||
| 306 | last() : Maybe[Domain] | ||
| 307 | |||
| 308 | The last method returns the last L<Zing::Domain> object created in the lookup | ||
| 309 | table. | ||
| 310 | |||
| 311 | =over 4 | ||
| 312 | |||
| 313 | =item last example #1 | ||
| 314 | |||
| 315 | # given: synopsis | ||
| 316 | |||
| 317 | $cursor->last; | ||
| 318 | |||
| 319 | =back | ||
| 320 | |||
| 321 | =cut | ||
| 322 | |||
| 323 | =head2 next | ||
| 324 | |||
| 325 | next() : Maybe[Domain] | ||
| 326 | |||
| 327 | The next method returns the next (after the current position) L<Zing::Domain> | ||
| 328 | object in the lookup table. | ||
| 329 | |||
| 330 | =over 4 | ||
| 331 | |||
| 332 | =item next example #1 | ||
| 333 | |||
| 334 | # given: synopsis | ||
| 335 | |||
| 336 | $cursor->next; | ||
| 337 | |||
| 338 | =back | ||
| 339 | |||
| 340 | =over 4 | ||
| 341 | |||
| 342 | =item next example #2 | ||
| 343 | |||
| 344 | # given: synopsis | ||
| 345 | |||
| 346 | $cursor->next; | ||
| 347 | $cursor->next; | ||
| 348 | |||
| 349 | =back | ||
| 350 | |||
| 351 | =cut | ||
| 352 | |||
| 353 | =head2 prev | ||
| 354 | |||
| 355 | prev() : Maybe[Domain] | ||
| 356 | |||
| 357 | The prev method returns the prev (before the current position) L<Zing::Domain> | ||
| 358 | object in the lookup table. | ||
| 359 | |||
| 360 | =over 4 | ||
| 361 | |||
| 362 | =item prev example #1 | ||
| 363 | |||
| 364 | # given: synopsis | ||
| 365 | |||
| 366 | $cursor->prev; | ||
| 367 | |||
| 368 | =back | ||
| 369 | |||
| 370 | =over 4 | ||
| 371 | |||
| 372 | =item prev example #2 | ||
| 373 | |||
| 374 | # given: synopsis | ||
| 375 | |||
| 376 | $cursor->prev; | ||
| 377 | $cursor->prev; | ||
| 378 | |||
| 379 | =back | ||
| 380 | |||
| 381 | =cut | ||
| 382 | |||
| 383 | =head2 reset | ||
| 384 | |||
| 385 | reset() : Cursor | ||
| 386 | |||
| 387 | The reset method returns the cursor to its starting position (defined at | ||
| 388 | construction). | ||
| 389 | |||
| 390 | =over 4 | ||
| 391 | |||
| 392 | =item reset example #1 | ||
| 393 | |||
| 394 | # given: synopsis | ||
| 395 | |||
| 396 | $cursor->prev; | ||
| 397 | $cursor->next; | ||
| 398 | $cursor->next; | ||
| 399 | |||
| 400 | $cursor->reset; | ||
| 401 | |||
| 402 | =back | ||
| 403 | |||
| 404 | =cut | ||
| 405 | |||
| 406 | =head1 AUTHOR | ||
| 407 | |||
| 408 | Al Newkirk, C<awncorp@cpan.org> | ||
| 409 | |||
| 410 | =head1 LICENSE | ||
| 411 | |||
| 412 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 413 | |||
| 414 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 415 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 416 | file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>. | ||
| 417 | |||
| 418 | =head1 PROJECT | ||
| 419 | |||
| 420 | L<Wiki|https://github.com/iamalnewkirk/zing/wiki> | ||
| 421 | |||
| 422 | L<Project|https://github.com/iamalnewkirk/zing> | ||
| 423 | |||
| 424 | L<Initiatives|https://github.com/iamalnewkirk/zing/projects> | ||
| 425 | |||
| 426 | L<Milestones|https://github.com/iamalnewkirk/zing/milestones> | ||
| 427 | |||
| 428 | L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md> | ||
| 429 | |||
| 430 | L<Issues|https://github.com/iamalnewkirk/zing/issues> | ||
| 431 | |||
| 432 | =cut |