Commit | Line | Data |
784877d9 |
1 | package Text::Tradition::Collation::Reading; |
2 | |
8e1394aa |
3 | use Moose; |
e4b0f464 |
4 | use overload '""' => \&_stringify, 'fallback' => 1; |
3a2ebbf4 |
5 | use Text::Tradition::Collation; |
784877d9 |
6 | |
3a2ebbf4 |
7 | =head1 NAME |
784877d9 |
8 | |
3a2ebbf4 |
9 | Text::Tradition::Collation::Reading - represents a reading (usually a word) in a collation. |
eca16057 |
10 | |
3a2ebbf4 |
11 | =head1 DESCRIPTION |
784877d9 |
12 | |
3a2ebbf4 |
13 | Text::Tradition is a library for representation and analysis of collated |
14 | texts, particularly medieval ones. A 'reading' refers to a unit of text, |
15 | usually a word, that appears in one or more witnesses (manuscripts) of the |
16 | tradition; the text of a given witness is composed of a set of readings in |
17 | a particular sequence |
784877d9 |
18 | |
3a2ebbf4 |
19 | =head1 METHODS |
1ca1163d |
20 | |
3a2ebbf4 |
21 | =head2 new |
8e1394aa |
22 | |
3a2ebbf4 |
23 | Creates a new reading in the given collation with the given attributes. |
24 | Options include: |
94c00c71 |
25 | |
3a2ebbf4 |
26 | =over 4 |
784877d9 |
27 | |
3a2ebbf4 |
28 | =item collation - The Text::Tradition::Collation object to which this reading belongs. Required. |
e2902068 |
29 | |
3a2ebbf4 |
30 | =item id - A unique identifier for this reading. Required. |
910a0a6d |
31 | |
3a2ebbf4 |
32 | =item text - The word or other text of the reading. |
784877d9 |
33 | |
3a2ebbf4 |
34 | =item is_start - The reading is the starting point for the collation. |
3265b0ce |
35 | |
3a2ebbf4 |
36 | =item is_end - The reading is the ending point for the collation. |
784877d9 |
37 | |
3a2ebbf4 |
38 | =item is_lacuna - The 'reading' represents a known gap in the text. |
de51424a |
39 | |
12720144 |
40 | =item is_ph - A temporary placeholder for apparatus parsing purposes. Do not use unless you know what you are doing. |
41 | |
3a2ebbf4 |
42 | =item rank - The sequence number of the reading. This should probably not be set manually. |
d047cd52 |
43 | |
3a2ebbf4 |
44 | =back |
8e1394aa |
45 | |
3a2ebbf4 |
46 | One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required. |
8e1394aa |
47 | |
3a2ebbf4 |
48 | =head2 collation |
94c00c71 |
49 | |
3a2ebbf4 |
50 | =head2 id |
94c00c71 |
51 | |
3a2ebbf4 |
52 | =head2 text |
4cdd82f1 |
53 | |
3a2ebbf4 |
54 | =head2 is_start |
4cdd82f1 |
55 | |
3a2ebbf4 |
56 | =head2 is_end |
4a8828f0 |
57 | |
3a2ebbf4 |
58 | =head2 is_lacuna |
4a8828f0 |
59 | |
3a2ebbf4 |
60 | =head2 rank |
4a8828f0 |
61 | |
3a2ebbf4 |
62 | Accessor methods for the given attributes. |
d047cd52 |
63 | |
3a2ebbf4 |
64 | =cut |
d047cd52 |
65 | |
3a2ebbf4 |
66 | has 'collation' => ( |
67 | is => 'ro', |
68 | isa => 'Text::Tradition::Collation', |
69 | # required => 1, |
70 | weak_ref => 1, |
71 | ); |
d047cd52 |
72 | |
3a2ebbf4 |
73 | has 'id' => ( |
74 | is => 'ro', |
75 | isa => 'Str', |
76 | required => 1, |
77 | ); |
d047cd52 |
78 | |
3a2ebbf4 |
79 | has 'text' => ( |
80 | is => 'ro', |
81 | isa => 'Str', |
82 | required => 1, |
49d4f2ac |
83 | writer => 'alter_text', |
3a2ebbf4 |
84 | ); |
d047cd52 |
85 | |
3a2ebbf4 |
86 | has 'is_start' => ( |
87 | is => 'ro', |
88 | isa => 'Bool', |
89 | default => undef, |
90 | ); |
91 | |
92 | has 'is_end' => ( |
93 | is => 'ro', |
94 | isa => 'Bool', |
95 | default => undef, |
96 | ); |
97 | |
98 | has 'is_lacuna' => ( |
99 | is => 'ro', |
100 | isa => 'Bool', |
101 | default => undef, |
102 | ); |
12720144 |
103 | |
104 | has 'is_ph' => ( |
105 | is => 'ro', |
106 | isa => 'Bool', |
107 | default => undef, |
108 | ); |
3a2ebbf4 |
109 | |
110 | has 'rank' => ( |
111 | is => 'rw', |
112 | isa => 'Int', |
113 | predicate => 'has_rank', |
114 | ); |
115 | |
116 | |
117 | around BUILDARGS => sub { |
118 | my $orig = shift; |
119 | my $class = shift; |
120 | my $args; |
121 | if( @_ == 1 ) { |
122 | $args = shift; |
123 | } else { |
124 | $args = { @_ }; |
125 | } |
126 | |
127 | # If one of our special booleans is set, we change the text and the |
128 | # ID to match. |
129 | |
1d310495 |
130 | if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) { |
3a2ebbf4 |
131 | $args->{'text'} = sprintf( "#LACUNA_%s#", $args->{'id'} ); |
132 | } elsif( exists $args->{'is_start'} ) { |
133 | $args->{'id'} = '#START#'; # Change the ID to ensure we have only one |
134 | $args->{'text'} = '#START#'; |
135 | $args->{'rank'} = 0; |
136 | } elsif( exists $args->{'is_end'} ) { |
137 | $args->{'id'} = '#END#'; # Change the ID to ensure we have only one |
138 | $args->{'text'} = '#END#'; |
12720144 |
139 | } elsif( exists $args->{'is_ph'} ) { |
140 | $args->{'text'} = $args->{'id'}; |
3a2ebbf4 |
141 | } |
142 | |
143 | $class->$orig( $args ); |
144 | }; |
145 | |
146 | =head2 is_meta |
147 | |
148 | A meta attribute (ha ha), which should be true if any of our 'special' |
149 | booleans are true. Implies that the reading does not represent a bit |
150 | of text found in a witness. |
151 | |
152 | =cut |
153 | |
154 | sub is_meta { |
155 | my $self = shift; |
12720144 |
156 | return $self->is_start || $self->is_end || $self->is_lacuna || $self->is_ph; |
3a2ebbf4 |
157 | } |
158 | |
159 | # Some syntactic sugar |
160 | sub related_readings { |
161 | my $self = shift; |
162 | return $self->collation->related_readings( $self, @_ ); |
163 | } |
164 | |
1d310495 |
165 | sub set_identical { |
166 | my( $self, $other ) = @_; |
167 | return $self->collation->add_relationship( $self, $other, |
168 | { 'type' => 'transposition' } ); |
169 | } |
170 | |
3a2ebbf4 |
171 | sub _stringify { |
172 | my $self = shift; |
173 | return $self->id; |
174 | } |
d047cd52 |
175 | |
021bdbac |
176 | no Moose; |
177 | __PACKAGE__->meta->make_immutable; |
d047cd52 |
178 | |
021bdbac |
179 | 1; |
d047cd52 |
180 | |