From: Tara L Andrews Date: Fri, 16 Mar 2012 08:05:25 +0000 (+0100) Subject: allow lookup by name as well as ID X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=ad39942ec31a2cc76e119652c03f6c8ab8b51cc9;p=scpubgit%2Fstemmatology.git allow lookup by name as well as ID --- diff --git a/lib/Text/Tradition/Directory.pm b/lib/Text/Tradition/Directory.pm index 2fa8fc5..7bfba71 100644 --- a/lib/Text/Tradition/Directory.pm +++ b/lib/Text/Tradition/Directory.pm @@ -131,6 +131,7 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); like( $e->message, qr/Cannot directly delete non-Tradition object/, "Exception has correct message" ); } + $f->delete( $uuid ); ok( !$f->exists( $uuid ), "Object is deleted from DB" ); ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ); @@ -141,6 +142,10 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); my $g = Text::Tradition::Directory->new( 'dsn' => $dsn ); my $scope = $g->new_scope; is( scalar $g->traditionlist, 1, "Now one object in new directory index" ); + my $ntobj = $g->tradition( 'CX' ); + my @w1 = sort $ntobj->witnesses; + my @w2 = sort( $nt->witnesses ); + is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" ); } =end testing @@ -221,7 +226,16 @@ sub save { sub tradition { my( $self, $id ) = @_; my $obj = $self->lookup( $id ); - unless( ref( $obj ) eq 'Text::Tradition' ) { + unless( $obj ) { + # Try looking up by name. + foreach my $item ( $self->traditionlist ) { + if( $item->{'name'} eq $id ) { + $obj = $self->lookup( $item->{'id'} ); + last; + } + } + } + if( $obj && ref( $obj ) ne 'Text::Tradition' ) { throw( "Retrieved object is a " . ref( $obj ) . ", not a Text::Tradition" ); } return $obj; diff --git a/t/text_tradition_directory.t b/t/text_tradition_directory.t index 5ca1a3e..de455ea 100644 --- a/t/text_tradition_directory.t +++ b/t/text_tradition_directory.t @@ -78,6 +78,7 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); like( $e->message, qr/Cannot directly delete non-Tradition object/, "Exception has correct message" ); } + $f->delete( $uuid ); ok( !$f->exists( $uuid ), "Object is deleted from DB" ); ok( !$f->exists( $sid ), "Object stemma also deleted from DB" ); @@ -88,6 +89,10 @@ is( ref( $nt ), 'Text::Tradition', "Made new tradition" ); my $g = Text::Tradition::Directory->new( 'dsn' => $dsn ); my $scope = $g->new_scope; is( scalar $g->traditionlist, 1, "Now one object in new directory index" ); + my $ntobj = $g->tradition( 'CX' ); + my @w1 = sort $ntobj->witnesses; + my @w2 = sort( $nt->witnesses ); + is_deeply( \@w1, \@w2, "Looked up remaining tradition by name" ); } }