[Bps-public-commit] r10874 - in SVN-PropDB: lib/SVN/PropDB

jesse at bestpractical.com jesse at bestpractical.com
Mon Feb 18 13:48:33 EST 2008


Author: jesse
Date: Mon Feb 18 13:47:58 2008
New Revision: 10874

Removed:
   SVN-PropDB/lib/SVN/PropDB/.Handle.pm.swp
   SVN-PropDB/lib/SVN/PropDB/.Record.pm.swp
   SVN-PropDB/t/.create.t.swp
   SVN-PropDB/t/basic.t
Modified:
   SVN-PropDB/lib/SVN/PropDB/Collection.pm
   SVN-PropDB/lib/SVN/PropDB/Handle.pm
   SVN-PropDB/lib/SVN/PropDB/Record.pm
   SVN-PropDB/t/create.t

Log:
*  YATTA. CRUD works

Modified: SVN-PropDB/lib/SVN/PropDB/Collection.pm
==============================================================================
--- SVN-PropDB/lib/SVN/PropDB/Collection.pm	(original)
+++ SVN-PropDB/lib/SVN/PropDB/Collection.pm	Mon Feb 18 13:47:58 2008
@@ -4,7 +4,7 @@
 package SVN::PropDB::Collection;
 use Params::Validate;
 use base qw/Class::Accessor/;
-__PACKAGE__->mk_accessors(qw'handle');
+__PACKAGE__->mk_accessors(qw'handle type');
 use SVN::PropDB::Record;
 
 
@@ -12,8 +12,8 @@
     my $class = shift;
     my $self = {};
     bless $self, $class;
-    my %args = validate(@_, { handle => 1});
-    $self->handle($args{'handle'});
+    my %args = validate(@_, { handle => 1, type => 1});
+    $self->$_($args{$_}) for (keys %args);
     return $self;
 }
 
@@ -23,17 +23,14 @@
     my $coderef = shift;
 
     # find all items,
-    my $nodes = $self->handle->current_root->dir_entries('/_propdb','/');
+    my $nodes = $self->handle->current_root->dir_entries($self->handle->db_root.'/'.$self->type.'/');
     # run coderef against each item;
     # if it matches, add it to _items
     foreach my $key (keys %$nodes) {
-        warn "considering $key";    
-        my $record = SVN::PropDB::Record->new(handle => $self->handle);
+        my $record = SVN::PropDB::Record->new(handle => $self->handle, type => $self->type);
         $record->load(uuid => $key);
         if($coderef->($record)) {
             push @{$self->{_items}}, $record;
-           } else {
-            warn "no love!";
         }
     
     }

Modified: SVN-PropDB/lib/SVN/PropDB/Handle.pm
==============================================================================
--- SVN-PropDB/lib/SVN/PropDB/Handle.pm	(original)
+++ SVN-PropDB/lib/SVN/PropDB/Handle.pm	Mon Feb 18 13:47:58 2008
@@ -12,21 +12,20 @@
 use SVN::Repos;
 use SVN::Fs;
 
-__PACKAGE__->mk_accessors(qw(repo_path repo_handle));
+__PACKAGE__->mk_accessors(qw(repo_path repo_handle db_root));
 
 sub new {
     my $class = shift;
     my $self  = {};
     bless $self, $class;
-    my %args = validate( @_, { repository => 1 } );
-
+    my %args = validate( @_, { repository => 1, db_root => 1 } );
+    $self->db_root($args{'db_root'});
     $self->repo_path( $args{'repository'} );
     $self->_connect();
 
     return $self;
 }
 
