From: Brendan Byrd Date: Fri, 6 Apr 2012 20:41:15 +0000 (-0400) Subject: add alt. hook to PerRequestSchema trait X-Git-Tag: v0.60~2 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=catagits%2FCatalyst-Model-DBIC-Schema.git;a=commitdiff_plain;h=566a0bcace1f933a42a84e4b92782d78911acbba add alt. hook to PerRequestSchema trait Allow defining the per_request_schema hook in the PerRequestSchema trait and document it. --- diff --git a/Changes b/Changes index e4f34fb..9ccf8ba 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,8 @@ Revision history for Perl extension Catalyst::Model::DBIC::Schema - - Additional paranoia in types as it's possible for loading code - to clobber $_ + - Add per_request_schema hook to PerRequestSchema trait and docs + - Additional paranoia in types as it's possible for loading code + to clobber $_ 0.59 2011-11-01 11:20:46 - update helper deps for new loader diff --git a/lib/Catalyst/Model/DBIC/Schema.pm b/lib/Catalyst/Model/DBIC/Schema.pm index 822f443..a1f229e 100644 --- a/lib/Catalyst/Model/DBIC/Schema.pm +++ b/lib/Catalyst/Model/DBIC/Schema.pm @@ -676,7 +676,7 @@ ozum: Ozum Eldogan C Pavel I. Shaydo C -t0m: Tomas Doran +SineSwiper: Brendan Byrd =head1 COPYRIGHT diff --git a/lib/Catalyst/TraitFor/Model/DBIC/Schema/PerRequestSchema.pm b/lib/Catalyst/TraitFor/Model/DBIC/Schema/PerRequestSchema.pm index 337a3c1..71bad99 100644 --- a/lib/Catalyst/TraitFor/Model/DBIC/Schema/PerRequestSchema.pm +++ b/lib/Catalyst/TraitFor/Model/DBIC/Schema/PerRequestSchema.pm @@ -1,9 +1,7 @@ package Catalyst::TraitFor::Model::DBIC::Schema::PerRequestSchema; use Moose::Role; -use namespace::autoclean; - -requires 'per_request_schema_attributes'; +use MooseX::MarkAsMethods autoclean => 1; with 'Catalyst::Component::InstancePerContext'; @@ -18,9 +16,16 @@ with attributes for each requests traits => ['PerRequestSchema'], }); - method per_request_schema_attributes($c) { + sub per_request_schema_attributes { + my ($self, $c) = @_; return (restricting_object => $c->user->obj); } + ### OR ### + sub per_request_schema { + my ($self, $c) = @_; + return $self->schema->schema_method($c->user->obj) + } + =head1 DESCRIPTION @@ -28,6 +33,10 @@ Clones the schema for each new request with the attributes retrieved from your C method, which you must implement. This method is passed the context. +Alternatively, you could also override the C method if you +need access to the schema clone and/or need to separate out the Model/Schema +methods. (See examples above and the defaults in the code.) + =cut sub build_per_context_instance { @@ -36,11 +45,23 @@ sub build_per_context_instance { my $new = bless {%$self}, ref $self; - $new->schema($new->schema->clone($self->per_request_schema_attributes($ctx))); + $new->schema($new->per_request_schema($ctx)); return $new; } +# Thanks to Matt Trout for this idea +sub per_request_schema { + my ($self, $c) = @_; + return $self->schema->clone($self->per_request_schema_attributes($c)); +} + +### TODO: This should probably be more elegant ### +sub per_request_schema_attributes { + confess "Either per_request_schema_attributes needs to be created, or per_request_schema needs to be overridden!"; +} + + =head1 SEE ALSO L, L