[Rt-commit] rt branch, 4.2/load-object-in-scalar-context, created. rt-4.1.17-198-gd9749a2

? sunnavy sunnavy at bestpractical.com
Tue Aug 20 18:11:39 EDT 2013


The branch, 4.2/load-object-in-scalar-context has been created
        at  d9749a2f80b55b6552204aaa0d77071878be5011 (commit)

- Log -----------------------------------------------------------------
commit d9749a2f80b55b6552204aaa0d77071878be5011
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Tue Feb 26 23:24:03 2013 +0800

    return status only in scalar context is more useful for Load... methods
    
    otherwise, it returns useless "2"(the list size) or the last item(i.e. message).
    
    now we can easily check if ->Load succeeded or not, for example,
    $principal->Load(...) or Abort(...)

diff --git a/lib/RT/ACE.pm b/lib/RT/ACE.pm
index f9879d7..ca1d054 100644
--- a/lib/RT/ACE.pm
+++ b/lib/RT/ACE.pm
@@ -120,7 +120,7 @@ sub LoadByValues {
     if ( $args{'RightName'} ) {
         my $canonic_name = $self->CanonicalizeRightName( $args{'RightName'} );
         unless ( $canonic_name ) {
-            return ( 0, $self->loc("Invalid right. Couldn't canonicalize right '[_1]'", $args{'RightName'}) );
+            return wantarray ? ( 0, $self->loc("Invalid right. Couldn't canonicalize right '[_1]'", $args{'RightName'}) ) : 0;
         }
         $args{'RightName'} = $canonic_name;
     }
@@ -131,14 +131,14 @@ sub LoadByValues {
                                      $args{'PrincipalType'} );
 
     unless ( $princ_obj->id ) {
-        return ( 0,
+        return wantarray ? ( 0,
                  $self->loc( 'Principal [_1] not found.', $args{'PrincipalId'} )
-        );
+        ) : 0;
     }
 
     my ($object, $object_type, $object_id) = $self->_ParseObjectArg( %args );
     unless( $object ) {
-        return ( 0, $self->loc("System error. Right not granted.") );
+        return wantarray ? ( 0, $self->loc("System error. Right not granted.")) : 0;
     }
 
     $self->LoadByCols( PrincipalId   => $princ_obj->Id,
@@ -149,11 +149,11 @@ sub LoadByValues {
 
     #If we couldn't load it.
     unless ( $self->Id ) {
-        return ( 0, $self->loc("ACE not found") );
+        return wantarray ? ( 0, $self->loc("ACE not found") ) : 0;
     }
 
     # if we could
-    return ( $self->Id, $self->loc("Right Loaded") );
+    return wantarray ? ( $self->Id, $self->loc("Right Loaded") ) : $self->Id;
 
 }
 
diff --git a/lib/RT/Article.pm b/lib/RT/Article.pm
index 8670462..78a9fb0 100644
--- a/lib/RT/Article.pm
+++ b/lib/RT/Article.pm
@@ -610,11 +610,11 @@ sub LoadByInclude {
     }
 
     unless ($ok) { # load failed, don't check Class
-        return ($ok, $msg);
+        return wantarray ? ($ok, $msg) : $ok;
     }
 
     unless ($Queue) { # we haven't requested extra sanity checking
-        return ($ok, $msg);
+        return wantarray ? ($ok, $msg) : $ok;
     }
 
     # ensure that this article is available for the Queue we're
@@ -622,10 +622,10 @@ sub LoadByInclude {
     my $class = $self->ClassObj;
     unless ($class->IsApplied(0) || $class->IsApplied($Queue)) {
         $self->LoadById(0);
-        return (0, $self->loc("The Class of the Article identified by [_1] is not applied to the current Queue",$Value));
+        return wantarray ? (0, $self->loc("The Class of the Article identified by [_1] is not applied to the current Queue",$Value)) : 0;
     }
 
-    return ($ok, $msg);
+    return wantarray ? ($ok, $msg) : $ok;
 
 }
 
diff --git a/lib/RT/Group.pm b/lib/RT/Group.pm
index 10c3aff..7ab0b65 100644
--- a/lib/RT/Group.pm
+++ b/lib/RT/Group.pm
@@ -251,7 +251,7 @@ sub LoadRoleGroup {
 
     my $object = delete $args{Object};
 
-    return (0, $self->loc("Object passed is not loaded"))
+    return wantarray ? (0, $self->loc("Object passed is not loaded")) : 0
        unless $object->id;
 
     # Translate Object to Domain + Instance
diff --git a/lib/RT/Link.pm b/lib/RT/Link.pm
index d51b3ce..43b7890 100644
--- a/lib/RT/Link.pm
+++ b/lib/RT/Link.pm
@@ -226,20 +226,20 @@ sub LoadByParams {
 
     my $base = RT::URI->new($self->CurrentUser);
     $base->FromURI( $args{'Base'} )
-        or return (0, $self->loc("Couldn't parse Base URI: [_1]", $args{Base}));
+        or return wantarray ? (0, $self->loc("Couldn't parse Base URI: [_1]", $args{Base})) : 0;
 
     my $target = RT::URI->new($self->CurrentUser);
     $target->FromURI( $args{'Target'} )
-        or return (0, $self->loc("Couldn't parse Target URI: [_1]", $args{Target}));
+        or return wantarray ? (0, $self->loc("Couldn't parse Target URI: [_1]", $args{Target})) : 0;
 
     my ( $id, $msg ) = $self->LoadByCols( Base   => $base->URI,
                                           Type   => $args{'Type'},
                                           Target => $target->URI );
 
     unless ($id) {
-        return ( 0, $self->loc("Couldn't load link: [_1]", $msg) );
+        return wantarray ? ( 0, $self->loc("Couldn't load link: [_1]", $msg) ) : 0;
     } else {
-        return ($id, $msg);
+        return wantarray ? ($id, $msg) : $id;
     }
 }
 
@@ -259,14 +259,14 @@ sub Load {
 
 
     if ( $identifier !~ /^\d+$/ ) {
-        return ( 0, $self->loc("That's not a numerical id") );
+        return wantarray ? ( 0, $self->loc("That's not a numerical id") ) : 0;
     }
     else {
         my ( $id, $msg ) = $self->LoadById($identifier);
         unless ( $self->Id ) {
-            return ( 0, $self->loc("Couldn't load link") );
+            return wantarray ? ( 0, $self->loc("Couldn't load link") ) : 0;
         }
-        return ( $id, $msg );
+        return wantarray ? ( $id, $msg ) : $id;
     }
 }
 
diff --git a/lib/RT/Record.pm b/lib/RT/Record.pm
index 49fe7fc..e375160 100644
--- a/lib/RT/Record.pm
+++ b/lib/RT/Record.pm
@@ -393,7 +393,10 @@ sub LoadByCols {
     # We don't want to hang onto this
     $self->ClearAttributes;
 
-    return $self->SUPER::LoadByCols( @_ ) unless $self->_Handle->CaseSensitive;
+    unless ( $self->_Handle->CaseSensitive ) {
+        my ( $ret, $msg ) = $self->SUPER::LoadByCols( @_ );
+        return wantarray ? ( $ret, $msg ) : $ret;
+    }
 
     # If this database is case sensitive we need to uncase objects for
     # explicit loading
@@ -411,7 +414,8 @@ sub LoadByCols {
             $hash{$key}->{function} = $func;
         }
     }
-    return $self->SUPER::LoadByCols( %hash );
+    my ( $ret, $msg ) = $self->SUPER::LoadByCols( %hash );
+    return wantarray ? ( $ret, $msg ) : $ret;
 }
 
 
diff --git a/lib/RT/ScripAction.pm b/lib/RT/ScripAction.pm
index 1e17d5e..41b62b9 100644
--- a/lib/RT/ScripAction.pm
+++ b/lib/RT/ScripAction.pm
@@ -119,7 +119,7 @@ sub Load  {
     my $identifier = shift;
 
     if (!$identifier) {
-        return (0, $self->loc('Input error'));
+        return wantarray ? (0, $self->loc('Input error')) : 0;
     }
 
     my ($ok, $msg);
@@ -138,7 +138,7 @@ sub Load  {
         $self->{'Template'} = shift;
     }
 
-    return ($ok, $msg);
+    return wantarray ? ($ok, $msg) : $ok;
 }
 
 
diff --git a/lib/RT/SharedSetting.pm b/lib/RT/SharedSetting.pm
index 1e704f8..95d7095 100644
--- a/lib/RT/SharedSetting.pm
+++ b/lib/RT/SharedSetting.pm
@@ -110,21 +110,21 @@ sub Load {
             $self->{'Privacy'} = $privacy;
             $self->PostLoad();
 
-            return (0, $self->loc("Permission Denied"))
+            return wantarray ? (0, $self->loc("Permission Denied")) : 0
                 unless $self->CurrentUserCanSee;
 
             my ($ok, $msg) = $self->PostLoadValidate;
-            return ($ok, $msg) if !$ok;
+            return wantarray ? ($ok, $msg) : $ok if !$ok;
 
-            return (1, $self->loc("Loaded [_1] [_2]", $self->ObjectName, $self->Name));
+            return wantarray ? (1, $self->loc("Loaded [_1] [_2]", $self->ObjectName, $self->Name)) : 1;
         } else {
             $RT::Logger->error("Could not load attribute " . $id
                     . " for object " . $privacy);
-            return (0, $self->loc("Failed to load [_1] [_2]", $self->ObjectName, $id))
+            return wantarray ? (0, $self->loc("Failed to load [_1] [_2]", $self->ObjectName, $id)) : 0;
         }
     } else {
         $RT::Logger->warning("Could not load object $privacy when loading " . $self->ObjectName);
-        return (0, $self->loc("Could not load object for [_1]", $privacy));
+        return wantarray ? (0, $self->loc("Could not load object for [_1]", $privacy)) : 0;
     }
 }
 
