shared database connection trait
skaufman [Mon, 23 Jan 2012 20:56:38 +0000 (20:56 +0000)]
lib/Catalyst/TraitFor/Model/DBIC/Schema/SharedDatabaseConnection.pm [new file with mode: 0644]

diff --git a/lib/Catalyst/TraitFor/Model/DBIC/Schema/SharedDatabaseConnection.pm b/lib/Catalyst/TraitFor/Model/DBIC/Schema/SharedDatabaseConnection.pm
new file mode 100644 (file)
index 0000000..7918d15
--- /dev/null
@@ -0,0 +1,29 @@
+use strict;
+use warnings;
+
+package Catalyst::TraitFor::Model::DBIC::Schema::SharedDatabaseConnection;
+
+use Moose::Role;
+
+has parent_model => (
+    is       => 'ro',
+    isa      => 'Str',
+    required => 1,
+);
+
+has cloned => (
+    is      => 'rw',
+    default => 0,
+);
+around ACCEPT_CONTEXT => sub {
+    my ( $orig, $self, $app, @rest ) = ( shift, shift, @_ );
+    unless ( $self->cloned ) {
+        $self->schema->storage(
+            $app->model( $self->parent_model )->schema->storage );
+        $self->cloned(1);
+    }
+    $self->$orig( $app, @rest );
+};
+
+no Moose::Role;
+1;