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 | |
3a2ebbf4 |
40 | =item rank - The sequence number of the reading. This should probably not be set manually. |
d047cd52 |
41 | |
3a2ebbf4 |
42 | =back |
8e1394aa |
43 | |
3a2ebbf4 |
44 | One of 'text', 'is_start', 'is_end', or 'is_lacuna' is required. |
8e1394aa |
45 | |
3a2ebbf4 |
46 | =head2 collation |
94c00c71 |
47 | |
3a2ebbf4 |
48 | =head2 id |
94c00c71 |
49 | |
3a2ebbf4 |
50 | =head2 text |
4cdd82f1 |
51 | |
3a2ebbf4 |
52 | =head2 is_start |
4cdd82f1 |
53 | |
3a2ebbf4 |
54 | =head2 is_end |
4a8828f0 |
55 | |
3a2ebbf4 |
56 | =head2 is_lacuna |
4a8828f0 |
57 | |
3a2ebbf4 |
58 | =head2 rank |
4a8828f0 |
59 | |
3a2ebbf4 |
60 | Accessor methods for the given attributes. |
d047cd52 |
61 | |
3a2ebbf4 |
62 | =cut |
d047cd52 |
63 | |
3a2ebbf4 |
64 | has 'collation' => ( |
65 | is => 'ro', |
66 | isa => 'Text::Tradition::Collation', |
67 | # required => 1, |
68 | weak_ref => 1, |
69 | ); |
d047cd52 |
70 | |
3a2ebbf4 |
71 | has 'id' => ( |
72 | is => 'ro', |
73 | isa => 'Str', |
74 | required => 1, |
75 | ); |
d047cd52 |
76 | |
3a2ebbf4 |
77 | has 'text' => ( |
78 | is => 'ro', |
79 | isa => 'Str', |
80 | required => 1, |
81 | ); |
d047cd52 |
82 | |
3a2ebbf4 |
83 | has 'is_start' => ( |
84 | is => 'ro', |
85 | isa => 'Bool', |
86 | default => undef, |
87 | ); |
88 | |
89 | has 'is_end' => ( |
90 | is => 'ro', |
91 | isa => 'Bool', |
92 | default => undef, |
93 | ); |
94 | |
95 | has 'is_lacuna' => ( |
96 | is => 'ro', |
97 | isa => 'Bool', |
98 | default => undef, |
99 | ); |
100 | |
101 | has 'rank' => ( |
102 | is => 'rw', |
103 | isa => 'Int', |
104 | predicate => 'has_rank', |
105 | ); |
106 | |
107 | |
108 | around BUILDARGS => sub { |
109 | my $orig = shift; |
110 | my $class = shift; |
111 | my $args; |
112 | if( @_ == 1 ) { |
113 | $args = shift; |
114 | } else { |
115 | $args = { @_ }; |
116 | } |
117 | |
118 | # If one of our special booleans is set, we change the text and the |
119 | # ID to match. |
120 | |
1d310495 |
121 | if( exists $args->{'is_lacuna'} && !exists $args->{'text'} ) { |
3a2ebbf4 |
122 | $args->{'text'} = sprintf( "#LACUNA_%s#", $args->{'id'} ); |
123 | } elsif( exists $args->{'is_start'} ) { |
124 | $args->{'id'} = '#START#'; # Change the ID to ensure we have only one |
125 | $args->{'text'} = '#START#'; |
126 | $args->{'rank'} = 0; |
127 | } elsif( exists $args->{'is_end'} ) { |
128 | $args->{'id'} = '#END#'; # Change the ID to ensure we have only one |
129 | $args->{'text'} = '#END#'; |
130 | } |
131 | |
132 | $class->$orig( $args ); |
133 | }; |
134 | |
135 | =head2 is_meta |
136 | |
137 | A meta attribute (ha ha), which should be true if any of our 'special' |
138 | booleans are true. Implies that the reading does not represent a bit |
139 | of text found in a witness. |
140 | |
141 | =cut |
142 | |
143 | sub is_meta { |
144 | my $self = shift; |
145 | return $self->is_start || $self->is_end || $self->is_lacuna; |
146 | } |
147 | |
148 | # Some syntactic sugar |
149 | sub related_readings { |
150 | my $self = shift; |
151 | return $self->collation->related_readings( $self, @_ ); |
152 | } |
153 | |
1d310495 |
154 | sub set_identical { |
155 | my( $self, $other ) = @_; |
156 | return $self->collation->add_relationship( $self, $other, |
157 | { 'type' => 'transposition' } ); |
158 | } |
159 | |
3a2ebbf4 |
160 | sub _stringify { |
161 | my $self = shift; |
162 | return $self->id; |
163 | } |
d047cd52 |
164 | |
021bdbac |
165 | no Moose; |
166 | __PACKAGE__->meta->make_immutable; |
d047cd52 |
167 | |
021bdbac |
168 | 1; |
d047cd52 |
169 | |