foo
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage / Engine.pm
index 91125c4..1177469 100644 (file)
@@ -2,8 +2,20 @@
 package MooseX::Storage::Engine;
 use Moose;
 
+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 {{}}
 );
@@ -14,15 +26,28 @@ has 'class'  => (is => 'rw', isa => 'Str');
 ## this is the API used by other modules ...
 
 sub collapse_object {
-       my $self = shift;
+    my ( $self, @args ) = @_;
+
+       # NOTE:
+       # mark the root object as seen ...
+       $self->seen->{$self->object} = undef;
+       
     $self->map_attributes('collapse_attribute');
-    $self->storage->{'__class__'} = $self->object->meta->name;    
+    $self->storage->{$CLASS_MARKER} = $self->object->meta->identifier;    
        return $self->storage;
 }
 
 sub expand_object {
-    my ($self, $data) = @_;
-    $self->map_attributes('expand_attribute', $data);
+    my ($self, $data, %options) = @_;
+    
+    $options{check_version}   = 1 unless exists $options{check_version};
+    $options{check_authority} = 1 unless exists $options{check_authority};   
+    
+       # NOTE:
+       # mark the root object as seen ...
+       $self->seen->{$data} = undef;    
+    
+    $self->map_attributes('expand_attribute', $data, \%options);
        return $self->storage;    
 }
 
@@ -34,15 +59,23 @@ sub collapse_attribute {
 }
 
 sub expand_attribute {
-    my ($self, $attr, $data)  = @_;
-    $self->storage->{$attr->name} = $self->expand_attribute_value($attr, $data->{$attr->name}) || return;
+    my ($self, $attr, $data, $options)  = @_;
+    $self->storage->{$attr->name} = $self->expand_attribute_value($attr, $data->{$attr->name}, $options) || return;
 }
 
 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->match_type($attr->type_constraint);
+        my $type_converter = $self->find_type_handler($attr->type_constraint);
         (defined $type_converter)
             || confess "Cannot convert " . $attr->type_constraint->name;
         $value = $type_converter->{collapse}->($value);
@@ -51,43 +84,181 @@ sub collapse_attribute_value {
 }
 
 sub expand_attribute_value {
-    my ($self, $attr, $value)  = @_;
+    my ($self, $attr, $value, $options)  = @_;
+
+       # 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->match_type($attr->type_constraint);
-        $value = $type_converter->{expand}->($value);
+        my $type_converter = $self->find_type_handler($attr->type_constraint);
+        $value = $type_converter->{expand}->($value, $options);
     }
        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;
 }
 
