Initial trait for doing per request schema
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / PerRequestSchema.pm
1 package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema;
2 use Moose;
3 use namespace::autoclean;
4
5 sub BUILD {}
6 after BUILD => sub {
7     my ($self) = @_;
8     confess("You have not implemented a per_request_schema_attributes method in " . ref($self))
9         unless $self->can('per_request_schema_attributes');
10 };
11
12 with 'Catalyst::Component::InstancePerContext';
13
14 sub build_per_context_instance {
15     my ( $self, $ctx ) = @_;
16     return $self unless blessed($ctx);
17
18     my $new = bless {%$self}, ref $self;
19
20     $new->schema( $new->schema->clone($self->per_request_schema_attributes($ctx)) );
21
22     return $new;
23 }
24
25 __PACKAGE__->meta->make_immutable;
26