Zing/Table.pm untested

filter: Statementstmt 0.0% 0 / 88 Branchbran 0.0% 0 / 26 Conditioncond 0.0% 0 / 2 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 7 Podpod n/a totaltotal 0.0% 0 / 123 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing::Table;
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::Channel';
15  
16 our $VERSION = '0.26'; # VERSION
17  
18 # ATTRIBUTES
19  
20 has 'type' => (
21 is => 'rw',
22 isa => 'TableType',
23 def => 'domain',
24 );
25  
26 has 'position' => (
27 is => 'rw',
28 isa => 'Maybe[Int]',
29 def => undef,
30 );
31  
32 # METHODS
33  
34 method count() {
35 return int $self->size;
36 }
37  
38 method drop() {
39 return $self->store->drop($self->term);
40 }
41  
42 method fetch(Int $size = 1) {
43 my $results = [];
44  
45 for (1..$size) {
46 if (my $domain = $self->next) {
47 push @$results, $domain;
48 }
49 }
50  
51 return $results;
52 }
53  
54 method first() {
55 return $self->head;
56 }
57  
58 method get(Str $key) {
59 my $type = $self->type;
60 return $self->app->$type(name => $key);
61 }
62  
63 method head() {
64 my $position = 0;
65  
66 if (my $data = $self->store->slot($self->term, $position)) {
67 return $self->app->term($data->{term})->object($self->env);
68 }
69 else {
70 return undef;
71 }
72 }
73  
74 method index(Int $position) {
75 if (my $data = $self->store->slot($self->term, $position)) {
76 return $self->app->term($data->{term})->object($self->env);
77 }
78 else {
79 return undef;
80 }
81 }
82  
83 method last() {
84 return $self->tail;
85 }
86  
87 method next() {
88 my $position = $self->position;
89  
90 if (!defined $position) {
91 $position = 0;
92 }
93 else {
94 $position++;
95 }
96 if (my $data = $self->store->slot($self->term, $position)) {
97 $self->position($position);
98 return $self->app->term($data->{term})->object($self->env);
99 }
100 else {
101 $self->position($position) if $position == $self->size;
102 return undef;
103 }
104 }
105  
106 method prev() {
107 my $position = $self->position;
108  
109 if (!defined $position) {
110 return undef;
111 }
112 elsif ($position == 0) {
113 $self->position(undef);
114 return undef;
115 }
116 else {
117 $position--;
118 }
119 if (my $data = $self->store->slot($self->term, $position)) {
120 $self->position($position);
121 return $self->app->term($data->{term})->object($self->env);
122 }
123 else {
124 return undef;
125 }
126 }
127  
128 around recv() {
129 if (my $data = $self->$orig) {
130 return $self->app->term($data->{term})->object($self->env);
131 }
132 else {
133 return undef;
134 }
135 }
136  
137 method renew() {
138 return $self->reset if (($self->{position} || 0) + 1) > $self->size;
139 return 0;
140 }
141  
142 method reset() {
143 return !($self->{position} = undef);
144 }
145  
146 method set(Str $key) {
147 my $type = $self->type;
148 my $repo = $self->app->$type(name => $key);
149 $self->send({term => $repo->term});
150 return $repo;
151 }
152  
153 method tail() {
154 my $size = $self->size;
155 my $position = $size ? ($size - 1) : 0;
156  
157 if (my $data = $self->store->slot($self->term, $position)) {
158 return $self->app->term($data->{term})->object($self->env);
159 }
160 else {
161 return undef;
162 }
163 }
164  
165 method term() {
166 return $self->app->term($self)->table;
167 }
168  
169 1;
170  
171 =encoding utf8
172  
173 =head1 NAME
174  
175 Zing::Table - Entity Lookup Table
176  
177 =cut
178  
179 =head1 ABSTRACT
180  
181 Entity Lookup Table Construct
182  
183 =cut
184  
185 =head1 SYNOPSIS
186  
187 use Zing::Table;
188  
189 my $table = Zing::Table->new(name => 'users');
190  
191 # my $domain = $table->set('unique-id');
192  
193 =cut
194  
195 =head1 DESCRIPTION
196  
197 This package provides an index and lookup-table for L<Zing::Repo> derived data
198 structures which provides the ability to create a collection of repo objects.
199  
200 =cut
201  
202 =head1 INHERITS
203  
204 This package inherits behaviors from:
205  
206 L<Zing::Channel>
207  
208 =cut
209  
210 =head1 LIBRARIES
211  
212 This package uses type constraints from:
213  
214 L<Zing::Types>
215  
216 =cut
217  
218 =head1 ATTRIBUTES
219  
220 This package has the following attributes:
221  
222 =cut
223  
224 =head2 position
225  
226 position(Maybe[Int])
227  
228 This attribute is read-write, accepts C<(Maybe[Int])> values, and is optional.
229  
230 =cut
231  
232 =head2 type
233  
234 type(TableType)
235  
236 This attribute is read-only, accepts C<(TableType)> values, and is optional.
237  
238 =cut
239  
240 =head1 METHODS
241  
242 This package implements the following methods:
243  
244 =cut
245  
246 =head2 count
247  
248 count() : Int
249  
250 The count method returns the number of L<Zing::Repo> objects in the table.
251  
252 =over 4
253  
254 =item count example #1
255  
256 # given: synopsis
257  
258 $table->count;
259  
260 =back
261  
262 =over 4
263  
264 =item count example #2
265  
266 # given: synopsis
267  
268 $table->set('user-12345');
269  
270 $table->count;
271  
272 =back
273  
274 =cut
275  
276 =head2 drop
277  
278 drop() : Int
279  
280 The drop method returns truthy if the table has been destroyed. This operation
281 does not cascade.
282  
283 =over 4
284  
285 =item drop example #1
286  
287 # given: synopsis
288  
289 $table->drop;
290  
291 =back
292  
293 =cut
294  
295 =head2 fetch
296  
297 fetch(Int $size = 1) : ArrayRef[Repo]
298  
299 The fetch method returns the next C<n> L<Zing::Repo> objects from the table.
300  
301 =over 4
302  
303 =item fetch example #1
304  
305 # given: synopsis
306  
307 $table->fetch;
308  
309 =back
310  
311 =over 4
312  
313 =item fetch example #2
314  
315 # given: synopsis
316  
317 $table->set('user-12345');
318 $table->set('user-12346');
319 $table->set('user-12347');
320  
321 $table->fetch(5);
322  
323 =back
324  
325 =cut
326  
327 =head2 first
328  
329 first() : Maybe[Repo]
330  
331 The first method returns the first L<Zing::Repo> object created in the table.
332  
333 =over 4
334  
335 =item first example #1
336  
337 # given: synopsis
338  
339 $table->first;
340  
341 =back
342  
343 =cut
344  
345 =head2 get
346  
347 get(Str $key) : Maybe[Repo]
348  
349 The get method returns the L<Zing::Repo> associated with a specific key.
350  
351 =over 4
352  
353 =item get example #1
354  
355 # given: synopsis
356  
357 $table->get('user-12345');
358  
359 =back
360  
361 =cut
362  
363 =head2 head
364  
365 head() : Maybe[Repo]
366  
367 The head method returns the first L<Zing::Repo> object created in the table.
368  
369 =over 4
370  
371 =item head example #1
372  
373 # given: synopsis
374  
375 $table->head;
376  
377 =back
378  
379 =cut
380  
381 =head2 index
382  
383 index(Int $position) : Maybe[Repo]
384  
385 The index method returns the L<Zing::Repo> object at the position (index) specified.
386  
387 =over 4
388  
389 =item index example #1
390  
391 # given: synopsis
392  
393 $table->index(0);
394  
395 =back
396  
397 =cut
398  
399 =head2 last
400  
401 last() : Maybe[Repo]
402  
403 The last method returns the last L<Zing::Repo> object created in the table.
404  
405 =over 4
406  
407 =item last example #1
408  
409 # given: synopsis
410  
411 $table->last;
412  
413 =back
414  
415 =cut
416  
417 =head2 next
418  
419 next() : Maybe[Repo]
420  
421 The next method returns the next L<Zing::Repo> object created in the table.
422  
423 =over 4
424  
425 =item next example #1
426  
427 # given: synopsis
428  
429 $table->next;
430  
431 =back
432  
433 =over 4
434  
435 =item next example #2
436  
437 # given: synopsis
438  
439 $table->position(undef);
440  
441 $table->prev;
442 $table->prev;
443 $table->next;
444  
445 =back
446  
447 =over 4
448  
449 =item next example #3
450  
451 # given: synopsis
452  
453 $table->position($table->size);
454  
455 $table->prev;
456 $table->next;
457 $table->prev;
458  
459 =back
460  
461 =cut
462  
463 =head2 prev
464  
465 prev() : Maybe[Repo]
466  
467 The prev method returns the previous L<Zing::Repo> object created in the table.
468  
469 =over 4
470  
471 =item prev example #1
472  
473 # given: synopsis
474  
475 $table->prev;
476  
477 =back
478  
479 =over 4
480  
481 =item prev example #2
482  
483 # given: synopsis
484  
485 $table->next;
486 $table->next;
487 $table->prev;
488  
489 =back
490  
491 =over 4
492  
493 =item prev example #3
494  
495 # given: synopsis
496  
497 $table->position($table->size);
498  
499 $table->next;
500 $table->next;
501 $table->prev;
502  
503 =back
504  
505 =over 4
506  
507 =item prev example #4
508  
509 # given: synopsis
510  
511 $table->position(undef);
512  
513 $table->next;
514 $table->prev;
515 $table->next;
516  
517 =back
518  
519 =cut
520  
521 =head2 renew
522  
523 renew() : Int
524  
525 The renew method returns truthy if it resets the internal cursor, otherwise falsy.
526  
527 =over 4
528  
529 =item renew example #1
530  
531 # given: synopsis
532  
533 $table->renew;
534  
535 =back
536  
537 =cut
538  
539 =head2 reset
540  
541 reset() : Int
542  
543 The reset method always reset the internal cursor and return truthy.
544  
545 =over 4
546  
547 =item reset example #1
548  
549 # given: synopsis
550  
551 $table->reset;
552  
553 =back
554  
555 =cut
556  
557 =head2 set
558  
559 set(Str $key) : Repo
560  
561 The set method creates a L<Zing::Repo> association with a specific key in the
562 table. The key should be unique. Adding the same key will result in duplicate
563 entries.
564  
565 =over 4
566  
567 =item set example #1
568  
569 # given: synopsis
570  
571 $table->set('user-12345');
572  
573 =back
574  
575 =cut
576  
577 =head2 tail
578  
579 tail() : Maybe[Repo]
580  
581 The tail method returns the last L<Zing::Repo> object created in the table.
582  
583 =over 4
584  
585 =item tail example #1
586  
587 # given: synopsis
588  
589 $table->tail;
590  
591 =back
592  
593 =cut
594  
595 =head2 term
596  
597 term() : Str
598  
599 The term method returns the name of the table.
600  
601 =over 4
602  
603 =item term example #1
604  
605 # given: synopsis
606  
607 $table->term;
608  
609 =back
610  
611 =cut
612  
613 =head1 AUTHOR
614  
615 Al Newkirk, C<awncorp@cpan.org>
616  
617 =head1 LICENSE
618  
619 Copyright (C) 2011-2019, Al Newkirk, et al.
620  
621 This is free software; you can redistribute it and/or modify it under the terms
622 of the The Apache License, Version 2.0, as elucidated in the L<"license
623 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
624  
625 =head1 PROJECT
626  
627 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
628  
629 L<Project|https://github.com/iamalnewkirk/zing>
630  
631 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
632  
633 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
634  
635 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
636  
637 L<Issues|https://github.com/iamalnewkirk/zing/issues>
638  
639 =cut