package MooseX::Storage::Engine;
use Moose;
+our $VERSION = '0.01';
+
+# the class marker when
+# serializing an object.
+our $CLASS_MARKER = '__CLASS__';
+
has 'storage' => (
is => 'rw',
isa => 'HashRef',
sub collapse_object {
my $self = shift;
$self->map_attributes('collapse_attribute');
- $self->storage->{'__class__'} = $self->object->meta->name;
+ $self->storage->{$CLASS_MARKER} = $self->object->meta->name;
return $self->storage;
}
my %OBJECT_HANDLERS = (
expand => sub {
my $data = shift;
- (exists $data->{'__class__'})
+ (exists $data->{$CLASS_MARKER})
|| confess "Serialized item has no class marker";
- $data->{'__class__'}->unpack($data);
+ $data->{$CLASS_MARKER}->unpack($data);
},
collapse => sub {
my $obj = shift;
my $array = shift;
foreach my $i (0 .. $#{$array}) {
next unless ref($array->[$i]) eq 'HASH'
- && exists $array->[$i]->{'__class__'};
+ && exists $array->[$i]->{$CLASS_MARKER};
$array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i])
}
$array;
my $hash = shift;
foreach my $k (keys %$hash) {
next unless ref($hash->{$k}) eq 'HASH'
- && exists $hash->{$k}->{'__class__'};
+ && exists $hash->{$k}->{$CLASS_MARKER};
$hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k})
}
$hash;
is_deeply(
$foo->pack,
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 10,
string => 'foo',
float => 10.5,
array => [ 1 .. 10 ],
hash => { map { $_ => undef } ( 1 .. 10 ) },
object => {
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 2
},
},
{
my $foo = Foo->unpack(
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 10,
string => 'foo',
float => 10.5,
array => [ 1 .. 10 ],
hash => { map { $_ => undef } ( 1 .. 10 ) },
object => {
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 2
},
}
is_deeply(
$foo->pack,
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 10,
string => 'foo',
float => 10.5,
array => [ 1 .. 10 ],
hash => { map { $_ => undef } ( 1 .. 10 ) },
object => {
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 2
},
},
{
my $foo = Foo->unpack(
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 10,
string => 'foo',
float => 10.5,
array => [ 1 .. 10 ],
hash => { map { $_ => undef } ( 1 .. 10 ) },
object => {
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
number => 2
},
}
is_deeply(
$foo->pack,
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
bars => [
map {
{
- __class__ => 'Bar',
+ __CLASS__ => 'Bar',
number => $_,
}
} (1 .. 10)
{
my $foo = Foo->unpack(
{
- __class__ => 'Foo',
+ __CLASS__ => 'Foo',
bars => [
map {
{
- __class__ => 'Bar',
+ __CLASS__ => 'Bar',
number => $_,
}
} (1 .. 10)
is_deeply(
$baz->pack,
{
- __class__ => 'Baz',
+ __CLASS__ => 'Baz',
bars => {
map {
($_ => {
- __class__ => 'Bar',
+ __CLASS__ => 'Bar',
number => $_,
})
} (1 .. 10)
{
my $baz = Baz->unpack(
{
- __class__ => 'Baz',
+ __CLASS__ => 'Baz',
bars => {
map {
($_ => {
- __class__ => 'Bar',
+ __CLASS__ => 'Bar',
number => $_,
})
} (1 .. 10)
is_json(
$json,
- '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}',
+ '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}',
'... got the right JSON'
);
}
{
my $foo = Foo->thaw(
- '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}'
+ '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}'
);
isa_ok( $foo, 'Foo' );
is(
$yaml,
q{---
-__class__: Foo
+__CLASS__: Foo
array:
- 1
- 2
9: ~
number: 10
object:
- __class__: Foo
+ __CLASS__: Foo
number: 2
string: foo
},
{
my $foo = Foo->thaw(q{---
-__class__: Foo
+__CLASS__: Foo
array:
- 1
- 2
9: ~
number: 10
object:
- __class__: Foo
+ __CLASS__: Foo
number: 2
string: foo
});
+++ /dev/null
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-
-use Test::More no_plan => 1;
-
-BEGIN {
- use_ok('MooseX::Storage');
-}
-
-{
- package Foo;
- use Moose;
- use MooseX::Storage;
-
- with Storage(format => 'YAML', io => 'File');
-
- has 'number' => (is => 'ro', isa => 'Int');
- has 'string' => (is => 'ro', isa => 'Str');
- has 'float' => (is => 'ro', isa => 'Num');
- has 'array' => (is => 'ro', isa => 'ArrayRef');
- has 'hash' => (is => 'ro', isa => 'HashRef');
- has 'object' => (is => 'ro', isa => 'Object');
-}
-
-my $file = 'temp.yaml';
-
-{
- my $foo = Foo->new(
- number => 10,
- string => 'foo',
- float => 10.5,
- array => [ 1 .. 10 ],
- hash => { map { $_ => undef } (1 .. 10) },
- object => Foo->new( number => 2 ),
- );
- isa_ok($foo, 'Foo');
-
- $foo->store($file);
-}
-
-{
- my $foo = Foo->load($file);
- isa_ok($foo, 'Foo');
-
- is($foo->number, 10, '... got the right number');
- is($foo->string, 'foo', '... got the right string');
- is($foo->float, 10.5, '... got the right float');
- is_deeply($foo->array, [ 1 .. 10], '... got the right array');
- is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
-
- isa_ok($foo->object, 'Foo');
- is($foo->object->number, 2, '... got the right number (in the embedded object)');
-}
-
-unlink $file;
+++ /dev/null
-#!/usr/bin/perl
-
-use strict;
-use warnings;
-
-use Test::More no_plan => 1;
-
-BEGIN {
- use_ok('MooseX::Storage');
-}
-
-{
- package Foo;
- use Moose;
- use MooseX::Storage;
-
- with Storage(format => 'YAML', io => 'AtomicFile');
-
- has 'number' => (is => 'ro', isa => 'Int');
- has 'string' => (is => 'ro', isa => 'Str');
- has 'float' => (is => 'ro', isa => 'Num');
- has 'array' => (is => 'ro', isa => 'ArrayRef');
- has 'hash' => (is => 'ro', isa => 'HashRef');
- has 'object' => (is => 'ro', isa => 'Object');
-}
-
-my $file = 'temp.yaml';
-
-{
- my $foo = Foo->new(
- number => 10,
- string => 'foo',
- float => 10.5,
- array => [ 1 .. 10 ],
- hash => { map { $_ => undef } (1 .. 10) },
- object => Foo->new( number => 2 ),
- );
- isa_ok($foo, 'Foo');
-
- $foo->store($file);
-}
-
-{
- my $foo = Foo->load($file);
- isa_ok($foo, 'Foo');
-
- is($foo->number, 10, '... got the right number');
- is($foo->string, 'foo', '... got the right string');
- is($foo->float, 10.5, '... got the right float');
- is_deeply($foo->array, [ 1 .. 10], '... got the right array');
- is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
-
- isa_ok($foo->object, 'Foo');
- is($foo->object->number, 2, '... got the right number (in the embedded object)');
-}
-
-unlink $file;