[Bps-public-commit] r10887 - in SVN-PropDB: lib/SVN/PropDB t/lib t/lib/TestApp

jesse at bestpractical.com jesse at bestpractical.com
Tue Feb 19 16:57:12 EST 2008


Author: jesse
Date: Tue Feb 19 16:57:11 2008
New Revision: 10887

Added:
   SVN-PropDB/t/lib/
   SVN-PropDB/t/lib/TestApp/
   SVN-PropDB/t/lib/TestApp/Bug.pm
   SVN-PropDB/t/validate.t
Modified:
   SVN-PropDB/lib/SVN/PropDB/Record.pm
   SVN-PropDB/t/create.t
   SVN-PropDB/t/use.t

Log:
* Added validation hooks

Modified: SVN-PropDB/lib/SVN/PropDB/Record.pm
==============================================================================
--- SVN-PropDB/lib/SVN/PropDB/Record.pm	(original)
+++ SVN-PropDB/lib/SVN/PropDB/Record.pm	Tue Feb 19 16:57:11 2008
@@ -21,25 +21,36 @@
     my %args = validate(@_, {  props => 1});
 
     $self->uuid($UUIDGEN->create_str);
+
+
+    $self->_canonicalize_props($args{'props'});
+    $self->_validate_props($args{'props'}) || return undef;
+
     $self->handle->create_node( props => $args{'props'}, uuid => $self->uuid, type => $self->type);
 
     return $self->uuid;
 }
 
 
+
+
+
 sub load {
     my $self = shift;
     my %args = validate(@_, { uuid => 1});
-    #my %props = $self->handle->get_node_props( uuid => $args{uuid});
     $self->uuid($args{uuid});
-    #$self->props(\%props);
 
 }
 
 sub set_prop {
     my $self = shift;
     my %args = validate(@_, { name => 1, value => 1});
-    $self->handle->set_node_props(type => $self->type, uuid => $self->uuid, props =>{$args{name}=> $args{value}});
+
+    my $props = { $args{'name'} => $args{'value'}};
+
+    $self->_canonicalize_props($props);
+    $self->_validate_props($props) || return undef;
+    $self->handle->set_node_props(type => $self->type, uuid => $self->uuid, props => $props );
 }
 
 sub get_props {
@@ -49,8 +60,8 @@
 
 sub prop {
     my $self = shift;
-    my %args = validate(@_, {name => 1});
-    return $self->get_props->{$args{'name'}};
+    my $prop = shift;
+    return $self->get_props->{$prop};
 }
 
 
@@ -66,5 +77,30 @@
 
 }
 
+sub _validate_props {
+    my $self = shift;
+    my $props = shift;
+    my $errors = {};
+    for my $key (keys %$props) {
+        if (my $sub = $self->can('validate_'.$key)) { 
+            $sub->($self, props => $props, errors => $errors) || return undef;
+        }
+    }
+    return 1;
+}
+
+
+sub _canonicalize_props {
+    my $self = shift;
+    my $props = shift;
+    my $errors = {};
+    for my $key (keys %$props) {
+        if (my $sub = $self->can('canonicalize_'.$key)) { 
+            $sub->($self, props => $props, errors => $errors);
+        }
+    }
+    return 1;
+}
+
 
 1;

Modified: SVN-PropDB/t/create.t
==============================================================================
--- SVN-PropDB/t/create.t	(original)
+++ SVN-PropDB/t/create.t	Tue Feb 19 16:57:11 2008
@@ -15,9 +15,9 @@
 isa_ok($record, 'SVN::PropDB::Record');
 my $uuid  = $record->create(props => { name => 'Jesse', age => 31});
 ok($uuid);
-is($record->prop(name => 'age'), 31);
+is($record->prop('age'), 31);
 $record->set_prop( name => 'age', value => 32);
-is($record->prop(name => 'age'), 32);
+is($record->prop('age'), 32);
 
     my $kaia = $record->create(props => { name => 'Kaia', age => 24});
 ok( $kaia);
@@ -28,19 +28,19 @@
 use_ok('SVN::PropDB::Collection');
 
 my $people = SVN::PropDB::Collection->new( handle => $cxn, type => 'Person');
