From 845aacfb3652a18ef12828628e68180591f6baf6 Mon Sep 17 00:00:00 2001 From: "mkanat%bugzilla.org" <> Date: Fri, 24 Aug 2007 02:31:19 +0000 Subject: Bug 373440: Make "check" into a generic function in Bugzilla::Object Patch By Max Kanat-Alexander r=LpSolit, a=LpSolit --- Bugzilla/Object.pm | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 49 insertions(+), 3 deletions(-) (limited to 'Bugzilla/Object.pm') diff --git a/Bugzilla/Object.pm b/Bugzilla/Object.pm index 4d54b04e1..9afad7633 100644 --- a/Bugzilla/Object.pm +++ b/Bugzilla/Object.pm @@ -104,6 +104,24 @@ sub _init { return $object; } +sub check { + my ($invocant, $param) = @_; + my $class = ref($invocant) || $invocant; + # If we were just passed a name, then just use the name. + if (!ref $param) { + $param = { name => $param }; + } + # Don't allow empty names. + if (exists $param->{name}) { + $param->{name} = trim($param->{name}); + $param->{name} || ThrowUserError('object_name_not_specified', + { class => $class }); + } + my $obj = $class->new($param) + || ThrowUserError('object_does_not_exist', {%$param, class => $class}); + return $obj; +} + sub new_from_list { my $invocant = shift; my $class = ref($invocant) || $invocant; @@ -463,7 +481,7 @@ during these comparisons. =over -=item C +=item C =over @@ -478,7 +496,7 @@ If you pass an integer, the integer is the id of the object, from the database, that we want to read in. (id is defined as the value in the L column). -If you pass in a hash, you can pass a C key. The +If you pass in a hashref, you can pass a C key. The value of the C key is the case-insensitive name of the object (from L) in the DB. @@ -503,7 +521,35 @@ are intended B for use by subclasses. =item B -A fully-initialized object. +A fully-initialized object, or C if there is no object in the +database matching the parameters you passed in. + +=back + +=item C + +=over + +=item B + +Checks if there is an object in the database with the specified name, and +throws an error if you specified an empty name, or if there is no object +in the database with that name. + +=item B + +The parameters are the same as for L, except that if you don't pass +a hashref, the single argument is the I of the object, not the id. + +=item B + +A fully initialized object, guaranteed. + +=item B + +If you implement this in your subclass, make sure that you also update +the C block at the bottom of the F +template. =back -- cgit v1.2.3-24-g4f1b