Nano/Nodes.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran n/a Conditioncond 0.0% 0 / 4 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 80 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Nano::Nodes;
2  
3 use 5.014;
4  
5 use strict;
6 use warnings;
7  
8 use registry 'Nano::Types';
9 use routines;
10  
11 use Data::Object::Class;
12 use Data::Object::ClassHas;
13  
14 extends 'Nano::Node';
15  
16 our $VERSION = '0.06'; # VERSION
17  
18 # ATTRIBUTES
19  
20 has orders => (
21 is => 'ro',
22 isa => 'ArrayRef[CodeRef]',
23 new => 1,
24 );
25  
26 fun new_orders($self) {
27 []
28 }
29  
30 has scopes => (
31 is => 'ro',
32 isa => 'ArrayRef[CodeRef]',
33 new => 1,
34 );
35  
36 fun new_scopes($self) {
37 []
38 }
39  
40 has type => (
41 is => 'ro',
42 isa => 'Str',
43 new => 1,
44 req => 1,
45 );
46  
47 fun new_type($self) {
48 undef
49 }
50  
51 # METHODS
52  
53 method add(HashRef $data) {
54 my $object = $self->nano->reify($self->type, $data);
55 my $table = $self->nano->table($self->id);
56 my $keyval = $table->set($object->id);
57 $keyval->send($object->serialize);
58 return $object;
59 }
60  
61 method all() {
62 return $self->search->all;
63 }
64  
65 method count() {
66 return $self->search->count;
67 }
68  
69 method drop() {
70 my $table = $self->nano->table($self->id);
71 $table->drop;
72 $self->next::method;
73 return $self;
74 }
75  
76 method first() {
77 return $self->search->first;
78 }
79  
80 method get(Str $name) {
81 my $table = $self->nano->table($self->id);
82 my $keyval = $table->get($name) or return undef;
83 my $object = $keyval->recv or return undef;
84 return $self->nano->object($object);
85 }
86  
87 method last() {
88 return $self->search->last;
89 }
90  
91 method order(CodeRef $callback) {
92 my $instance = ref($self)->new(
93 %{$self},
94 orders => [@{$self->orders}, $callback],
95 );
96 return $instance;
97 }
98  
99 method scope(CodeRef $callback) {
100 my $instance = ref($self)->new(
101 %{$self},
102 scopes => [@{$self->scopes}, $callback],
103 );
104 return $instance;
105 }
106  
107 method search() {
108 require Nano::Search; Nano::Search->new(
109 orders => $self->orders,
110 scopes => $self->scopes,
111 nodes => $self,
112 );
113 }
114  
115 method serialize() {
116 $self->id;
117 {
118 '$name' => $self->nano->name($self),
119 '$data' => $self->nano->dump($self),
120 '$type' => 'nodes',
121 }
122 }
123  
124 method set(Node $object) {
125 my $table = $self->nano->table($self->id);
126 my $keyval = $table->set($object->id);
127 $keyval->send($object->serialize);
128 return $object;
129 }
130  
131 1;
132  
133 =encoding utf8
134  
135 =head1 NAME
136  
137 Nano::Nodes - Persistable Index
138  
139 =cut
140  
141 =head1 ABSTRACT
142  
143 Persistable Index Super Class
144  
145 =cut
146  
147 =head1 SYNOPSIS
148  
149 use Nano::Nodes;
150  
151 my $nodes = Nano::Nodes->new(
152 type => 'Nano::Node',
153 );
154  
155 # $nodes->save;
156  
157 =cut
158  
159 =head1 DESCRIPTION
160  
161 This package provides a persistable index super class. It is meant to be
162 subclassed but can be used directly as well.
163  
164 =cut
165  
166 =head1 INHERITS
167  
168 This package inherits behaviors from:
169  
170 L<Nano::Node>
171  
172 =cut
173  
174 =head1 LIBRARIES
175  
176 This package uses type constraints from:
177  
178 L<Nano::Types>
179  
180 =cut
181  
182 =head1 ATTRIBUTES
183  
184 This package has the following attributes:
185  
186 =cut
187  
188 =head2 orders
189  
190 orders(ArrayRef[CodeRef])
191  
192 This attribute is read-only, accepts C<(ArrayRef[CodeRef])> values, and is optional.
193  
194 =cut
195  
196 =head2 scopes
197  
198 scopes(ArrayRef[CodeRef])
199  
200 This attribute is read-only, accepts C<(ArrayRef[CodeRef])> values, and is optional.
201  
202 =cut
203  
204 =head2 type
205  
206 type(Str)
207  
208 This attribute is read-only, accepts C<(Str)> values, and is required.
209  
210 =cut
211  
212 =head1 METHODS
213  
214 This package implements the following methods:
215  
216 =cut
217  
218 =head2 add
219  
220 add(HashRef $data) : Object
221  
222 The add method creates a new object, adds it to the index, and returns the
223 created object.
224  
225 =over 4
226  
227 =item add example #1
228  
229 # given: synopsis
230  
231 my $node = $nodes->add({ rand => time });
232  
233 =back
234  
235 =cut
236  
237 =head2 all
238  
239 all() : ArrayRef[Object]
240  
241 The all method proxies to the attached L<Nano::Search> instance and returns the
242 results.
243  
244 =over 4
245  
246 =item all example #1
247  
248 # given: synopsis
249  
250 my $all = $nodes->all;
251  
252 =back
253  
254 =cut
255  
256 =head2 count
257  
258 count() : Int
259  
260 The count method proxies to the attached L<Nano::Search> instance and returns
261 the results.
262  
263 =over 4
264  
265 =item count example #1
266  
267 # given: synopsis
268  
269 my $count = $nodes->count;
270  
271 =back
272  
273 =cut
274  
275 =head2 drop
276  
277 drop() : Object
278  
279 The drop method deletes the entire index and all of its indices.
280  
281 =over 4
282  
283 =item drop example #1
284  
285 # given: synopsis
286  
287 $nodes = $nodes->drop;
288  
289 =back
290  
291 =cut
292  
293 =head2 first
294  
295 first() : Maybe[Object]
296  
297 The first method proxies to the attached L<Nano::Search> instance and returns
298 the result.
299  
300 =over 4
301  
302 =item first example #1
303  
304 # given: synopsis
305  
306 my $first = $nodes->first;
307  
308 =back
309  
310 =cut
311  
312 =head2 get
313  
314 get(Str $name) : Maybe[Object]
315  
316 The get method returns the object (based on ID) from the index (if found).
317  
318 =over 4
319  
320 =item get example #1
321  
322 # given: synopsis
323  
324 my $result = $nodes->get('0000001');
325  
326 =back
327  
328 =cut
329  
330 =head2 last
331  
332 last() : Maybe[Object]
333  
334 The last method proxies to the attached L<Nano::Search> instance and returns
335 the result.
336  
337 =over 4
338  
339 =item last example #1
340  
341 # given: synopsis
342  
343 my $last = $nodes->last;
344  
345 =back
346  
347 =cut
348  
349 =head2 order
350  
351 order(CodeRef $callback) : Object
352  
353 The order method registers a sort order (search ordering) and returns a new
354 invocant instance.
355  
356 =over 4
357  
358 =item order example #1
359  
360 # given: synopsis
361  
362 $nodes = $nodes->order(sub {
363 my ($a, $b) = @_;
364  
365 $a->id cmp $b->id
366 });
367  
368 =back
369  
370 =cut
371  
372 =head2 scope
373  
374 scope(CodeRef $callback) : Object
375  
376 The scope method registers a scope (search filter) and returns a new invocant
377 instance.
378  
379 =over 4
380  
381 =item scope example #1
382  
383 # given: synopsis
384  
385 $nodes = $nodes->scope(sub {
386 my ($node) = @_;
387  
388 !!$node->{active}
389 });
390  
391 =back
392  
393 =cut
394  
395 =head2 search
396  
397 search() : Search
398  
399 The search method returns a L<Nano::Search> object associated with the invocant.
400  
401 =over 4
402  
403 =item search example #1
404  
405 # given: synopsis
406  
407 my $search = $nodes->search;
408  
409 =back
410  
411 =cut
412  
413 =head2 serialize
414  
415 serialize() : HashRef
416  
417 The serialize method returns a persistence representaton of the invocant.
418 Circular dependencies can result in a deep recursion error, however, circular
419 dependencies can be persisted if modeled properly. B<Note:> blessed objects
420 which are neither L<Nano::Node> nor L<Nano::Nodes> will be ignored.
421  
422 =over 4
423  
424 =item serialize example #1
425  
426 # given: synopsis
427  
428 my $serial = $nodes->serialize;
429  
430 =back
431  
432 =cut
433  
434 =head2 set
435  
436 set(Node $object) : Object
437  
438 The set method adds the node object provided to the index and returns the
439 provided object.
440  
441 =over 4
442  
443 =item set example #1
444  
445 # given: synopsis
446  
447 use Nano::Node;
448  
449 my $node = Nano::Node->new(id => '0000003');
450  
451 $node = $nodes->set($node);
452  
453 =back
454  
455 =cut
456  
457 =head1 AUTHOR
458  
459 Al Newkirk, C<awncorp@cpan.org>
460  
461 =head1 LICENSE
462  
463 Copyright (C) 2011-2019, Al Newkirk, et al.
464  
465 This is free software; you can redistribute it and/or modify it under the terms
466 of the The Apache License, Version 2.0, as elucidated in the L<"license
467 file"|https://github.com/cpanery/nano/blob/master/LICENSE>.
468  
469 =head1 PROJECT
470  
471 L<Wiki|https://github.com/cpanery/nano/wiki>
472  
473 L<Project|https://github.com/cpanery/nano>
474  
475 L<Initiatives|https://github.com/cpanery/nano/projects>
476  
477 L<Milestones|https://github.com/cpanery/nano/milestones>
478  
479 L<Contributing|https://github.com/cpanery/nano/blob/master/CONTRIBUTE.md>
480  
481 L<Issues|https://github.com/cpanery/nano/issues>
482  
483 =cut