| 1 | package Nano::Node; | ||
| 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 | our $VERSION = '0.06'; # VERSION | ||
| 15 | |||
| 16 | # ATTRIBUTES | ||
| 17 | |||
| 18 | has id => ( | ||
| 19 | is => 'ro', | ||
| 20 | isa => 'Str', | ||
| 21 | new => 1, | ||
| 22 | ); | ||
| 23 | |||
| 24 | fun new_id($self) { | ||
| 25 | require Zing::ID; Zing::ID->new->string | ||
| 26 | } | ||
| 27 | |||
| 28 | has nano => ( | ||
| 29 | is => 'ro', | ||
| 30 | isa => 'Nano', | ||
| 31 | new => 1, | ||
| 32 | ); | ||
| 33 | |||
| 34 | fun new_nano($self) { | ||
| 35 | require Nano; Nano->new | ||
| 36 | } | ||
| 37 | |||
| 38 | # METHODS | ||
| 39 | |||
| 40 | method drop() { | ||
| 41 | my $keyval = $self->nano->keyval($self->id); | ||
| 42 | $keyval->drop; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | method load() { | ||
| 47 | return $self->nano->find($self->id); | ||
| 48 | } | ||
| 49 | |||
| 50 | method save() { | ||
| 51 | my $serial = $self->serialize; | ||
| 52 | my $keyval = $self->nano->keyval($self->id); | ||
| 53 | $keyval->send($serial); | ||
| 54 | return $keyval->term; | ||
| 55 | } | ||
| 56 | |||
| 57 | method serialize() { | ||
| 58 | $self->id; | ||
| 59 | { | ||
| 60 | '$name' => $self->nano->name($self), | ||
| 61 | '$data' => $self->nano->dump($self), | ||
| 62 | '$type' => 'node', | ||
| 63 | } | ||
| 64 | } | ||
| 65 | |||
| 66 | 1; | ||
| 67 | |||
| 68 | =encoding utf8 | ||
| 69 | |||
| 70 | =head1 NAME | ||
| 71 | |||
| 72 | Nano::Node - Persistable Entity | ||
| 73 | |||
| 74 | =cut | ||
| 75 | |||
| 76 | =head1 ABSTRACT | ||
| 77 | |||
| 78 | Persistable Entity Super Class | ||
| 79 | |||
| 80 | =cut | ||
| 81 | |||
| 82 | =head1 SYNOPSIS | ||
| 83 | |||
| 84 | use Nano::Node; | ||
| 85 | |||
| 86 | my $node = Nano::Node->new( | ||
| 87 | id => '0000001', | ||
| 88 | ); | ||
| 89 | |||
| 90 | # $node->save; | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | =head1 DESCRIPTION | ||
| 95 | |||
| 96 | This package provides a persistable entity super class. It is meant to be | ||
| 97 | subclassed but can be used directly as well. | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 LIBRARIES | ||
| 102 | |||
| 103 | This package uses type constraints from: | ||
| 104 | |||
| 105 | L<Nano::Types> | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | =head1 ATTRIBUTES | ||
| 110 | |||
| 111 | This package has the following attributes: | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | =head2 id | ||
| 116 | |||
| 117 | id(Str) | ||
| 118 | |||
| 119 | This attribute is read-only, accepts C<(Str)> values, and is optional. | ||
| 120 | |||
| 121 | =cut | ||
| 122 | |||
| 123 | =head2 nano | ||
| 124 | |||
| 125 | nano(Nano) | ||
| 126 | |||
| 127 | This attribute is read-only, accepts C<(Nano)> values, and is optional. | ||
| 128 | |||
| 129 | =cut | ||
| 130 | |||
| 131 | =head1 METHODS | ||
| 132 | |||
| 133 | This package implements the following methods: | ||
| 134 | |||
| 135 | =cut | ||
| 136 | |||
| 137 | =head2 drop | ||
| 138 | |||
| 139 | drop() : Object | ||
| 140 | |||
| 141 | The drop method removes the prior persisted object data. | ||
| 142 | |||
| 143 | =over 4 | ||
| 144 | |||
| 145 | =item drop example #1 | ||
| 146 | |||
| 147 | # given: synopsis | ||
| 148 | |||
| 149 | $node = $node->drop; | ||
| 150 | |||
| 151 | =back | ||
| 152 | |||
| 153 | =cut | ||
| 154 | |||
| 155 | =head2 load | ||
| 156 | |||
| 157 | load() : Object | ||
| 158 | |||
| 159 | The load method reloads and returns an object from source. | ||
| 160 | |||
| 161 | =over 4 | ||
| 162 | |||
| 163 | =item load example #1 | ||
| 164 | |||
| 165 | # given: synopsis | ||
| 166 | |||
| 167 | $node->save; | ||
| 168 | |||
| 169 | $node = $node->load; | ||
| 170 | |||
| 171 | =back | ||
| 172 | |||
| 173 | =cut | ||
| 174 | |||
| 175 | =head2 save | ||
| 176 | |||
| 177 | save() : Str | ||
| 178 | |||
| 179 | The save method commits the object data to the storage backend. | ||
| 180 | |||
| 181 | =over 4 | ||
| 182 | |||
| 183 | =item save example #1 | ||
| 184 | |||
| 185 | # given: synopsis | ||
| 186 | |||
| 187 | my $term = $node->save; | ||
| 188 | |||
| 189 | =back | ||
| 190 | |||
| 191 | =cut | ||
| 192 | |||
| 193 | =head2 serialize | ||
| 194 | |||
| 195 | serialize() : HashRef | ||
| 196 | |||
| 197 | The serialize method returns a persistence representaton of the invocant. | ||
| 198 | Circular dependencies can result in a deep recursion error, however, circular | ||
| 199 | dependencies can be persisted if modeled properly. B<Note:> blessed objects | ||
| 200 | which are neither L<Nano::Node> nor L<Nano::Nodes> will be ignored. | ||
| 201 | |||
| 202 | =over 4 | ||
| 203 | |||
| 204 | =item serialize example #1 | ||
| 205 | |||
| 206 | # given: synopsis | ||
| 207 | |||
| 208 | my $serial = $node->serialize; | ||
| 209 | |||
| 210 | =back | ||
| 211 | |||
| 212 | =cut | ||
| 213 | |||
| 214 | =head1 AUTHOR | ||
| 215 | |||
| 216 | Al Newkirk, C<awncorp@cpan.org> | ||
| 217 | |||
| 218 | =head1 LICENSE | ||
| 219 | |||
| 220 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 221 | |||
| 222 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 223 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 224 | file"|https://github.com/cpanery/nano/blob/master/LICENSE>. | ||
| 225 | |||
| 226 | =head1 PROJECT | ||
| 227 | |||
| 228 | L<Wiki|https://github.com/cpanery/nano/wiki> | ||
| 229 | |||
| 230 | L<Project|https://github.com/cpanery/nano> | ||
| 231 | |||
| 232 | L<Initiatives|https://github.com/cpanery/nano/projects> | ||
| 233 | |||
| 234 | L<Milestones|https://github.com/cpanery/nano/milestones> | ||
| 235 | |||
| 236 | L<Contributing|https://github.com/cpanery/nano/blob/master/CONTRIBUTE.md> | ||
| 237 | |||
| 238 | L<Issues|https://github.com/cpanery/nano/issues> | ||
| 239 | |||
| 240 | =cut |