80f737c417777adc592d0ef6f20259a1dbc03d8b
[scpubgit/stemmatology.git] / lib / Text / Tradition / Collation / Reading.pm
1 package Text::Tradition::Collation::Reading;
2
3 use Moose;
4 use overload '""' => \&_stringify, 'fallback' => 1;
5
6 =head1 NAME
7
8 Text::Tradition::Collation::Reading - represents a reading (usually a word) in a collation.
9     
10 =head1 DESCRIPTION
11
12 Text::Tradition is a library for representation and analysis of collated
13 texts, particularly medieval ones.  A 'reading' refers to a unit of text,
14 usually a word, that appears in one or more witnesses (manuscripts) of the
15 tradition; the text of a given witness is composed of a set of readings in
16 a particular sequence
17
18 =head1 METHODS
19
20 =head2 new
21
22 Creates a new reading in the given collation with the given attributes. 
23 Options include:
24
25 =over 4
26
27 =item collation - The Text::Tradition::Collation object to which this reading belongs.  Required.
28
29 =item id - A unique identifier for this reading. Required.
30
31 =item text - The word or other text of the reading.
32
33 =item is_start - The reading is the starting point for the collation.
34
35 =item is_end - The reading is the ending point for the collation.
36
37 =item is_lacuna - The 'reading' represents a known gap in the text.
38
39 =item is_ph - A temporary placeholder for apparatus parsing purposes.  Do not use unless you know what you are doing.
40
41 =item rank - The sequence number of the reading. This should probably not be set manually.
42
43 =back
44
45 One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required.
46
47 =head2 collation
48
49 =head2 id
50
51 =head2 text
52
53 =head2 is_start
54
55 =head2 is_end
56
57 =head2 is_lacuna
58
59 =head2 rank
60
61 Accessor methods for the given attributes.
62
63 =cut
64
65 has 'collation' => (
66         is => 'ro',
67         isa => 'Text::Tradition::Collation',
68         # required => 1,
69         weak_ref => 1,
70         );
71
72 has 'id' => (
73         is => 'ro',
74         isa => 'Str',
75         required => 1,
76         );
77
78 has 'text' => (
79         is => 'ro',
80         isa => 'Str',
81         required => 1,
82         writer => 'alter_text',
83         );
84         
85 has 'is_start' => (
86         is => 'ro',
87         isa => 'Bool',
88         default => undef,
89         );
90
91 has 'is_end' => (
92         is => 'ro',
93         isa => 'Bool',
94         default => undef,
95         );
96     
97 has 'is_lacuna' => (
98     is => 'ro',
99     isa => 'Bool',
100         default => undef,
101     );
102     
103 has 'is_ph' => (
104         is => 'ro',
105         isa => 'Bool',
106         default => undef,
107         );
108         
109 has 'is_common' => (
110         is => 'rw',
111         isa => 'Bool',
112         default => undef,
113         );
114
115 has 'rank' => (
116     is => 'rw',
117     isa => 'Int',
118     predicate => 'has_rank',
119     );
120
121
122 around BUILDARGS => sub {
123         my $orig = shift;
124         my $class = shift;
125         my $args;
126         if( @_ == 1 ) {
127                 $args = shift;
128         } else {
129                 $args = { @_ };
130         }
131                         
132         # If one of our special booleans is set, we change the text and the
133         # ID to match.
134         if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) {
135                 $args->{'text'} = '#LACUNA#';
136         } elsif( exists $args->{'is_start'} ) {
137                 $args->{'id'} = '#START#';  # Change the ID to ensure we have only one
138                 $args->{'text'} = '#START#';
139                 $args->{'rank'} = 0;
140         } elsif( exists $args->{'is_end'} ) {
141                 $args->{'id'} = '#END#';        # Change the ID to ensure we have only one
142                 $args->{'text'} = '#END#';
143         } elsif( exists $args->{'is_ph'} ) {
144                 $args->{'text'} = $args->{'id'};
145         }
146         
147         $class->$orig( $args );
148 };
149
150 =head2 is_meta
151
152 A meta attribute (ha ha), which should be true if any of our 'special'
153 booleans are true.  Implies that the reading does not represent a bit 
154 of text found in a witness.
155
156 =cut
157
158 sub is_meta {
159         my $self = shift;
160         return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph;    
161 }
162
163 =head1 Convenience methods
164
165 =head2 related_readings
166
167 Calls Collation's related_readings with $self as the first argument.
168
169 =cut
170
171 sub related_readings {
172         my $self = shift;
173         return $self->collation->related_readings( $self, @_ );
174 }
175
176 =head2 predecessors
177
178 Returns a list of Reading objects that immediately precede $self in the collation.
179
180 =cut
181
182 sub predecessors {
183         my $self = shift;
184         my @pred = $self->collation->sequence->predecessors( $self->id );
185         return map { $self->collation->reading( $_ ) } @pred;
186 }
187
188 =head2 successors
189
190 Returns a list of Reading objects that immediately follow $self in the collation.
191
192 =cut
193
194 sub successors {
195         my $self = shift;
196         my @succ = $self->collation->sequence->successors( $self->id );
197         return map { $self->collation->reading( $_ ) } @succ;
198 }
199
200 =head2 set_identical( $other_reading)
201
202 Backwards compatibility method, to add a transposition relationship
203 between $self and $other_reading.  Don't use this.
204
205 =cut
206
207 sub set_identical {
208         my( $self, $other ) = @_;
209         return $self->collation->add_relationship( $self, $other, 
210                 { 'type' => 'transposition' } );
211 }
212
213 sub _stringify {
214         my $self = shift;
215         return $self->id;
216 }
217
218 no Moose;
219 __PACKAGE__->meta->make_immutable;
220
221 1;
222