Commit | Line | Data |
dd3b58b0 |
1 | package Text::Tradition::Witness; |
2 | use Moose; |
3 | |
784877d9 |
4 | # Sigil. Required identifier for a witness. |
dd3b58b0 |
5 | has 'sigil' => ( |
d047cd52 |
6 | is => 'ro', |
7 | isa => 'Str', |
8 | required => 1, |
9 | ); |
dd3b58b0 |
10 | |
d047cd52 |
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. |
dd3b58b0 |
14 | has 'text' => ( |
d047cd52 |
15 | is => 'rw', |
16 | isa => 'ArrayRef[Str]', |
de51424a |
17 | predicate => 'has_text', |
d047cd52 |
18 | ); |
dd3b58b0 |
19 | |
d047cd52 |
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', |
8e1394aa |
25 | predicate => 'has_source', |
d047cd52 |
26 | ); |
784877d9 |
27 | |
4a8828f0 |
28 | has 'path' => ( |
29 | is => 'rw', |
30 | isa => 'ArrayRef[Text::Tradition::Collation::Reading]', |
de51424a |
31 | predicate => 'has_path', |
4a8828f0 |
32 | ); |
33 | |
784877d9 |
34 | sub BUILD { |
35 | my $self = shift; |
d047cd52 |
36 | if( $self->has_source ) { |
784877d9 |
37 | # Read the file and initialize the text. |
d047cd52 |
38 | open( WITNESS, $self->source ) or die "Could not open " |
784877d9 |
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; |
d047cd52 |
47 | $self->text( \@words ); |
784877d9 |
48 | } |
49 | } |
50 | |
de51424a |
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 | |
dd3b58b0 |
65 | no Moose; |
66 | __PACKAGE__->meta->make_immutable; |