-$people->matching(sub { (shift->prop(name => 'species')||'') ne 'cat'});
+$people->matching(sub { (shift->prop( 'species')||'') ne 'cat'});
 is($#{$people->as_array_ref}, 1);
 my @people= @{$people->as_array_ref};
-is_deeply([ sort map {$_->prop(name => 'name')} @people], [qw(Jesse Kaia)]);
+is_deeply([ sort map {$_->prop('name')} @people], [qw(Jesse Kaia)]);
 
 my $cats = SVN::PropDB::Collection->new( handle => $cxn, type => 'Person');
-$cats->matching(sub { (shift->prop(name => 'species')||'') eq 'cat'});
+$cats->matching(sub { (shift->prop( 'species')||'') eq 'cat'});
 is($#{$cats->as_array_ref}, 1);
 my @cats= @{$cats->as_array_ref};
 for (@cats) {
-    is ($_->prop(name=>'age') , "0.7");
+    is ($_->prop('age') , "0.7");
 }
-is_deeply([ sort map {$_->prop(name => 'name')} @cats], [qw(Mao Mei)]);
+is_deeply([ sort map {$_->prop('name')} @cats], [qw(Mao Mei)]);
 
 my $cat = SVN::PropDB::Record->new(handle => $cxn, type => 'Person');
 $cat->load(uuid => $mao);
@@ -51,7 +51,7 @@
 
 is($#{$cats->as_array_ref}, 1);
 for (@cats) {
-    is ($_->prop(name=>'age') , "0.8");
+    is ($_->prop('age') , "0.8");
 }
 
 for (@cats) {

Added: SVN-PropDB/t/lib/TestApp/Bug.pm
==============================================================================
--- (empty file)
+++ SVN-PropDB/t/lib/TestApp/Bug.pm	Tue Feb 19 16:57:11 2008
@@ -0,0 +1,22 @@
+use warnings;
+use strict;
+
+package TestApp::Bug;
+use base qw/SVN::PropDB::Record/;
+
+
+sub new { shift->SUPER::new( @_, type => 'bug') }
+
+
+sub validate_name { 
+    my $self = shift;
+    my %args = (@_);
+
+    return 1 if ($args{props}->{'name'} eq 'Jesse');
+
+    return 0;
+
+}
+
+
+1;

Modified: SVN-PropDB/t/use.t
==============================================================================
--- SVN-PropDB/t/use.t	(original)
+++ SVN-PropDB/t/use.t	Tue Feb 19 16:57:11 2008
@@ -2,9 +2,10 @@
 use strict;
 use Test::More 'no_plan';
 
-
+use_ok('SVN::PropDB');
 use_ok('SVN::PropDB::Editor');
 use_ok('SVN::PropDB::Handle');
 use_ok('SVN::PropDB::Record');
+use_ok('SVN::PropDB::Collection');
 
 1;

Added: SVN-PropDB/t/validate.t
==============================================================================
--- (empty file)
+++ SVN-PropDB/t/validate.t	Tue Feb 19 16:57:11 2008
@@ -0,0 +1,28 @@
+use warnings;
+use strict;
+use Test::More 'no_plan';
+use File::Temp qw'tempdir';
+use lib 't/lib';
+
+use_ok('SVN::PropDB::Handle');
+my $REPO= tempdir(CLEANUP => 0).'/repo-'.$$;
+ok(! -d $REPO);
+`svnadmin create $REPO`;
+ok(-d $REPO, "The repo exists ater svnadmin create");
+my $cxn = SVN::PropDB::Handle->new( repository => "$REPO", db_root => '/_propdb-test');
+isa_ok($cxn, 'SVN::PropDB::Handle', "Got the cxn");
+use_ok('TestApp::Bug');
+
+my $record = TestApp::Bug->new(handle => $cxn);
+
+
+isa_ok($record, 'TestApp::Bug');
+isa_ok($record, 'SVN::PropDB::Record');
+
+
+
+my $uuid  = $record->create(props => { name => 'Jesse', age => 31});
+ok($uuid);
+
+my $kuuid  = $record->create(props => { name => 'Bob', age => 31});
+ok(!$kuuid);



More information about the Bps-public-commit mailing list