Nano.pm untested

filter: Statementstmt 0.0% 0 / 98 Branchbran 0.0% 0 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod n/a totaltotal 0.0% 0 / 141 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package Nano;
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 use Data::Object::Space;
14  
15 use Scalar::Util ();
16  
17 our $VERSION = '0.06'; # VERSION
18  
19 # ATTRIBUTES
20  
21 has env => (
22 is => 'ro',
23 isa => 'Env',
24 new => 1,
25 );
26  
27 fun new_env($self) {
28 require Nano::Env; Nano::Env->new
29 }
30  
31 # SUBS
32  
33 sub _dump {
34 my ($data) = @_;
35  
36 if (!defined $data) {
37 return undef;
38 }
39 elsif (Scalar::Util::blessed($data)) {
40 if ($data->isa('Nano::Node')) {
41 return {
42 '$node' => $data->save
43 };
44 }
45 else {
46 return {
47 '$skip' => 1,
48 };
49 }
50 }
51 elsif (UNIVERSAL::isa($data, 'ARRAY')) {
52 my $copy = [];
53 for (my $i = 0; $i < @$data; $i++) {
54 $copy->[$i] = _dump($data->[$i]);
55 }
56 return $copy;
57 }
58 elsif (UNIVERSAL::isa($data, 'HASH')) {
59 my $copy = {};
60 for my $key (keys %$data) {
61 $copy->{$key} = _dump($data->{$key});
62 }
63 return $copy;
64 }
65 elsif (ref($data)) {
66 return {
67 '$skip' => 1,
68 };
69 }
70 else {
71 return $data;
72 }
73 }
74  
75 sub _load {
76 my ($self, $data) = @_;
77  
78 if (!defined $data) {
79 return undef;
80 }
81 elsif (UNIVERSAL::isa($data, 'ARRAY')) {
82 my $copy = [];
83 for (my $i = 0; $i < @$data; $i++) {
84 if (ref($data->[$i]) eq 'HASH') {
85 next if $data->[$i]{'$skip'};
86 }
87 $copy->[$i] = _load($self, $data->[$i]);
88 }
89 return $copy;
90 }
91 elsif (UNIVERSAL::isa($data, 'HASH')) {
92 if (defined($data->{'$node'})) {
93 return $self->object($self->term($data->{'$node'})->object($self->env)->recv);
94 }
95 my $copy = {};
96 for my $key (keys %$data) {
97 if (ref($data->{$key}) eq 'HASH') {
98 next if $data->{$key}{'$skip'};
99 }
100 $copy->{$key} = _load($self, $data->{$key});
101 }
102 return $copy;
103 }
104 else {
105 return $data;
106 }
107 }
108  
109 # METHODS
110  
111 method dump(Object $object) {
112 return _dump({%{($object)}});
113 }
114  
115 method find(Str $name) {
116 if (my $keyval = $self->keyval($name)) {
117 if (my $object = $keyval->recv) {
118 return $self->object($object);
119 }
120 }
121 return undef;
122 }
123  
124 method hash(Str $name) {
125 require Digest::SHA; Digest::SHA::sha1_hex($name);
126 }
127  
128 method keyval(Str $name) {
129 return $self->env->app->keyval(name => $name);
130 }
131  
132 method name(Object $object) {
133 return ref($object);
134 }
135  
136 method object(HashRef $object) {
137 return $self->reify(@{$object}{qw($name $data)});
138 }
139  
140 method reify(Str $name, HashRef $data) {
141 return Data::Object::Space->new($name)->build($self->_load($data));
142 }
143  
144 method table(Str $name) {
145 return $self->env->app->table(name => $name, type => 'keyval');
146 }
147  
148 method term(Str $term) {
149 return $self->env->app->term($term);
150 }
151  
152 1;
153  
154 =encoding utf8
155  
156 =head1 NAME
157  
158 Nano - Object Persistence
159  
160 =cut
161  
162 =head1 ABSTRACT
163  
164 Minimalist Object Persistence
165  
166 =cut
167  
168 =head1 SYNOPSIS
169  
170 package Person;
171  
172 use Moo;
173  
174 extends 'Nano::Node';
175  
176 has name => (
177 is => 'ro',
178 required => 1,
179 );
180  
181 has friends => (
182 is => 'ro',
183 default => sub { People->new }
184 );
185  
186 sub extroverted {
187 my ($self) = @_;
188 ($self->friends->count > 1) ? 1 : 0
189 }
190  
191 sub introverted {
192 my ($self) = @_;
193 ($self->friends->count < 2) ? 1 : 0
194 }
195  
196 package People;
197  
198 use Moo;
199  
200 extends 'Nano::Nodes';
201  
202 sub new_type {
203 'Person'
204 }
205  
206 sub extroverted {
207 my ($self) = @_;
208  
209 $self->scope(sub {
210 my ($person) = @_;
211 $person->extroverted
212 });
213 }
214  
215 sub introverted {
216 my ($self) = @_;
217  
218 $self->scope(sub {
219 my ($person) = @_;
220 $person->introverted
221 });
222 }
223  
224 package main;
225  
226 my $rachel = Person->new(
227 id => 'rachel',
228 name => 'rachel',
229 );
230 my $monica = Person->new(
231 id => 'monica',
232 name => 'monica',
233 );
234 my $phoebe = Person->new(
235 id => 'phoebe',
236 name => 'phoebe',
237 );
238  
239 $rachel->friends->set($monica);
240 $rachel->friends->set($phoebe);
241  
242 $monica->friends->set($rachel);
243 $monica->friends->set($phoebe);
244  
245 $phoebe->friends->set($rachel);
246 $phoebe->friends->set($monica);
247  
248 $rachel->save;
249 $monica->save;
250 $phoebe->save;
251  
252 $phoebe->friends->count; # 2
253 $phoebe->friends->extroverted->count; # 2
254 $phoebe->friends->introverted->count; # 0
255  
256 my $nano = Nano->new;
257  
258 my $friend = $nano->find('rachel');
259  
260 =cut
261  
262 =head1 DESCRIPTION
263  
264 This package provides a minimalist framework for persisting objects (I<i.e.
265 class instances>) with as little effort as possible. This framework relies on
266 the L<Zing> toolkit which provides pluggable storage and serialization options.
267  
268 =cut
269  
270 =head1 LIBRARIES
271  
272 This package uses type constraints from:
273  
274 L<Nano::Types>
275  
276 =cut
277  
278 =head1 ATTRIBUTES
279  
280 This package has the following attributes:
281  
282 =cut
283  
284 =head2 env
285  
286 env(Env)
287  
288 This attribute is read-only, accepts C<(Env)> values, and is optional.
289  
290 =cut
291  
292 =head1 METHODS
293  
294 This package implements the following methods:
295  
296 =cut
297  
298 =head2 dump
299  
300 dump(Object $object) : HashRef
301  
302 The dump method returns a serialized hash representation for the object
303 provided.
304  
305 =over 4
306  
307 =item dump example #1
308  
309 my $nano = Nano->new;
310  
311 my $rachel = $nano->find('rachel');
312  
313 my $dump = $nano->dump($rachel);
314  
315 =back
316  
317 =cut
318  
319 =head2 find
320  
321 find(Str $name) : Node
322  
323 The find method finds, inflates, and returns a prior persisted object for the
324 ID provided.
325  
326 =over 4
327  
328 =item find example #1
329  
330 my $nano = Nano->new;
331  
332 my $phoebe = $nano->find('phoebe');
333  
334 =back
335  
336 =cut
337  
338 =head2 hash
339  
340 hash(Str $name) : Str
341  
342 The hash method returns a SHA-1 digest for the string provided.
343  
344 =over 4
345  
346 =item hash example #1
347  
348 my $nano = Nano->new;
349  
350 my $email = 'me@example.com';
351  
352 $nano->hash($email);
353  
354 =back
355  
356 =cut
357  
358 =head2 keyval
359  
360 keyval(Str $name) : KeyVal
361  
362 The keyval method returns a L<Zing::KeyVal> object for the ID provided.
363  
364 =over 4
365  
366 =item keyval example #1
367  
368 my $nano = Nano->new;
369  
370 my $keyval = $nano->keyval('rachel');
371  
372 =back
373  
374 =cut
375  
376 =head2 name
377  
378 name(Object $object) : Str
379  
380 The name method returns the class name for the object provided.
381  
382 =over 4
383  
384 =item name example #1
385  
386 my $nano = Nano->new;
387  
388 my $rachel = $nano->find('rachel');
389  
390 my $name = $nano->name($rachel);
391  
392 =back
393  
394 =cut
395  
396 =head2 object
397  
398 object(HashRef $object) : Object
399  
400 The object method returns an object derived from a prior serialization
401 representation.
402  
403 =over 4
404  
405 =item object example #1
406  
407 my $nano = Nano->new;
408  
409 my $new_rachel = $nano->object({
410 '$type' => 'node',
411 '$name' => 'Person',
412 '$data' => {
413 'id' => 'rachel',
414 'name' => 'rachel',
415 'nano' => {
416 '$skip' => 1
417 },
418 'friends' => {
419 '$skip' => 1
420 },
421 },
422 });
423  
424 =back
425  
426 =cut
427  
428 =head2 reify
429  
430 reify(Str $name, HashRef $data) : Object
431  
432 The reify method constructs an object from the class name and data provided.
433  
434 =over 4
435  
436 =item reify example #1
437  
438 my $nano = Nano->new;
439  
440 my $new_rachel = $nano->reify('Person', {
441 id => 'rachel',
442 name => 'rachel',
443 });
444  
445 =back
446  
447 =cut
448  
449 =head2 table
450  
451 table(Str $name) : Table
452  
453 The table method returns a L<Zing::Table> object for the ID provided.
454  
455 =over 4
456  
457 =item table example #1
458  
459 my $nano = Nano->new;
460  
461 my $rachel = $nano->find('rachel');
462  
463 my $table = $nano->table($rachel->friends->id);
464  
465 =back
466  
467 =cut
468  
469 =head1 AUTHOR
470  
471 Al Newkirk, C<awncorp@cpan.org>
472  
473 =head1 LICENSE
474  
475 Copyright (C) 2011-2019, Al Newkirk, et al.
476  
477 This is free software; you can redistribute it and/or modify it under the terms
478 of the The Apache License, Version 2.0, as elucidated in the L<"license
479 file"|https://github.com/cpanery/nano/blob/master/LICENSE>.
480  
481 =head1 PROJECT
482  
483 L<Wiki|https://github.com/cpanery/nano/wiki>
484  
485 L<Project|https://github.com/cpanery/nano>
486  
487 L<Initiatives|https://github.com/cpanery/nano/projects>
488  
489 L<Milestones|https://github.com/cpanery/nano/milestones>
490  
491 L<Contributing|https://github.com/cpanery/nano/blob/master/CONTRIBUTE.md>
492  
493 L<Issues|https://github.com/cpanery/nano/issues>
494  
495 =cut