reuse code refs from ->can
Guillermo Roditi [Sat, 22 Nov 2008 00:06:46 +0000 (00:06 +0000)]
Changes
lib/DBIx/Class/Schema/RestrictWithObject/RestrictComp/Source.pm
t/var/RestrictByUserTest.db

diff --git a/Changes b/Changes
index 555ac37..c06c3e0 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,2 +1,5 @@
-0.00001_01 NODATEYET
-        -initial release
+0.00002 
+  -reuse coderefs from can
+
+0.00001
+  -initial release
index 10dd5bb..a8de0f5 100644 (file)
@@ -18,21 +18,28 @@ Intercept call to C<resultset> 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;
 }
index bf10322..dd6c9e8 100644 (file)
Binary files a/t/var/RestrictByUserTest.db and b/t/var/RestrictByUserTest.db differ