cdf00ee0734bdc672cd0c5a81d7281ccfdf22a33
[catagits/Catalyst-Model-DBIC-Schema.git] / lib / Catalyst / TraitFor / Model / DBIC / Schema / PerRequestSchema.pm
1 package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema;
2
3 use Moose::Role;
4 use namespace::autoclean;
5
6 requires 'per_request_schema_attributes';
7
8 with 'Catalyst::Component::InstancePerContext';
9
10 =head1 NAME
11
12 Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema - Clone the schema
13 with attributes for each requests
14
15 =head1 SYNOPSIS
16
17     __PACKAGE__->config({
18         traits => ['PerRequestSchema'],
19     });
20
21     method per_request_schema_attributes($c) {
22         return (restricting_object => $c->user->obj);
23     }
24
25 =head1 DESCRIPTION
26
27 Clones the schema for each new request with the attributes retrieved from your
28 C<per_request_schema_attributes> method, which you must implement. This method
29 is passed the context.
30
31 =cut
32
33 sub build_per_context_instance {
34     my ( $self, $ctx ) = @_;
35     return $self unless blessed($ctx);
36
37     my $new = bless {%$self}, ref $self;
38
39     $new->schema( $new->schema->clone($self->per_request_schema_attributes($ctx)) );
40
41     return $new;
42 }
43
44 __PACKAGE__->meta->make_immutable;
45
46 =head1 SEE ALSO
47
48 L<Catalyst::Model::DBIC::Schema>, L<DBIx::Class::Schema>
49
50 =head1 AUTHOR
51
52 See L<Catalyst::Model::DBIC::Schema/AUTHOR> and
53 L<Catalyst::Model::DBIC::Schema/CONTRIBUTORS>.
54
55 =head1 COPYRIGHT
56
57 See L<Catalyst::Model::DBIC::Schema/COPYRIGHT>.
58
59 =head1 LICENSE
60
61 This program is free software, you can redistribute it and/or modify it
62 under the same terms as Perl itself.
63
64 =cut
65
66 1;