[Jifty-commit] r2562 - in Jifty-DBI/trunk: . lib/Jifty/DBI lib/Jifty/DBI/Record

jifty-commit at lists.jifty.org jifty-commit at lists.jifty.org
Thu Jan 25 22:41:12 EST 2007


Author: jesse
Date: Thu Jan 25 22:41:11 2007
New Revision: 2562

Modified:
   Jifty-DBI/trunk/   (props changed)
   Jifty-DBI/trunk/Changes
   Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Record/Cachable.pm
   Jifty-DBI/trunk/lib/Jifty/DBI/Record/Memcached.pm
   Jifty-DBI/trunk/t/01records.t

Log:
 r21322 at hualien:  jesse | 2007-01-26 11:40:49 +0800
 * load* and create are now optionally class methods that instantiate and return new objects on the fly, for greater source purity


Modified: Jifty-DBI/trunk/Changes
==============================================================================
--- Jifty-DBI/trunk/Changes	(original)
+++ Jifty-DBI/trunk/Changes	Thu Jan 25 22:41:11 2007
@@ -1,5 +1,7 @@
 Revision history for Perl extension Jifty::DBI.
 
+- load, load_by_cols, load_from_hash and create are now optionally class methods.
+
 0.30 Wed Jan 17 15:29:44 EST 2007
 
 - update version dependency on DateTime to 0.34

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record.pm	Thu Jan 25 22:41:11 2007
@@ -748,6 +748,8 @@
 
 =head2 load
 
+C<load> can be called as a class or object method.
+
 Takes a single argument, $id. Calls load_by_cols to retrieve the row 
 whose primary key is $id.
 
