| 1 | package Nano::Stash; | ||
| 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::Role; | ||
| 12 | use Data::Object::RoleHas; | ||
| 13 | |||
| 14 | requires 'nano'; | ||
| 15 | |||
| 16 | our $VERSION = '0.06'; # VERSION | ||
| 17 | |||
| 18 | # ATTRIBUTES | ||
| 19 | |||
| 20 | has stashed => ( | ||
| 21 | is => 'ro', | ||
| 22 | isa => 'HashRef', | ||
| 23 | new => 1, | ||
| 24 | ); | ||
| 25 | |||
| 26 | fun new_stashed($self) { | ||
| 27 | {} | ||
| 28 | } | ||
| 29 | |||
| 30 | # METHODS | ||
| 31 | |||
| 32 | method get(Str $name) { | ||
| 33 | my $id = $self->stashed->{$name}; | ||
| 34 | return $id ? $self->nano->find($id) : undef; | ||
| 35 | } | ||
| 36 | |||
| 37 | method set(Str $name, Node $node) { | ||
| 38 | $self->stashed->{$name} = $node->id; | ||
| 39 | return $node; | ||
| 40 | } | ||
| 41 | |||
| 42 | method stash(Str $name, Maybe[Node] $node) { | ||
| 43 | return $node ? $self->set($name, $node) : $self->get($name); | ||
| 44 | } | ||
| 45 | |||
| 46 | 1; | ||
| 47 | |||
| 48 | =encoding utf8 | ||
| 49 | |||
| 50 | =head1 NAME | ||
| 51 | |||
| 52 | Nano::Stash - Stashable Role | ||
| 53 | |||
| 54 | =cut | ||
| 55 | |||
| 56 | =head1 ABSTRACT | ||
| 57 | |||
| 58 | Stashable Entity Role | ||
| 59 | |||
| 60 | =cut | ||
| 61 | |||
| 62 | =head1 SYNOPSIS | ||
| 63 | |||
| 64 | package Example; | ||
| 65 | |||
| 66 | use Moo; | ||
| 67 | |||
| 68 | extends 'Nano::Node'; | ||
| 69 | |||
| 70 | with 'Nano::Stash'; | ||
| 71 | |||
| 72 | sub bestie { | ||
| 73 | my ($self, @args) = @_; | ||
| 74 | return $self->stash('bestie', @args); | ||
| 75 | } | ||
| 76 | |||
| 77 | package main; | ||
| 78 | |||
| 79 | my $example = Example->new; | ||
| 80 | |||
| 81 | # $example->bestie($example); | ||
| 82 | |||
| 83 | =cut | ||
| 84 | |||
| 85 | =head1 DESCRIPTION | ||
| 86 | |||
| 87 | This package provides an entity-stashing role, useful for the ad-hoc persisting | ||
| 88 | of L<Nano::Node> entities. This role also makes it possible to save/load | ||
| 89 | circularly dependent entities. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | =head1 LIBRARIES | ||
| 94 | |||
| 95 | This package uses type constraints from: | ||
| 96 | |||
| 97 | L<Nano::Types> | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | =head1 ATTRIBUTES | ||
| 102 | |||
| 103 | This package has the following attributes: | ||
| 104 | |||
| 105 | =cut | ||
| 106 | |||
| 107 | =head2 stashed | ||
| 108 | |||
| 109 | stashed(HashRef) | ||
| 110 | |||
| 111 | This attribute is read-only, accepts C<(HashRef)> values, and is optional. | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | =head1 METHODS | ||
| 116 | |||
| 117 | This package implements the following methods: | ||
| 118 | |||
| 119 | =cut | ||
| 120 | |||
| 121 | =head2 get | ||
| 122 | |||
| 123 | get(Str $name) : Maybe[Node] | ||
| 124 | |||
| 125 | The get method finds and loads a previously stashed L<Nano::Node> entity. | ||
| 126 | |||
| 127 | =over 4 | ||
| 128 | |||
| 129 | =item get example #1 | ||
| 130 | |||
| 131 | my $example = Example->new; | ||
| 132 | |||
| 133 | my $bestie = $example->get('bestie'); | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =over 4 | ||
| 138 | |||
| 139 | =item get example #2 | ||
| 140 | |||
| 141 | my $example = Example->new; | ||
| 142 | |||
| 143 | $example->bestie($example); | ||
| 144 | $example->save; | ||
| 145 | |||
| 146 | my $bestie = $example->get('bestie'); | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | =head2 set | ||
| 153 | |||
| 154 | set(Str $name, Node $node) : Node | ||
| 155 | |||
| 156 | The set method stashes the L<Nano::Node> entity provided by name. This does not | ||
| 157 | save the subject or invocant. | ||
| 158 | |||
| 159 | =over 4 | ||
| 160 | |||
| 161 | =item set example #1 | ||
| 162 | |||
| 163 | my $example = Example->new; | ||
| 164 | |||
| 165 | my $bestie = $example->set('bestie', $example); | ||
| 166 | |||
| 167 | =back | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | =head2 stash | ||
| 172 | |||
| 173 | stash(Str $name, Maybe[Node] $node) : Maybe[Node] | ||
| 174 | |||
| 175 | The stash method will L</get> or L</set> a stashed L<Nano::Node> entity based | ||
| 176 | on the arguments provided. | ||
| 177 | |||
| 178 | =over 4 | ||
| 179 | |||
| 180 | =item stash example #1 | ||
| 181 | |||
| 182 | my $example = Example->new; | ||
| 183 | |||
| 184 | my $bestie = $example->stash('bestie'); | ||
| 185 | |||
| 186 | =back | ||
| 187 | |||
| 188 | =over 4 | ||
| 189 | |||
| 190 | =item stash example #2 | ||
| 191 | |||
| 192 | my $example = Example->new; | ||
| 193 | |||
| 194 | $example->bestie($example); | ||
| 195 | $example->save; | ||
| 196 | |||
| 197 | my $bestie = $example->stash('bestie'); | ||
| 198 | |||
| 199 | =back | ||
| 200 | |||
| 201 | =over 4 | ||
| 202 | |||
| 203 | =item stash example #3 | ||
| 204 | |||
| 205 | my $example = Example->new; | ||
| 206 | |||
| 207 | my $bestie = $example->stash('bestie', $example); | ||
| 208 | |||
| 209 | =back | ||
| 210 | |||
| 211 | =cut | ||
| 212 | |||
| 213 | =head1 AUTHOR | ||
| 214 | |||
| 215 | Al Newkirk, C<awncorp@cpan.org> | ||
| 216 | |||
| 217 | =head1 LICENSE | ||
| 218 | |||
| 219 | Copyright (C) 2011-2019, Al Newkirk, et al. | ||
| 220 | |||
| 221 | This is free software; you can redistribute it and/or modify it under the terms | ||
| 222 | of the The Apache License, Version 2.0, as elucidated in the L<"license | ||
| 223 | file"|https://github.com/cpanery/nano/blob/master/LICENSE>. | ||
| 224 | |||
| 225 | =head1 PROJECT | ||
| 226 | |||
| 227 | L<Wiki|https://github.com/cpanery/nano/wiki> | ||
| 228 | |||
| 229 | L<Project|https://github.com/cpanery/nano> | ||
| 230 | |||
| 231 | L<Initiatives|https://github.com/cpanery/nano/projects> | ||
| 232 | |||
| 233 | L<Milestones|https://github.com/cpanery/nano/milestones> | ||
| 234 | |||
| 235 | L<Contributing|https://github.com/cpanery/nano/blob/master/CONTRIBUTE.md> | ||
| 236 | |||
| 237 | L<Issues|https://github.com/cpanery/nano/issues> | ||
| 238 | |||
| 239 | =cut |