Commit | Line | Data |
79ac28c6 |
1 | package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema; |
63dcfadb |
2 | |
2a9509a4 |
3 | use Moose::Role; |
79ac28c6 |
4 | use namespace::autoclean; |
5 | |
2a9509a4 |
6 | requires 'per_request_schema_attributes'; |
79ac28c6 |
7 | |
8 | with 'Catalyst::Component::InstancePerContext'; |
9 | |
63dcfadb |
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 | |
79ac28c6 |
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 | |
63dcfadb |
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; |