@@ -755,7 +757,6 @@
 
 sub load {
     my $self = shift;
-
     return unless @_ and defined $_[0];
 
     return $self->load_by_cols( id => shift );
@@ -763,6 +764,8 @@
 
 =head2 load_by_cols
 
+C<load_by_cols> can be called as a class or object method.
+
 Takes a hash of columns and values. Loads the first record that matches all
 keys.
 
@@ -774,8 +777,15 @@
 =cut
 
 sub load_by_cols {
-    my $self = shift;
+    my $class    = shift;
     my %hash = (@_);
+    my ($self);
+    if (ref($class)) {
+            ($self,$class) = ($class,undef);
+    } else {
+            $self = $class->new( handle => (delete $hash{'_handle'} || undef));
+    }
+
     my ( @bind, @phrases );
     foreach my $key ( keys %hash ) {
         if ( defined $hash{$key} && $hash{$key} ne '' ) {
@@ -816,7 +826,9 @@
         . $self->table
         . " WHERE "
         . join( ' AND ', @phrases );
-    return ( $self->_load_from_sql( $query_string, @bind ) );
+    if ($class) { $self->_load_from_sql( $query_string, @bind ); return $self}
+    else {return $self->_load_from_sql( $query_string, @bind );}
+
 }
 
 =head2 load_by_primary_keys 
@@ -845,8 +857,16 @@
 =cut
 
 sub load_from_hash {
-    my $self    = shift;
+    my $class    = shift;
     my $hashref = shift;
+    my ($self);
+
+    if (ref($class)) {
+            ($self,$class) = ($class,undef);
+    } else {
+            $self = $class->new( handle => (delete $hashref->{'_handle'} || undef));
+    }
+    
 
     foreach my $f ( keys %$hashref ) {
         $self->{'fetched'}{ lc $f } = 1;
@@ -901,6 +921,8 @@
 
 =head2 create PARAMHASH
 
+C<create> can be called as either a class or object method
+
 This method creates a new record with the values specified in the PARAMHASH.
 
 This method calls two hooks in your subclass:
@@ -926,9 +948,18 @@
 =cut 
 
 sub create {
-    my $self    = shift;
+    my $class    = shift;
     my %attribs = @_;
 
+    my ($self);
+    if (ref($class)) {
+            ($self,$class) = ($class,undef);
+    } else {
+            $self = $class->new( handle => (delete $attribs{'_handle'} || undef));
+    }
+
+
+
     if ( $self->can('before_create') ) {
         my $before_ret = $self->before_create( \%attribs );
         return ($before_ret) unless ($before_ret);
@@ -979,7 +1010,13 @@
 
     my $ret = $self->_handle->insert( $self->table, %attribs );
     $self->after_create( \$ret ) if $self->can('after_create');
-    return ($ret);
+    if ($class) {
+        $self->load_by_cols(id => $ret);
+        return ($self);
+    }
+    else {
+     return ($ret);
+    }
 }
 
 =head2 delete

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record/Cachable.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record/Cachable.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record/Cachable.pm	Thu Jan 25 22:41:11 2007
@@ -87,26 +87,43 @@
 sub load_from_hash {
     my $self = shift;
 
-    # Blow away the primary cache key since we're loading.
-    $self->{'_jifty_cache_pkey'} = undef;
-    my ( $rvalue, $msg ) = $self->SUPER::load_from_hash(@_);
+    my ( $rvalue, $msg );
+    if ( ref($self) ) {
 
-    ## Check the return value, if its good, cache it!
-    if ($rvalue) {
-        $self->_store();
+        # Blow away the primary cache key since we're loading.
+        $self->{'_jifty_cache_pkey'} = undef;
+        ( $rvalue, $msg ) = $self->SUPER::load_from_hash(@_);
+
+        ## Check the return value, if its good, cache it!
+        $self->_store() if ($rvalue);
+        return ( $rvalue, $msg );
+    } else {    # Called as a class method;
+        $self = $self->SUPER::load_from_hash(@_);
+        ## Check the return value, if its good, cache it!
+        $self->_store() if ( $self->id );
+        return ($self);
     }
 
-    return ( $rvalue, $msg );
 }
 
 sub load_by_cols {
-    my ( $self, %attr ) = @_;
-    ## Generate the cache key
+    my ( $class, %attr ) = @_;
 
-    my $alt_key =$self->_gen_record_cache_key(%attr);
-    if ($self->_fetch($alt_key)) {
-        return ( 1, "Fetched from cache" );
+    my ($self);
+    if ( ref($class) ) {
+        ( $self, $class ) = ( $class, undef );
+    } else {
+        $self = $class->new(
+            handle => ( delete $attr{'_handle'} || undef ) );
     }
+
+    ## Generate the cache key
+    my $alt_key = $self->_gen_record_cache_key(%attr);
+    if ( $self->_fetch($alt_key) ) {
+        if ($class) { return $self }
+        else { return ( 1, "Fetched from cache" ) }
+    }
+
     # Blow away the primary cache key since we're loading.
     $self->{'_jifty_cache_pkey'} = undef;
 
@@ -116,11 +133,14 @@
     if ($rvalue) {
         ## Only cache the object if its okay to do so.
         $self->_store();
-        $self->_key_cache->set( $alt_key => $self->_primary_record_cache_key );
+        $self->_key_cache->set(
+            $alt_key => $self->_primary_record_cache_key );
 
     }
-    return ( $rvalue, $msg );
-
+    if ($class) { return $self }
+    else {
+        return ( $rvalue, $msg );
+    }
 }
 
 # Function: __set

Modified: Jifty-DBI/trunk/lib/Jifty/DBI/Record/Memcached.pm
==============================================================================
--- Jifty-DBI/trunk/lib/Jifty/DBI/Record/Memcached.pm	(original)
+++ Jifty-DBI/trunk/lib/Jifty/DBI/Record/Memcached.pm	Thu Jan 25 22:41:11 2007
@@ -53,33 +53,51 @@
     my $self = shift;
 
     # Blow away the primary cache key since we're loading.
-    my ( $rvalue, $msg ) = $self->SUPER::load_from_hash(@_);
+    if ( ref($self) ) {
+        my ( $rvalue, $msg ) = $self->SUPER::load_from_hash(@_);
+        ## Check the return value, if its good, cache it!
+        $self->_store() if ($rvalue);
+        return ( $rvalue, $msg );
+    } else {
+        $self = $self->SUPER::load_from_hash(@_);
+        ## Check the return value, if its good, cache it!
+        $self->_store() if ( $self->id );
+        return $self;
 
-    ## Check the return value, if its good, cache it!
-    $self->_store() if ($rvalue);
-
-    return ( $rvalue, $msg );
+    }
 }
 
 sub load_by_cols {
-    my ( $self, %attr ) = @_;
+    my ( $class, %attr ) = @_;
+
+    my ($self);
+    if ( ref($class) ) {
+        ( $self, $class ) = ( $class, undef );
+    } else {
+        $self = $self->new( handle => ( delete $attr{'_handle'} || undef ) );
+    }
 
     ## Generate the cache key
     my $key = $self->_gen_load_by_cols_key(%attr);
-        return ( 1, "Fetched from cache" ) if ( $self->_get($key)  );
-
+    if ( $self->_get($key) ) {
+        if ($class) { return $self }
+        else { return ( 1, "Fetched from cache" ) }
+    }
     ## Fetch from the DB!
     my ( $rvalue, $msg ) = $self->SUPER::load_by_cols(%attr);
     ## Check the return value, if its good, cache it!
     if ($rvalue) {
         $self->_store();
-        if ($key ne $self->_primary_key) {
-            $MEMCACHED->add( $key, $self->_primary_cache_key, $self->_cache_config->{'cache_for_sec'} );
+        if ( $key ne $self->_primary_key ) {
+            $MEMCACHED->add( $key, $self->_primary_cache_key,
+                $self->_cache_config->{'cache_for_sec'} );
             $self->{'loaded_by_cols'} = $key;
         }
     }
-    return ( $rvalue, $msg );
-
+    if ($class) { return $self }
+    else {
+        return ( $rvalue, $msg );
+    }
 }
 
 # Function: __set

Modified: Jifty-DBI/trunk/t/01records.t
==============================================================================
--- Jifty-DBI/trunk/t/01records.t	(original)
+++ Jifty-DBI/trunk/t/01records.t	Thu Jan 25 22:41:11 2007
@@ -8,7 +8,7 @@
 BEGIN { require "t/utils.pl" }
 our (@available_drivers);
 
-use constant TESTS_PER_DRIVER => 64;
+use constant TESTS_PER_DRIVER => 70;
 
 my $total = scalar(@available_drivers) * TESTS_PER_DRIVER;
 plan tests => $total;
@@ -44,11 +44,27 @@
 
         can_ok($rec,'create');
 
+        # Test create and load as class methods
+    
+        my $record2 = TestApp::Address->create( _handle => $handle, name => 'Enoch', phone => '123 456 7890');
+        isa_ok($record2, 'TestApp::Address');
+        ok($record2->id, "Created a record with a class method");
+
+        my $clone2 = TestApp::Address->load_by_cols( _handle => $handle, name => 'Enoch');
+        isa_ok($clone2, 'TestApp::Address');
+        is($clone2->phone, '123 456 7890');
+
+        { 
+            local *TestApp::Address::_handle = sub { return $handle};
+        my $clone_by_id = TestApp::Address->load($record2->id);
+        isa_ok($clone_by_id, 'TestApp::Address');
+        is($clone_by_id->phone, '123 456 7890');
+        }
+
         my ($id) = $rec->create( name => 'Jesse', phone => '617 124 567');
         ok($id,"Created record ". $id);
         ok($rec->load($id), "Loaded the record");
 
-
         is($rec->id, $id, "The record has its id");
         is ($rec->name, 'Jesse', "The record's name is Jesse");
 


More information about the Jifty-commit mailing list