-our $MYROOT = '/_propdb';
 sub current_root {
     my $self = shift;
      $self->repo_handle->fs->revision_root ($self->repo_handle->fs->youngest_rev);
@@ -37,11 +36,17 @@
 sub _connect {
     my $self  = shift;
     my $repos = SVN::Repos::open( $self->repo_path );
-    warn "opened the repos";
     $self->repo_handle($repos);
-    unless($self->current_root->is_dir($MYROOT)) {
+    $self->_create_nonexistent_dir($self->db_root);
+}
+
+
+sub _create_nonexistent_dir {
+    my $self = shift;
+    my $dir = shift;
+    unless($self->current_root->is_dir($dir) ){
     my $edit = $self->begin_edit;
-    $edit->root->make_dir($MYROOT);
+    $edit->root->make_dir($dir);
     $self->commit_edit($edit);
     }
 
@@ -64,24 +69,27 @@
 
 sub create_node {
     my $self = shift;
-    my %args = validate( @_, { uuid => 1, props => 1 } );
+    my %args = validate( @_, { uuid => 1, props => 1, type => 1 } );
+    $self->_create_nonexistent_dir(join('/',$self->db_root, $args{'type'}));
     my $edit = $self->begin_edit();
-    my $file = $self->file_for(uuid => $args{uuid});
+    
+
+    my $file = $self->file_for(uuid => $args{uuid}, type => $args{'type'});
     $edit->root->make_file( $file );
     {
         my $stream = $edit->root->apply_text($file, undef);
         print $stream Dumper($args{'props'});
         close $stream;
     }
-    $self->_set_node_props(uuid => $args{uuid}, props => $args{props}, edit => $edit);
+    $self->_set_node_props(uuid => $args{uuid}, props => $args{props}, edit => $edit, type => $args{'type'});
     $self->commit_edit($edit);
 
 }
 
 sub _set_node_props {
 my $self = shift;
-    my %args = validate(@_, { uuid => 1, props=> 1, edit => 1});
-    my $file = $self->file_for(uuid => $args{uuid});
+    my %args = validate(@_, { uuid => 1, props=> 1, edit => 1, type => 1});
+    my $file = $self->file_for(uuid => $args{uuid}, type => $args{type});
     foreach my $prop (keys %{$args{'props'}}) {
         $args{edit}->root->change_node_prop($file,$prop, $args{'props'}->{$prop}, undef);
     }
@@ -89,16 +97,20 @@
 
 sub delete_node {
     my $self = shift;
-    my %args = validate( @_, { uuid => 1 } );
+    my %args = validate( @_, { uuid => 1, type => 1} );
+    my $edit = $self->begin_edit();
+    $edit->root->delete($self->file_for(uuid => $args{uuid}, type => $args{type})); 
+    $self->commit_edit($edit);
+
 
 }
 
 sub set_node_props {
     my $self = shift;
-    my %args = validate( @_, { uuid => 1, props => 1 } );
+    my %args = validate( @_, { uuid => 1, props => 1, type => 1 } );
     my $edit = $self->begin_edit();
-    my $file = $self->file_for(uuid => $args{uuid});
-    $self->_set_node_props(uuid => $args{uuid}, props => $args{props} ,edit => $edit);
+    my $file = $self->file_for(uuid => $args{uuid}, type => $args{'type'});
+    $self->_set_node_props(uuid => $args{uuid}, props => $args{props} ,edit => $edit, type => $args{'type'});
     $self->commit_edit($edit);
 
 }
@@ -107,16 +119,16 @@
 
 sub get_node_props {
     my $self = shift;
-    my %args = validate( @_, { uuid => 1,  } );
-    return $self->current_root->node_proplist($self->file_for(uuid => $args{'uuid'}));
+    my %args = validate( @_, { uuid => 1, type => 1 } );
+    return $self->current_root->node_proplist($self->file_for(uuid => $args{'uuid'}, type => $args{'type'}));
 }
 
 
 
 sub file_for {
 my $self = shift;
-    my %args = validate( @_, { uuid => 1,  } );
-    my $file = $MYROOT."/".$args{'uuid'};
+    my %args = validate( @_, { uuid => 1,  type => 1} );
+    my $file =  join("/",$self->db_root, $args{'type'}, $args{'uuid'});
     return $file;
 
 }

Modified: SVN-PropDB/lib/SVN/PropDB/Record.pm
==============================================================================
--- SVN-PropDB/lib/SVN/PropDB/Record.pm	(original)
+++ SVN-PropDB/lib/SVN/PropDB/Record.pm	Mon Feb 18 13:47:58 2008
@@ -4,15 +4,15 @@
 package SVN::PropDB::Record;
 use Params::Validate;
 use base qw'Class::Accessor';
-__PACKAGE__->mk_accessors(qw'handle props uuid');
+__PACKAGE__->mk_accessors(qw'handle props uuid type');
 my $UUIDGEN = Data::UUID->new();
 
 sub new {
     my $class = shift;
     my $self = {};
     bless $self, $class;
-    my %args = validate(@_, { handle => 1});
-    $self->handle($args{'handle'});
+    my %args = validate(@_, { handle => 1, type => 1});
+    $self->$_($args{$_}) for keys(%args);
     return $self;
 }
 
@@ -21,7 +21,7 @@
     my %args = validate(@_, {  props => 1});
 
     $self->uuid($UUIDGEN->create_str);
-    $self->handle->create_node( props => $args{'props'}, uuid => $self->uuid);
+    $self->handle->create_node( props => $args{'props'}, uuid => $self->uuid, type => $self->type);
 
     return $self->uuid;
 }
@@ -39,12 +39,12 @@
 sub set_prop {
     my $self = shift;
     my %args = validate(@_, { name => 1, value => 1});
-    $self->handle->set_node_props(uuid => $self->uuid, props =>{$args{name}=> $args{value}});
+    $self->handle->set_node_props(type => $self->type, uuid => $self->uuid, props =>{$args{name}=> $args{value}});
 }
 
 sub get_props {
     my $self = shift;
-    return $self->handle->get_node_props(uuid => $self->uuid);
+    return $self->handle->get_node_props(uuid => $self->uuid, type => $self->type);
 }
 
 sub prop {
@@ -59,4 +59,12 @@
     my %args = validate(@_, { name => 1});
     $self->handle->delete_node_prop(uuid => $self->uuid, name => $args{'name'});
 }
+
+sub delete {
+    my $self = shift;
+    $self->handle->delete_node(type => $self->type, uuid => $self->uuid);
+
+}
+
+
 1;

Modified: SVN-PropDB/t/create.t
==============================================================================
--- SVN-PropDB/t/create.t	(original)
+++ SVN-PropDB/t/create.t	Mon Feb 18 13:47:58 2008
@@ -8,10 +8,10 @@
 ok(! -d $REPO);
 `svnadmin create $REPO`;
 ok(-d $REPO, "The repo exists ater svnadmin create");
-my $cxn = SVN::PropDB::Handle->new( repository => "$REPO");
+my $cxn = SVN::PropDB::Handle->new( repository => "$REPO", db_root => '/_propdb-test');
 isa_ok($cxn, 'SVN::PropDB::Handle', "Got the cxn");
 use_ok('SVN::PropDB::Record');
-my $record = SVN::PropDB::Record->new(handle =>$cxn);
+my $record = SVN::PropDB::Record->new(handle =>$cxn, type => 'Person');
 isa_ok($record, 'SVN::PropDB::Record');
 my $uuid  = $record->create(props => { name => 'Jesse', age => 31});
 ok($uuid);
@@ -27,13 +27,13 @@
 ok ($mei);
 use_ok('SVN::PropDB::Collection');
 
-my $people = SVN::PropDB::Collection->new( handle => $cxn);
+my $people = SVN::PropDB::Collection->new( handle => $cxn, type => 'Person');
 $people->matching(sub { (shift->prop(name => '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)]);
 
-my $cats = SVN::PropDB::Collection->new( handle => $cxn);
+my $cats = SVN::PropDB::Collection->new( handle => $cxn, type => 'Person');
 $cats->matching(sub { (shift->prop(name => 'species')||'') eq 'cat'});
 is($#{$cats->as_array_ref}, 1);
 my @cats= @{$cats->as_array_ref};
@@ -42,17 +42,29 @@
 }
 is_deeply([ sort map {$_->prop(name => 'name')} @cats], [qw(Mao Mei)]);
 
-my $cat = SVN::PropDB::Record->new(handle => $cxn);
+my $cat = SVN::PropDB::Record->new(handle => $cxn, type => 'Person');
 $cat->load(uuid => $mao);
 $cat->set_prop(name => 'age', value => '0.8');
-my $cat2 = SVN::PropDB::Record->new(handle => $cxn);
+my $cat2 = SVN::PropDB::Record->new(handle => $cxn, type => 'Person');
 $cat2->load(uuid => $mei);
 $cat2->set_prop(name => 'age', value => '0.8');
 
 is($#{$cats->as_array_ref}, 1);
-my @cats= @{$cats->as_array_ref};
 for (@cats) {
     is ($_->prop(name=>'age') , "0.8");
 }
 
+for (@cats) {
+    ok ($_->delete);
+}
+
+
+my $records = SVN::PropDB::Collection->new(type => 'Person', handle => $cxn);
+$records->matching(sub {1});
+is($#{$records->as_array_ref} , 1);
+
+
+
+
+
 1;



More information about the Bps-public-commit mailing list