package MooseX::Storage::Engine;
use Moose;
-our $VERSION = '0.01';
+our $VERSION = '0.02';
# the class marker when
# serializing an object.
our $CLASS_MARKER = '__CLASS__';
has 'storage' => (
- is => 'rw',
+ is => 'ro',
+ isa => 'HashRef',
+ default => sub {{}}
+);
+
+has 'seen' => (
+ is => 'ro',
isa => 'HashRef',
default => sub {{}}
);
sub collapse_object {
my $self = shift;
+
+ # NOTE:
+ # mark the root object as seen ...
+ $self->seen->{$self->object} = undef;
+
$self->map_attributes('collapse_attribute');
- $self->storage->{$CLASS_MARKER} = $self->object->meta->name;
+ $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;
return $self->storage;
}
sub expand_object {
my ($self, $data) = @_;
+
+ # NOTE:
+ # mark the root object as seen ...
+ $self->seen->{$data} = undef;
+
$self->map_attributes('expand_attribute', $data);
return $self->storage;
}
sub collapse_attribute_value {
my ($self, $attr) = @_;
my $value = $attr->get_value($self->object);
+
+ # NOTE:
+ # this might not be enough, we might
+ # need to make it possible for the
+ # cycle checker to return the value
+ $self->check_for_cycle_in_collapse($attr, $value)
+ if ref $value;
+
if (defined $value && $attr->has_type_constraint) {
my $type_converter = $self->find_type_handler($attr->type_constraint);
(defined $type_converter)
sub expand_attribute_value {
my ($self, $attr, $value) = @_;
+
+ # NOTE:
+ # (see comment in method above ^^)
+ $self->check_for_cycle_in_expansion($attr, $value)
+ if ref $value;
+
if (defined $value && $attr->has_type_constraint) {
my $type_converter = $self->find_type_handler($attr->type_constraint);
$value = $type_converter->{expand}->($value);
return $value;
}
+# NOTE:
+# possibly these two methods will
+# be used by a cycle supporting
+# engine. However, I am not sure
+# if I can make a cycle one work
+# anyway.
+
+sub check_for_cycle_in_collapse {
+ my ($self, $attr, $value) = @_;
+ (!exists $self->seen->{$value})
+ || confess "Basic Engine does not support cycles in class("
+ . ($attr->associated_class->name) . ").attr("
+ . ($attr->name) . ") with $value";
+ $self->seen->{$value} = undef;
+}
+
+sub check_for_cycle_in_expansion {
+ my ($self, $attr, $value) = @_;
+ (!exists $self->seen->{$value})
+ || confess "Basic Engine does not support cycles in class("
+ . ($attr->associated_class->name) . ").attr("
+ . ($attr->name) . ") with $value";
+ $self->seen->{$value} = undef;
+}
+
# util methods ...
sub map_attributes {
my ($self, $method_name, @args) = @_;
map {
$self->$method_name($_, @args)
+ } grep {
+ # Skip our special skip attribute :)
+ !$_->isa('MooseX::Storage::Meta::Attribute::DoNotSerialize')
} ($self->object || $self->class)->meta->compute_all_applicable_attributes;
}
my $data = shift;
(exists $data->{$CLASS_MARKER})
|| confess "Serialized item has no class marker";
- $data->{$CLASS_MARKER}->unpack($data);
+ # check the class more thoroughly here ...
+ my ($class, $version, $authority) = (split '-' => $data->{$CLASS_MARKER});
+ my $meta = eval { $class->meta };
+ confess "Class ($class) is not loaded, cannot unpack" if $@;
+ ($meta->version == $version)
+ || confess "Class ($class) versions don't match."
+ . " got=($version) available=(" . ($meta->version || '') . ")"
+ if defined $version;
+ ($meta->authority eq $authority)
+ || confess "Class ($class) authorities don't match."
+ . " got=($authority) available=(" . ($meta->authority || '') . ")"
+ if defined $authority;
+ # all is well ...
+ $class->unpack($data);
},
collapse => sub {
my $obj = shift;
#'CodeRef' => {
# expand => sub {}, # use eval ...
# collapse => sub {}, # use B::Deparse ...
- #}
+ #}
);
sub add_custom_type_handler {
=head1 NAME
-MooseX::Storage::Engine
-
-=head1 SYNOPSIS
+MooseX::Storage::Engine - The meta-engine to handle collapsing and expanding objects
=head1 DESCRIPTION
+No user serviceable parts inside. If you really want to know, read the source :)
+
=head1 METHODS
=head2 Accessors
=item B<storage>
+=item B<seen>
+
=back
=head2 API
=item B<expand_attribute_value>
+=item B<check_for_cycle_in_collapse>
+
+=item B<check_for_cycle_in_expansion>
+
=item B<map_attributes>
=back