[Jifty-commit] r2564 - in jifty/trunk: lib/Jifty

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Jan 25 22:55:36 EST 2007


Author: jesse
Date: Thu Jan 25 22:55:36 2007
New Revision: 2564

Modified:
   jifty/trunk/   (props changed)
   jifty/trunk/lib/Jifty/Record.pm

Log:
 r21328 at hualien:  jesse | 2007-01-26 11:55:19 +0800
 * Allow create, load_or_create and load_by_cols to be used as class methods.


Modified: jifty/trunk/lib/Jifty/Record.pm
==============================================================================
--- jifty/trunk/lib/Jifty/Record.pm	(original)
+++ jifty/trunk/lib/Jifty/Record.pm	Thu Jan 25 22:55:36 2007
@@ -32,6 +32,7 @@
 
 =head2 create PARAMHASH
 
+C<create> can be called as either a class method or an object method.
 
 Takes an array of key-value pairs and inserts a new row into the database representing
 this object.
@@ -51,7 +52,14 @@
 =cut 
 
 sub create {
-    my $self    = shift;
+    my $class    = shift;
+    my $self;
+    if (ref($class)) { 
+        ($self,$class) = ($class,undef);
+    } else {
+        $self = $class->new();
+    }
+
     my %attribs = @_;
 
     unless ( $self->check_create_rights(@_) ) {
@@ -73,7 +81,11 @@
         my ( $val, $msg ) = $func->($self, $attr);
         unless ($val) {
             $self->log->error("There was a validation error for $key");
-            return ( $val, $msg );
+            if ($class) {
+                return($self);
+            } else {
+                return ( $val, $msg );
+            }
         }
         }
         # remove blank values. We'd rather have nulls
@@ -90,7 +102,11 @@
     }
     my ($id, $status) = $msg;
     $self->load_by_cols( id => $id ) if ($id);
-    return wantarray ? ($id, $status) : $id;
+    if ($class) {
+        return $self;
+    } else {
+        return wantarray ? ($id, $status) : $id;
+    }
 }
 
 
@@ -107,13 +123,21 @@
 
 =head2 load_or_create
 
-Attempts to load a record with the named parameters passed in.  If it
+C<load_or_create> can be called as either a class method or an object method.
+It attempts to load a record with the named parameters passed in.  If it
 can't do so, it creates a new record.
 
 =cut
 
 sub load_or_create {
-    my $self = shift;
+    my $class = shift;
+    my $self;
+    if (ref($class)) {
+       ($self,$class) = ($class,undef); 
+    } else {
+        $self = $class->new();
+    }
+
     my %args = (@_);
 
     my ( $id, $msg ) = $self->load_by_cols(%args);


More information about the Jifty-commit mailing list