Zing/Lookup.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 12 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 130 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Zing::Lookup;
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::Domain';
15  
16 our $VERSION = '0.26'; # VERSION
17  
18 # BUILDERS
19  
20 fun BUILD($self) {
21 my $savepoint = $self->savepoint;
22  
23 if ($savepoint->test) {
24 $self->{state} = $savepoint->snapshot;
25 $self->{position} = $savepoint->position;
26 $self->{metadata} = $savepoint->metadata;
27 }
28  
29 return $self->apply;
30 }
31  
32 # METHODS
33  
34 method cursor() {
35 return $self->app->cursor(lookup => $self);
36 }
37  
38 method decr(Any @args) {
39 $self->throw(error_not_supported($self, 'decr'));
40 }
41  
42 around del($key) {
43 my $name = $self->hash($key);
44 my $item = $self->state->{$name};
45 return $self if !$item;
46 my $next = $item->{next};
47 my $prev = $item->{prev};
48 if ($next && $prev) {
49 $self->change('set', $prev, {%{$self->state->{$prev}}, next => $next});
50 $self->change('set', $next, {%{$self->state->{$next}}, prev => $prev});
51 }
52 elsif ($next && !$prev) {
53 $self->metadata->{tail} = $next;
54 $self->change('set', $next, {%{$self->state->{$next}}, prev => undef});
55 }
56 elsif (!$next && $prev) {
57 $self->metadata->{head} = $prev;
58 $self->change('set', $prev, {%{$self->state->{$prev}}, next => undef});
59 }
60 $self->$orig($name);
61 $self->app->domain(name => $item->{name})->drop;
62 return $self;
63 }
64  
65 around drop() {
66 if (my $savepoint = $self->savepoint) {
67 $savepoint->drop if $savepoint->test;
68 }
69 return $self->$orig;
70 }
71  
72 method get(Str $key) {
73 my $data = $self->apply->state->{$self->hash($key)};
74 return undef if !$data;
75 return $self->app->domain(name => $data->{name});
76 }
77  
78 method head() {
79 return $self->metadata->{head};
80 }
81  
82 method incr(Any @args) {
83 $self->throw(error_not_supported($self, 'incr'));
84 }
85  
86 method hash(Str $key) {
87 require Digest::SHA; Digest::SHA::sha1_hex($key);
88 }
89  
90 method pop(Any @args) {
91 $self->throw(error_not_supported($self, 'pop'));
92 }
93  
94 method push(Any @args) {
95 $self->throw(error_not_supported($self, 'push'));
96 }
97  
98 method restore(HashRef $data) {
99 return $self->{state} = {};
100 }
101  
102 method set(Str $key) {
103 my $hash = $self->hash($key);
104 my $name = $key;
105 my $domain = $self->app->domain(name => $name);
106 my $prev = $self->apply->head;
107 if ($prev && $self->state->{$prev}) {
108 $self->change('set', $prev, {%{$self->state->{$prev}}, next => $hash});
109 }
110 $self->metadata->{head} = $hash;
111 $self->metadata->{tail} = $hash if !$self->metadata->{tail};
112 $self->change('set', $hash, {name => $name, next => undef, prev => $prev});
113 return $domain;
114 }
115  
116 method shift(Any @args) {
117 $self->throw(error_not_supported($self, 'shift'));
118 }
119  
120 method savepoint() {
121 return $self->app->savepoint(lookup => $self);
122 }
123  
124 method snapshot() {
125 return {};
126 }
127  
128 method tail() {
129 return $self->metadata->{tail};
130 }
131  
132 method term() {
133 return $self->app->term($self)->lookup;
134 }
135  
136 method unshift(Any @args) {
137 $self->throw(error_not_supported($self, 'unshift'));
138 }
139  
140 # ERRORS
141  
142 fun error_not_supported(Object $object, Str $method) {
143 code => 'error_not_implemented',
144 message => "@{[ref($object)]} method \"$method\" not supported",
145 }
146  
147 1;
148  
149 =encoding utf8
150  
151 =head1 NAME
152  
153 Zing::Lookup - Domain Lookup Table
154  
155 =cut
156  
157 =head1 ABSTRACT
158  
159 Domain Lookup Table Construct
160  
161 =cut
162  
163 =head1 SYNOPSIS
164  
165 use Zing::Lookup;
166  
167 my $lookup = Zing::Lookup->new(name => 'users');
168  
169 # my $domain = $lookup->set('unique-id');
170  
171 =cut
172  
173 =head1 DESCRIPTION
174  
175 This package provides an index and lookup-table for L<Zing::Domain> data
176 structures which provides the ability to create a collection of domains with
177 full history of state changes.
178  
179 =cut
180  
181 =head1 INHERITS
182  
183 This package inherits behaviors from:
184  
185 L<Zing::Domain>
186  
187 =cut
188  
189 =head1 LIBRARIES
190  
191 This package uses type constraints from:
192  
193 L<Zing::Types>
194  
195 =cut
196  
197 =head1 METHODS
198  
199 This package implements the following methods:
200  
201 =cut
202  
203 =head2 cursor
204  
205 cursor() : Cursor
206  
207 The cursor method returns a L<Zing::Cursor> object which provides the ability
208 to page-through and traverse the lookup dataset forwards and backwards.
209  
210 =over 4
211  
212 =item cursor example #1
213  
214 # given: synopsis
215  
216 $lookup->cursor;
217  
218 =back
219  
220 =cut
221  
222 =head2 del
223  
224 del(Str $key) : Lookup
225  
226 The del method deletes the L<Zing::Domain> associated with a specific key.
227  
228 =over 4
229  
230 =item del example #1
231  
232 # given: synopsis
233  
234 $lookup->del('user-12345');
235  
236 =back
237  
238 =over 4
239  
240 =item del example #2
241  
242 # given: synopsis
243  
244 $lookup->set('user-12345', 'me@example.com');
245  
246 $lookup->del('user-12345');
247  
248 =back
249  
250 =cut
251  
252 =head2 drop
253  
254 drop() : Int
255  
256 The drop method returns truthy if the lookup has been destroyed. This operation
257 does not cascade.
258  
259 =over 4
260  
261 =item drop example #1
262  
263 # given: synopsis
264  
265 $lookup->set('user-12345', 'me@example.com');
266  
267 $lookup->drop;
268  
269 =back
270  
271 =over 4
272  
273 =item drop example #2
274  
275 # given: synopsis
276  
277 $lookup->set('user-12345', 'me@example.com');
278  
279 $lookup->savepoint->send;
280  
281 $lookup->drop;
282  
283 =back
284  
285 =cut
286  
287 =head2 get
288  
289 get(Str $key) : Maybe[Domain]
290  
291 The get method return the L<Zing::Domain> associated with a specific key.
292  
293 =over 4
294  
295 =item get example #1
296  
297 # given: synopsis
298  
299 $lookup->get('user-12345');
300  
301 =back
302  
303 =over 4
304  
305 =item get example #2
306  
307 # given: synopsis
308  
309 $lookup->set('user-12345')->set(email => 'me@example.com');
310  
311 $lookup->get('user-12345');
312  
313 =back
314  
315 =cut
316  
317 =head2 savepoint
318  
319 savepoint() : Savepoint
320  
321 The savepoint method returns a L<Zing::Savepoint> object which provides the
322 ability to save and restore large indices (lookup states). If a lookup has an
323 associated savepoint it will be loaded automatically on object construction.
324  
325 =over 4
326  
327 =item savepoint example #1
328  
329 # given: synopsis
330  
331 $lookup->savepoint;
332  
333 =back
334  
335 =cut
336  
337 =head2 set
338  
339 set(Str $key) : Domain
340  
341 The set method creates a L<Zing::Domain> association with a specific key in the
342 lookup. The key must be unique or will overwrite any existing data.
343  
344 =over 4
345  
346 =item set example #1
347  
348 # given: synopsis
349  
350 $lookup->del('user-12345');
351  
352 $lookup->set('user-12345');
353  
354 =back
355  
356 =cut
357  
358 =head1 AUTHOR
359  
360 Al Newkirk, C<awncorp@cpan.org>
361  
362 =head1 LICENSE
363  
364 Copyright (C) 2011-2019, Al Newkirk, et al.
365  
366 This is free software; you can redistribute it and/or modify it under the terms
367 of the The Apache License, Version 2.0, as elucidated in the L<"license
368 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
369  
370 =head1 PROJECT
371  
372 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
373  
374 L<Project|https://github.com/iamalnewkirk/zing>
375  
376 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
377  
378 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
379  
380 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
381  
382 L<Issues|https://github.com/iamalnewkirk/zing/issues>
383  
384 =cut