@@ -144,11 +144,11 @@ sub LoadById {
     my ($ok, $msg) = $attr->LoadById($id);
 
     if (!$ok) {
-        return (0, $self->loc("Failed to load [_1] [_2]: [_3]", $self->ObjectName, $id, $msg))
+        return wantarray ? (0, $self->loc("Failed to load [_1] [_2]: [_3]", $self->ObjectName, $id, $msg)) : 0;
     }
 
     my $privacy = $self->_build_privacy($attr->ObjectType, $attr->ObjectId);
-    return (0, $self->loc("Bad privacy for attribute [_1]", $id))
+    return wantarray ? (0, $self->loc("Bad privacy for attribute [_1]", $id)) : 0
         if !$privacy;
 
     return $self->Load($privacy, $id);
diff --git a/lib/RT/Shredder/Plugin.pm b/lib/RT/Shredder/Plugin.pm
index 60ba333..e247889 100644
--- a/lib/RT/Shredder/Plugin.pm
+++ b/lib/RT/Shredder/Plugin.pm
@@ -161,6 +161,8 @@ Other arguments are sent to the constructor of the plugin
 Returns C<$status> and C<$message>. On errors status
 is C<false> value.
 
+In scalar context, returns $status only.
+
 =cut
 
 sub LoadByName
@@ -172,14 +174,14 @@ sub LoadByName
     local $@;
     my $plugin = "RT::Shredder::Plugin::$name";
     eval "require $plugin" or return( 0, $@ );
-    return( 0, "Plugin '$plugin' has no method new") unless $plugin->can('new');
+    return wantarray ? ( 0, "Plugin '$plugin' has no method new") : 0 unless $plugin->can('new');
 
     my $obj = eval { $plugin->new( @_ ) };
-    return( 0, $@ ) if $@;
-    return( 0, 'constructor returned empty object' ) unless $obj;
+    return wantarray ? ( 0, $@ ) : 0 if $@;
+    return wantarray ? ( 0, 'constructor returned empty object' ) : 0 unless $obj;
 
     $self->Rebless( $obj );
-    return( 1, "successfuly load plugin" );
+    return wantarray ? ( 1, "successfuly load plugin" ) : 1;
 }
 
 =head2 LoadByString
diff --git a/lib/RT/User.pm b/lib/RT/User.pm
index 152981a..94031d1 100644
--- a/lib/RT/User.pm
+++ b/lib/RT/User.pm
@@ -554,8 +554,8 @@ sub LoadOrCreateByEmail {
             }
         }
     }
-    return (0, $message) unless $self->id;
-    return ($self->Id, $message);
+    return wantarray ? (0, $message) : 0 unless $self->id;
+    return wantarray ? ($self->Id, $message) : $self->Id;
 }
 
 =head2 ValidateEmailAddress ADDRESS

-----------------------------------------------------------------------


More information about the Rt-commit mailing list