From: t0m <bobtfish@bobtfish.net>
Date: Wed, 24 Jun 2009 21:18:29 +0000 (+0100)
Subject: Actually do something useful with the engine_traits parameter, by appling a role... 
X-Git-Tag: 0.20~9
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a473d69d41c77bb4ba2d94dfd731bdcf6e7a8d63;p=gitmo%2FMooseX-Storage.git

Actually do something useful with the engine_traits parameter, by appling a role in the MooseX::Storage::Engine::Trait:: namespace to the engine instance created
---

diff --git a/lib/MooseX/Storage/Basic.pm b/lib/MooseX/Storage/Basic.pm
index cfdbb1f..a803e74 100644
--- a/lib/MooseX/Storage/Basic.pm
+++ b/lib/MooseX/Storage/Basic.pm
@@ -7,14 +7,14 @@ our $VERSION   = '0.18';
 our $AUTHORITY = 'cpan:STEVAN';
 
 sub pack {
-    my ( $self, @args ) = @_;
-    my $e = $self->_storage_get_engine_class->new( object => $self );
-    $e->collapse_object(@args);
+    my ( $self, %args ) = @_;
+    my $e = $self->_storage_get_engine_class(%args)->new( object => $self );
+    $e->collapse_object(%args);
 }
 
 sub unpack {
     my ($class, $data, %args) = @_;
-    my $e = $class->_storage_get_engine_class->new(class => $class);
+    my $e = $class->_storage_get_engine_class(%args)->new(class => $class);
     
     $class->_storage_construct_instance( 
         $e->expand_object($data, %args), 
@@ -23,7 +23,25 @@ sub unpack {
 }
 
 sub _storage_get_engine_class {
-    'MooseX::Storage::Engine';
+    my ($self, %args) = @_;
+
+    my $default = 'MooseX::Storage::Engine';
+
+    return $default
+        unless (
+            exists $args{engine_traits} 
+         && ref($args{engine_traits}) eq 'ARRAY'
+         && scalar(@{$args{engine_traits}})
+    );
+    
+    my @roles = map { sprintf("%s::Trait::%s", $default, $_) }
+        @{$args{engine_traits}};
+    
+    Moose::Meta::Class->create_anon_class(
+        superclasses => [$default],
+        roles => [ @roles ],
+        cache => 1,
+    )->name;
 }
 
 sub _storage_construct_instance {
diff --git a/lib/MooseX/Storage/Engine/Trait/DisableCycleDetection.pm b/lib/MooseX/Storage/Engine/Trait/DisableCycleDetection.pm
new file mode 100644
index 0000000..717e243
--- /dev/null
+++ b/lib/MooseX/Storage/Engine/Trait/DisableCycleDetection.pm
@@ -0,0 +1,5 @@
+package MooseX::Storage::Engine::Trait::DisableCycleDetection;
+use Moose::Role;
+
+1;
+
diff --git a/lib/MooseX/Storage/Engine/Trait/OnlyWhenBuilt.pm b/lib/MooseX/Storage/Engine/Trait/OnlyWhenBuilt.pm
new file mode 100644
index 0000000..163be53
--- /dev/null
+++ b/lib/MooseX/Storage/Engine/Trait/OnlyWhenBuilt.pm
@@ -0,0 +1,7 @@
+package MooseX::Storage::Engine::Trait::OnlyWhenBuilt;
+use Moose::Role;
+
+our $VERSION = 1;
+
+1;
+