Fix issue I'm seeing in MX::Storage when using it in multiple roles which I combine.
[gitmo/MooseX-Storage.git] / lib / MooseX / Storage.pm
index ccf48f4..810b764 100644 (file)
@@ -4,6 +4,9 @@ use Moose qw(confess);
 
 use MooseX::Storage::Meta::Attribute::DoNotSerialize;
 
+our $VERSION   = '0.15';
+our $AUTHORITY = 'cpan:STEVAN';
+
 sub import {
     my $pkg = caller();
     
@@ -12,38 +15,49 @@ sub import {
     ($pkg->can('meta'))
         || confess "This package can only be used in Moose based classes";
     
-    $pkg->meta->alias_method('Storage' => sub {
-        my %params = @_;
+    $pkg->meta->add_method('Storage' => __PACKAGE__->meta->find_method_by_name('_injected_storage_role_generator')); 
+}
+
+sub _injected_storage_role_generator {
+    my %params = @_;
         
-        $params{'base'} ||= 'Basic';
+    if (exists $params{'base'}) {
+        $params{'base'} = ('Base::' . $params{'base'});        
+    }
+    else {
+        $params{'base'} = 'Basic';        
+    }
         
-        my @roles = (
-            ('MooseX::Storage::' . $params{'base'}),
-        );
+    my @roles = (
+        ('MooseX::Storage::' . $params{'base'}),
+    );
         
-        # NOTE:
-        # you don't have to have a format 
-        # role, this just means you dont 
-        # get anything other than pack/unpack
-        push @roles => 'MooseX::Storage::Format::' . $params{'format'}
-            if exists $params{'format'};
+    # NOTE:
+    # you don't have to have a format 
+    # role, this just means you dont 
+    # get anything other than pack/unpack
+    push @roles => 'MooseX::Storage::Format::' . $params{'format'}
+        if exists $params{'format'};
             
+    # NOTE:
+    # many IO roles don't make sense unless 
+    # you have also have a format role chosen
+    # too, the exception being StorableFile
+    if (exists $params{'io'}) {
         # NOTE:
-        # if you do choose an IO role, then 
-        # you *must* have a format role chosen
-        # since load/store require freeze/thaw
-        if (exists $params{'io'}) {
-            (exists $params{'format'})
-                || confess "You must specify a format role in order to use an IO role";
-            push @roles => 'MooseX::Storage::IO::' . $params{'io'};
-        }
+        # we dont need this code anymore, cause 
+        # the role composition will catch it for 
+        # us. This allows the StorableFile to work
+        #(exists $params{'format'})
+        #    || confess "You must specify a format role in order to use an IO role";
+        push @roles => 'MooseX::Storage::IO::' . $params{'io'};
+    }
         
-        Class::MOP::load_class($_) 
-            || die "Could not load role (" . $_ . ") for package ($pkg)"
-                foreach @roles;        
+    Class::MOP::load_class($_) 
+        || die "Could not load role (" . $_ . ")"
+            foreach @roles;        
         
-        return @roles;
-    });
+    return @roles;
 }
 
 1;
@@ -62,6 +76,8 @@ MooseX::Storage - An serialization framework for Moose classes
   use Moose;
   use MooseX::Storage;
   
+  our $VERSION = '0.01';
+  
   with Storage('format' => 'JSON', 'io' => 'File');
   
   has 'x' => (is => 'rw', isa => 'Int');
@@ -75,20 +91,20 @@ MooseX::Storage - An serialization framework for Moose classes
   ## object in perl data structures
   
   # pack the class into a hash
-  $p->pack(); # { __CLASS__ => 'Point', x => 10, y => 10 }
+  $p->pack(); # { __CLASS__ => 'Point-0.01', x => 10, y => 10 }
   
   # unpack the hash into a class
-  my $p2 = Point->unpack({ __CLASS__ => 'Point', x => 10, y => 10 });
+  my $p2 = Point->unpack({ __CLASS__ => 'Point-0.01', x => 10, y => 10 });
 
   ## methods to freeze/thaw into 
   ## a specified serialization format
   ## (in this case JSON)
   
   # pack the class into a JSON string
-  $p->freeze(); # { "__CLASS__" : "Point", "x" : 10, "y" : 10 }
+  $p->freeze(); # { "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }
   
   # unpack the JSON string into a class
-  my $p2 = Point->thaw('{ "__CLASS__" : "Point", "x" : 10, "y" : 10 }');  
+  my $p2 = Point->thaw('{ "__CLASS__" : "Point-0.01", "x" : 10, "y" : 10 }');  
 
   ## methods to load/store a class 
   ## on the file system
@@ -103,6 +119,14 @@ MooseX::Storage is a serialization framework for Moose, it provides
 a very flexible and highly pluggable way to serialize Moose classes
 to a number of different formats and styles.
 
+=head2 Important Note
+
+This is still an early release of this module, so use with caution. 
+It's outward facing serialization API should be considered stable, 
+but I still reserve the right to make tweaks if I need too. Anything
+beyond the basic pack/unpack, freeze/thaw and load/store should not 
+be relied on.
+
 =head2 Levels of Serialization
 
 There are 3 levels to the serialization, each of which builds upon 
@@ -135,8 +159,8 @@ have it. You can just use C<pack>/C<unpack> instead.
 The third (io) level is C<load> and C<store>. In this level we are reading 
 and writing data to file/network/database/etc. 
 
-This level is also optional, it does however require the C<format> level 
-to be present (at least the current state does).
+This level is also optional, in most cases it does require a C<format> role
+to also be used, the expection being the C<StorableFile> role.
 
 =back
 
@@ -216,8 +240,8 @@ hand if you like.
 
 =head1 TODO
 
-This module needs docs and probably a couple a Cookbook of some kind 
-as well. This is an early release, so that is my excuse for now :)
+This module needs docs and probably a Cookbook of some kind as well. 
+This is an early release, so that is my excuse for now :)
 
 For the time being, please read the tests and feel free to email me 
 if you have any questions. This module can also be discussed on IRC 
@@ -235,9 +259,11 @@ Chris Prather E<lt>chris.prather@iinteractive.comE<gt>
 
 Stevan Little E<lt>stevan.little@iinteractive.comE<gt>
 
+Yuval Kogman E<lt>yuval.kogman@iinteractive.comE<gt>
+
 =head1 COPYRIGHT AND LICENSE
 
-Copyright 2007 by Infinity Interactive, Inc.
+Copyright 2007-2008 by Infinity Interactive, Inc.
 
 L<http://www.iinteractive.com>