| 1 | package Venus::Dump; | ||
| 2 | |||
| 3 | use 5.018; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use overload ( | ||
| 9 | '""' => 'explain', | ||
| 10 | '~~' => 'explain', | ||
| 11 | fallback => 1, | ||
| 12 | ); | ||
| 13 | |||
| 14 | use Venus::Class 'attr', 'base', 'with'; | ||
| 15 | |||
| 16 | base 'Venus::Kind::Utility'; | ||
| 17 | |||
| 18 | with 'Venus::Role::Valuable'; | ||
| 19 | with 'Venus::Role::Buildable'; | ||
| 20 | with 'Venus::Role::Accessible'; | ||
| 21 | with 'Venus::Role::Explainable'; | ||
| 22 | |||
| 23 | # ATTRIBUTES | ||
| 24 | |||
| 25 | attr 'decoder'; | ||
| 26 | attr 'encoder'; | ||
| 27 | |||
| 28 | # BUILDERS | ||
| 29 | |||
| 30 | sub build_arg { | ||
| 31 | my ($self, $data) = @_; | ||
| 32 | |||
| 33 | return { | ||
| 34 | value => $data | ||
| 35 | }; | ||
| 36 | } | ||
| 37 | |||
| 38 | sub build_args { | ||
| 39 | my ($self, $data) = @_; | ||
| 40 | |||
| 41 | if (keys %$data == 1 && exists $data->{value}) { | ||
| 42 | return $data; | ||
| 43 | } | ||
| 44 | return { | ||
| 45 | value => $data | ||
| 46 | }; | ||
| 47 | } | ||
| 48 | |||
| 49 | sub build_nil { | ||
| 50 | my ($self, $data) = @_; | ||
| 51 | |||
| 52 | return { | ||
| 53 | value => $data | ||
| 54 | }; | ||
| 55 | } | ||
| 56 | |||
| 57 | sub build_self { | ||
| 58 | my ($self, $data) = @_; | ||
| 59 | |||
| 60 | $self->encoder(sub { | ||
| 61 | my ($data) = @_; | ||
| 62 | require Data::Dumper; | ||
| 63 | no warnings 'once'; | ||
| 64 | local $Data::Dumper::Indent = 1; | ||
| 65 | local $Data::Dumper::Purity = 1; | ||
| 66 | local $Data::Dumper::Quotekeys = 0; | ||
| 67 | local $Data::Dumper::Deepcopy = 1; | ||
| 68 | local $Data::Dumper::Deparse = 1; | ||
| 69 | local $Data::Dumper::Sortkeys = 1; | ||
| 70 | local $Data::Dumper::Terse = 1; | ||
| 71 | local $Data::Dumper::Useqq = 1; | ||
| 72 | Data::Dumper->Dump([$data]) | ||
| 73 | }); | ||
| 74 | |||
| 75 | $self->decoder(sub { | ||
| 76 | my ($text) = @_; | ||
| 77 | require Symbol; | ||
| 78 | my $name = join '::', __PACKAGE__, join '_', 'Eval', rand =~ s/\D//gr; | ||
| 79 | my $data = eval "package $name; no warnings; return $text"; | ||
| 80 | Symbol::delete_package($name); | ||
| 81 | return $data; | ||
| 82 | }); | ||
| 83 | |||
| 84 | return $self; | ||
| 85 | } | ||
| 86 | |||
| 87 | # METHODS | ||
| 88 | |||
| 89 | sub assertion { | ||
| 90 | my ($self) = @_; | ||
| 91 | |||
| 92 | my $assert = $self->SUPER::assertion; | ||
| 93 | |||
| 94 | $assert->clear->expression('hashref'); | ||
| 95 | |||
| 96 | return $assert; | ||
| 97 | } | ||
| 98 | |||
| 99 | sub decode { | ||
| 100 | my ($self, $data) = @_; | ||
| 101 | |||
| 102 | # double-traversing the data structure due to lack of boolean support | ||
| 103 | return $self->set(FROM_BOOL($self->decoder->($data))); | ||
| 104 | } | ||
| 105 | |||
| 106 | sub encode { | ||
| 107 | my ($self) = @_; | ||
| 108 | |||
| 109 | # double-traversing the data structure due to lack of boolean support | ||
| 110 | return $self->encoder->(TO_BOOL($self->get)); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub explain { | ||
| 114 | my ($self) = @_; | ||
| 115 | |||
| 116 | return $self->encode; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub FROM_BOOL { | ||
| 120 | my ($value) = @_; | ||
| 121 | |||
| 122 | require Venus::Boolean; | ||
| 123 | |||
| 124 | if (ref($value) eq 'HASH') { | ||
| 125 | for my $key (keys %$value) { | ||
| 126 | $value->{$key} = FROM_BOOL($value->{$key}); | ||
| 127 | } | ||
| 128 | return $value; | ||
| 129 | } | ||
| 130 | |||
| 131 | if (ref($value) eq 'ARRAY') { | ||
| 132 | for my $key (keys @$value) { | ||
| 133 | $value->[$key] = FROM_BOOL($value->[$key]); | ||
| 134 | } | ||
| 135 | return $value; | ||
| 136 | } | ||
| 137 | |||
| 138 | return Venus::Boolean::TO_BOOL(Venus::Boolean::FROM_BOOL($value)); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub TO_BOOL { | ||
| 142 | my ($value) = @_; | ||
| 143 | |||
| 144 | require Venus::Boolean; | ||
| 145 | |||
| 146 | if (ref($value) eq 'HASH') { | ||
| 147 | $value = { | ||
| 148 | %$value | ||
| 149 | }; | ||
| 150 | for my $key (keys %$value) { | ||
| 151 | $value->{$key} = TO_BOOL($value->{$key}); | ||
| 152 | } | ||
| 153 | return $value; | ||
| 154 | } | ||
| 155 | |||
| 156 | if (ref($value) eq 'ARRAY') { | ||
| 157 | $value = [ | ||
| 158 | @$value | ||
| 159 | ]; | ||
| 160 | for my $key (keys @$value) { | ||
| 161 | $value->[$key] = TO_BOOL($value->[$key]); | ||
| 162 | } | ||
| 163 | return $value; | ||
| 164 | } | ||
| 165 | |||
| 166 | return Venus::Boolean::TO_BOOL_TFO($value); | ||
| 167 | } | ||
| 168 | |||
| 169 | 1; | ||
| 170 | |||
| 171 | |||
| 172 | |||
| 173 | =head1 NAME | ||
| 174 | |||
| 175 | Venus::Dump - Dump Class | ||
| 176 | |||
| 177 | =cut | ||
| 178 | |||
| 179 | =head1 ABSTRACT | ||
| 180 | |||
| 181 | Dump Class for Perl 5 | ||
| 182 | |||
| 183 | =cut | ||
| 184 | |||
| 185 | =head1 SYNOPSIS | ||
| 186 | |||
| 187 | package main; | ||
| 188 | |||
| 189 | use Venus::Dump; | ||
| 190 | |||
| 191 | my $dump = Venus::Dump->new( | ||
| 192 | value => { name => ['Ready', 'Robot'], version => 0.12, stable => !!1, } | ||
| 193 | ); | ||
| 194 | |||
| 195 | # $dump->encode; | ||
| 196 | |||
| 197 | =cut | ||
| 198 | |||
| 199 | =head1 DESCRIPTION | ||
| 200 | |||
| 201 | This package provides methods for reading and writing dumped (i.e. | ||
| 202 | stringified) Perl data. | ||
| 203 | |||
| 204 | =cut | ||
| 205 | |||
| 206 | =head1 ATTRIBUTES | ||
| 207 | |||
| 208 | This package has the following attributes: | ||
| 209 | |||
| 210 | =cut | ||
| 211 | |||
| 212 | =head2 decoder | ||
| 213 | |||
| 214 | decoder(CodeRef) | ||
| 215 | |||
| 216 | This attribute is read-write, accepts C<(CodeRef)> values, and is optional. | ||
| 217 | |||
| 218 | =cut | ||
| 219 | |||
| 220 | =head2 encoder | ||
| 221 | |||
| 222 | encoder(CodeRef) | ||
| 223 | |||
| 224 | This attribute is read-write, accepts C<(CodeRef)> values, and is optional. | ||
| 225 | |||
| 226 | =cut | ||
| 227 | |||
| 228 | =head1 INHERITS | ||
| 229 | |||
| 230 | This package inherits behaviors from: | ||
| 231 | |||
| 232 | L<Venus::Kind::Utility> | ||
| 233 | |||
| 234 | =cut | ||
| 235 | |||
| 236 | =head1 INTEGRATES | ||
| 237 | |||
| 238 | This package integrates behaviors from: | ||
| 239 | |||
| 240 | L<Venus::Role::Accessible> | ||
| 241 | |||
| 242 | L<Venus::Role::Buildable> | ||
| 243 | |||
| 244 | L<Venus::Role::Explainable> | ||
| 245 | |||
| 246 | L<Venus::Role::Valuable> | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | =head1 METHODS | ||
| 251 | |||
| 252 | This package provides the following methods: | ||
| 253 | |||
| 254 | =cut | ||
| 255 | |||
| 256 | =head2 decode | ||
| 257 | |||
| 258 | decode(string $text) (any) | ||
| 259 | |||
| 260 | The decode method decodes the Perl string, sets the object value, and returns | ||
| 261 | the decoded value. | ||
| 262 | |||
| 263 | I<Since C<0.01>> | ||
| 264 | |||
| 265 | =over 4 | ||
| 266 | |||
| 267 | =item decode example 1 | ||
| 268 | |||
| 269 | # given: synopsis; | ||
| 270 | |||
| 271 | my $decode = $dump->decode('{codename=>["Ready","Robot"],stable=>!!1}'); | ||
| 272 | |||
| 273 | # { codename => ["Ready", "Robot"], stable => 1 } | ||
| 274 | |||
| 275 | =back | ||
| 276 | |||
| 277 | =cut | ||
| 278 | |||
| 279 | =head2 encode | ||
| 280 | |||
| 281 | encode() (string) | ||
| 282 | |||
| 283 | The encode method encodes the objects value as a Perl string and returns the | ||
| 284 | encoded string. | ||
| 285 | |||
| 286 | I<Since C<0.01>> | ||
| 287 | |||
| 288 | =over 4 | ||
| 289 | |||
| 290 | =item encode example 1 | ||
| 291 | |||
| 292 | # given: synopsis; | ||
| 293 | |||
| 294 | my $encode = $dump->encode; | ||
| 295 | |||
| 296 | # '{name => ["Ready","Robot"], stable => !!1, version => "0.12"}' | ||
| 297 | |||
| 298 | =back | ||
| 299 | |||
| 300 | =cut | ||
| 301 | |||
| 302 | =head1 AUTHORS | ||
| 303 | |||
| 304 | Awncorp, C<awncorp@cpan.org> | ||
| 305 | |||
| 306 | =cut | ||
| 307 | |||
| 308 | =head1 LICENSE | ||
| 309 | |||
| 310 | Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>. | ||
| 311 | |||
| 312 | This program is free software, you can redistribute it and/or modify it under | ||
| 313 | the terms of the Apache license version 2.0. | ||
| 314 | |||
| 315 | =cut |