From: Guillermo Roditi Date: Sat, 22 Nov 2008 00:06:46 +0000 (+0000) Subject: reuse code refs from ->can X-Git-Tag: 0.0002~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4b2a6da6c1917d7ec157f9ca1861fb261e277c2f;p=dbsrgits%2FDBIx-Class-Schema-RestrictWithObject.git reuse code refs from ->can --- diff --git a/Changes b/Changes index 555ac37..c06c3e0 100644 --- a/Changes +++ b/Changes @@ -1,2 +1,5 @@ -0.00001_01 NODATEYET - -initial release +0.00002 + -reuse coderefs from can + +0.00001 + -initial release diff --git a/lib/DBIx/Class/Schema/RestrictWithObject/RestrictComp/Source.pm b/lib/DBIx/Class/Schema/RestrictWithObject/RestrictComp/Source.pm index 10dd5bb..a8de0f5 100644 --- a/lib/DBIx/Class/Schema/RestrictWithObject/RestrictComp/Source.pm +++ b/lib/DBIx/Class/Schema/RestrictWithObject/RestrictComp/Source.pm @@ -18,21 +18,28 @@ Intercept call to C and return restricted resultset =cut +#TODO: +# - We should really be caching method name hits to avoid the can() +# unless it really is necessary. This would be done at the restrictor +# class level. {$source_name} => $restricting_method (undef if n/a) + sub resultset { my $self = shift; my $rs = $self->next::method(@_); - if (my $obj = $self->schema->restricting_object) { - my $s = $self->source_name; - $s =~ s/::/_/g; - my $pre = $self->schema->restricted_prefix; - my $meth = "restrict_${s}_resultset"; - - #if a prefix was set, try that first - if($pre){ - my $meth_pre = "restrict_${pre}_${s}_resultset"; - return $obj->$meth_pre($rs) if $obj->can($meth_pre); + my $obj = $self->schema->restricting_object; + return $rs unless $obj; + + my $s = $self->source_name; + $s =~ s/::/_/g; + #if a prefix was set, try that first + if(my $pre = $self->schema->restricted_prefix) { + if(my $coderef = $obj->can("restrict_${pre}_${s}_resultset")) { + return $obj->$coderef($rs); } - $rs = $obj->$meth($rs) if $obj->can($meth); + } + #should this be an elsif?! + if(my $coderef = $obj->can("restrict_${s}_resultset")) { + return $obj->$coderef($rs); } return $rs; } diff --git a/t/var/RestrictByUserTest.db b/t/var/RestrictByUserTest.db index bf10322..dd6c9e8 100644 Binary files a/t/var/RestrictByUserTest.db and b/t/var/RestrictByUserTest.db differ