Skip to content
58 changes: 37 additions & 21 deletions lib/MooseX/Storage/Engine.pm
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,8 @@ my %OBJECT_HANDLERS = (
);


my %TYPES = (
my %TYPES;
%TYPES = (
# NOTE:
# we need to make sure that we properly numify the numbers
# before and after them being futzed with, because some of
Expand All @@ -225,23 +226,20 @@ my %TYPES = (
'Value' => { expand => sub { shift }, collapse => sub { shift } },
'Bool' => { expand => sub { shift }, collapse => sub { shift } },
# These are the trickier ones, (see notes)
# NOTE:
# Because we are nice guys, we will check
# your ArrayRef and/or HashRef one level
# down and inflate any objects we find.
# But this is where it ends, it is too
# expensive to try and do this any more
# recursively, when it is probably not
# necessary in most of the use cases.
# However, if you need more then this, subtype
# and add a custom handler.
'ArrayRef' => {
expand => sub {
my ( $array, @args ) = @_;
foreach my $i (0 .. $#{$array}) {
next unless ref($array->[$i]) eq 'HASH'
&& exists $array->[$i]->{$CLASS_MARKER};
$array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i], @args);
if (ref($array->[$i]) eq 'HASH') {
$array->[$i] = exists($array->[$i]{$CLASS_MARKER})
? $TYPES{ $array->[$i]{$CLASS_MARKER} }
? $TYPES{ $array->[$i]{$CLASS_MARKER} }{expand}->($array->[$i], @args)
: $OBJECT_HANDLERS{expand}->($array->[$i], @args)
: $TYPES{HashRef}{expand}->($array->[$i], @args);
}
elsif (ref($array->[$i]) eq 'ARRAY') {
$array->[$i] = $TYPES{ArrayRef}{expand}->($array->[$i], @args);
}
}
$array;
},
Expand All @@ -252,7 +250,9 @@ my %TYPES = (
# otherwise it will affect the
# other real version.
[ map {
blessed($_)
$TYPES{ref($_)}
? $TYPES{ref($_)}->{collapse}->($_, @args)
: blessed($_)
? $OBJECT_HANDLERS{collapse}->($_, @args)
: $_
} @$array ]
Expand All @@ -262,9 +262,16 @@ my %TYPES = (
expand => sub {
my ( $hash, @args ) = @_;
foreach my $k (keys %$hash) {
next unless ref($hash->{$k}) eq 'HASH'
&& exists $hash->{$k}->{$CLASS_MARKER};
$hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}, @args);
if (ref($hash->{$k}) eq 'HASH' ) {
$hash->{$k} = exists($hash->{$k}->{$CLASS_MARKER})
? $TYPES{ $hash->{$k}->{$CLASS_MARKER} }
? $TYPES{ $hash->{$k}->{$CLASS_MARKER} }{expand}->($hash->{$k}, @args)
: $OBJECT_HANDLERS{expand}->($hash->{$k}, @args)
: $TYPES{HashRef}{expand}->($hash->{$k}, @args);
}
elsif (ref($hash->{$k}) eq 'ARRAY') {
$hash->{$k} = $TYPES{ArrayRef}{expand}->($hash->{$k}, @args);
}
}
$hash;
},
Expand All @@ -275,9 +282,11 @@ my %TYPES = (
# otherwise it will affect the
# other real version.
+{ map {
blessed($hash->{$_})
? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
: ($_ => $hash->{$_})
$TYPES{ref($hash->{$_})}
? ($_ => $TYPES{ref($hash->{$_})}{collapse}->($hash->{$_}, @args))
: blessed($hash->{$_})
? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}, @args))
: ($_ => $hash->{$_})
} keys %$hash }
}
},
Expand All @@ -292,6 +301,13 @@ my %TYPES = (
#}
);

%TYPES = (
%TYPES,
'HASH' => $TYPES{HashRef},
'ARRAY' => $TYPES{ArrayRef},
);


sub add_custom_type_handler {
my ($self, $type_name, %handlers) = @_;
(exists $handlers{expand} && exists $handlers{collapse})
Expand Down
Loading