+## ------------------------------------------------------------------
+## This is all the type handler stuff, it is in a state of flux
+## right now, so this may change, or it may just continue to be 
+## improved upon. Comments and suggestions are welcomed.
+## ------------------------------------------------------------------
+
+# NOTE:
+# these are needed by the 
+# ArrayRef and HashRef handlers
+# below, so I need easy access 
+my %OBJECT_HANDLERS = (
+    expand => sub {
+        my ($data, $options) = @_;   
+        (exists $data->{$CLASS_MARKER})
+            || confess "Serialized item has no class marker";
+        # 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 $@;     
+        
+        if ($options->{check_version}) {
+            my $meta_version = $meta->version;
+            if (defined $meta_version && $version) {            
+                if ($options->{check_version} eq 'allow_less_than') {
+                    ($meta_version <= $version)
+                        || confess "Class ($class) versions is not less than currently available." 
+                                 . " got=($version) available=($meta_version)";                
+                }
+                elsif ($options->{check_version} eq 'allow_greater_than') {
+                    ($meta->version >= $version)
+                        || confess "Class ($class) versions is not greater than currently available." 
+                                 . " got=($version) available=($meta_version)";                
+                }            
+                else {
+                    ($meta->version == $version)
+                        || confess "Class ($class) versions don't match." 
+                                 . " got=($version) available=($meta_version)";
+                }
+            }
+        }
+        
+        if ($options->{check_authority}) {
+            my $meta_authority = $meta->authority;
+            ($meta->authority eq $authority)
+                || confess "Class ($class) authorities don't match." 
+                         . " got=($authority) available=($meta_authority)"
+                if defined $meta_authority && defined $authority;            
+        }
+            
+        # all is well ...
+        $class->unpack($data);
+    },
+    collapse => sub {
+        my $obj = shift;
+#        ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
+#            || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
+        $obj->pack();
+    },
+);
+
+
 my %TYPES = (
+    # These are boring ones, so they use the identity function ...
     'Int'      => { expand => sub { shift }, collapse => sub { shift } },
     'Num'      => { expand => sub { shift }, collapse => sub { shift } },
     'Str'      => { expand => sub { shift }, collapse => sub { shift } },
-    'ArrayRef' => { expand => sub { shift }, collapse => sub { shift } },
-    'HashRef'  => { expand => sub { shift }, collapse => sub { shift } },
-    'Object'   => {
+    # 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 
+    # nessecary in most of the use cases.
+    # However, if you need more then this, subtype 
+    # and add a custom handler.    
+    'ArrayRef' => { 
         expand => sub {
-            my $data = shift;   
-            (exists $data->{'__class__'})
-                || confess "Serialized item has no class marker";
-            $data->{'__class__'}->unpack($data);
-        },
+            my $array = shift;
+            foreach my $i (0 .. $#{$array}) {
+                next unless ref($array->[$i]) eq 'HASH' 
+                         && exists $array->[$i]->{$CLASS_MARKER};
+                $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i])
+            }
+            $array;
+        }, 
+        collapse => sub { 
+            my $array = shift;   
+            # NOTE:         
+            # we need to make a copy cause
+            # otherwise it will affect the 
+            # other real version.
+            [ map {
+                blessed($_)
+                    ? $OBJECT_HANDLERS{collapse}->($_)
+                    : $_
+            } @$array ] 
+        } 
+    },
+    'HashRef'  => { 
+        expand   => sub {
+            my $hash = shift;
+            foreach my $k (keys %$hash) {
+                next unless ref($hash->{$k}) eq 'HASH' 
+                         && exists $hash->{$k}->{$CLASS_MARKER};
+                $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k})
+            }
+            $hash;            
+        }, 
         collapse => sub {
-            my $obj = shift;
-            ($obj->can('does') && $obj->does('MooseX::Storage::Basic'))
-                || confess "Bad object ($obj) does not do MooseX::Storage::Basic role";
-            $obj->pack();
-        },
+            my $hash = shift;   
+            # NOTE:         
+            # we need to make a copy cause
+            # otherwise it will affect the 
+            # other real version.
+            +{ map {
+                blessed($hash->{$_})
+                    ? ($_ => $OBJECT_HANDLERS{collapse}->($hash->{$_}))
+                    : ($_ => $hash->{$_})
+            } keys %$hash }            
+        } 
     },
+    'Object'   => \%OBJECT_HANDLERS,
     # NOTE:
     # The sanity of enabling this feature by 
     # default is very questionable.
@@ -95,10 +266,22 @@ my %TYPES = (
     #'CodeRef' => {
     #    expand   => sub {}, # use eval ...
     #    collapse => sub {}, # use B::Deparse ...        
-    #}       
+    #} 
 );
 
-sub match_type {
+sub add_custom_type_handler {
+    my ($class, $type_name, %handlers) = @_;
+    (exists $handlers{expand} && exists $handlers{collapse})
+        || confess "Custom type handlers need an expand *and* a collapse method";
+    $TYPES{$type_name} = \%handlers;
+}
+
+sub remove_custom_type_handler {
+    my ($class, $type_name) = @_;
+    delete $TYPES{$type_name} if exists $TYPES{$type_name};
+}
+
+sub find_type_handler {
     my ($self, $type_constraint) = @_;
     
     # this should handle most type usages
@@ -131,15 +314,6 @@ sub match_type {
     # totally out the door ;)
     # - SL
     
-
-       # To cover the last possibilities we 
-       # need a way for people to extend this 
-       # process. Which they can do by subclassing
-       # this class and overriding the method 
-       # below to handle things.
-       my $match = $self->custom_type_match($type_constraint);
-       return $match if defined $match;
-
     # NOTE:
     # if this method hasnt returned by now
     # then we have no been able to find a 
@@ -147,11 +321,6 @@ sub match_type {
     confess "Cannot handle type constraint (" . $type_constraint->name . ")";    
 }
 
-sub custom_type_match {
-    return;
-    # my ($self, $type_constraint) = @_;
-}
-
 1;
 
 __END__
@@ -160,12 +329,12 @@ __END__
 
 =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
@@ -178,6 +347,8 @@ MooseX::Storage::Engine
 
 =item B<storage>
 
+=item B<seen>
+
 =back
 
 =head2 API
@@ -202,9 +373,23 @@ MooseX::Storage::Engine
 
 =item B<expand_attribute_value>
 
+=item B<check_for_cycle_in_collapse>
+
+=item B<check_for_cycle_in_expansion>
+
 =item B<map_attributes>
 
-=item B<match_type>
+=back
+
+=head2 Type Constraint Handlers
+
+=over 4
+
+=item B<find_type_handler>
+
+=item B<add_custom_type_handler>
+
+=item B<remove_custom_type_handler>
 
 =back