tests passing with new library, yay
[scpubgit/stemmatology.git] / lib / Text / Tradition / Witness.pm
index 4215f87..cb461c5 100644 (file)
@@ -3,29 +3,39 @@ use Moose;
 
 # Sigil. Required identifier for a witness.
 has 'sigil' => (
-               is => 'ro',
-               isa => 'Str',
-               );
+    is => 'ro',
+    isa => 'Str',
+    required => 1,
+    );
 
-# Text.  This might be an array of strings, but it might also be an
-# array of graph nodes.
+# Text.  This is an array of strings (i.e. word tokens).
+# TODO Think about how to handle this for the case of pre-prepared
+# collations, where the tokens are in the graph already.
 has 'text' => (
-              is => 'rw',
-              isa => 'Array',
-              );
+    is => 'rw',
+    isa => 'ArrayRef[Str]',
+    predicate => 'has_text',
+    );
 
-# File.  This is where we read in the witness, if not from a
-# pre-prepared collation.
-has 'file' => (
-              is => 'ro',
-              isa => 'Str',
-              );
+# Source.  This is where we read in the witness, if not from a
+# pre-prepared collation.  It is probably a filename.
+has 'source' => (
+    is => 'ro',
+    isa => 'Str',
+    predicate => 'has_source',
+    );
+
+has 'path' => (
+    is => 'rw',
+    isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
+    predicate => 'has_path',
+    );        
 
 sub BUILD {
     my $self = shift;
-    if( $self->has_file ) {
+    if( $self->has_source ) {
        # Read the file and initialize the text.
-       open( WITNESS, $self->file ) or die "Could not open " 
+       open( WITNESS, $self->source ) or die "Could not open " 
            . $self->file . "for reading";
        # TODO support TEI as well as plaintext, sometime
        my @words;
@@ -34,9 +44,23 @@ sub BUILD {
            push( @words, split( /\s+/, $_ ) );
        }
        close WITNESS;
-       $self->text( @words );
+       $self->text( \@words );
     }
 }
 
+# If the text is not present, and the path is, and this is a 'get'
+# request, generate text from path.
+around text => sub {
+    my $orig = shift;
+    my $self = shift;
+
+    if( $self->has_path && !$self->has_text && !@_ ) {
+       my @words = map { $_->label } @{$self->path};
+       $self->$orig( \@words );
+    }
+    
+    $self->$orig( @_ );
+};
+
 no Moose;
 __PACKAGE__->meta->make_immutable;