tests passing with new library, yay
[scpubgit/stemmatology.git] / lib / Text / Tradition / Witness.pm
1 package Text::Tradition::Witness;
2 use Moose;
3
4 # Sigil. Required identifier for a witness.
5 has 'sigil' => (
6     is => 'ro',
7     isa => 'Str',
8     required => 1,
9     );
10
11 # Text.  This is an array of strings (i.e. word tokens).
12 # TODO Think about how to handle this for the case of pre-prepared
13 # collations, where the tokens are in the graph already.
14 has 'text' => (
15     is => 'rw',
16     isa => 'ArrayRef[Str]',
17     predicate => 'has_text',
18     );
19
20 # Source.  This is where we read in the witness, if not from a
21 # pre-prepared collation.  It is probably a filename.
22 has 'source' => (
23     is => 'ro',
24     isa => 'Str',
25     predicate => 'has_source',
26     );
27
28 has 'path' => (
29     is => 'rw',
30     isa => 'ArrayRef[Text::Tradition::Collation::Reading]',
31     predicate => 'has_path',
32     );         
33
34 sub BUILD {
35     my $self = shift;
36     if( $self->has_source ) {
37         # Read the file and initialize the text.
38         open( WITNESS, $self->source ) or die "Could not open " 
39             . $self->file . "for reading";
40         # TODO support TEI as well as plaintext, sometime
41         my @words;
42         while(<WITNESS>) {
43             chomp;
44             push( @words, split( /\s+/, $_ ) );
45         }
46         close WITNESS;
47         $self->text( \@words );
48     }
49 }
50
51 # If the text is not present, and the path is, and this is a 'get'
52 # request, generate text from path.
53 around text => sub {
54     my $orig = shift;
55     my $self = shift;
56
57     if( $self->has_path && !$self->has_text && !@_ ) {
58         my @words = map { $_->label } @{$self->path};
59         $self->$orig( \@words );
60     }
61     
62     $self->$orig( @_ );
63 };
64
65 no Moose;
66 __PACKAGE__->meta->make_immutable;