split out persistence / DB functionality
[scpubgit/stemmatology.git] / morphology / lib / Text / Tradition / Language.pm
1 package Text::Tradition::Language;
2
3 use strict;
4 use warnings;
5 use Module::Load;
6 use Moose::Role;
7
8 requires 'throw';
9
10 =head1 NAME
11
12 Text::Tradition::Language - add-on role to enable language awareness and 
13 morphology functions to a Text::Tradition object.  See also 
14 L<Text::Tradition::Morphology> for individual reading morphologies.
15
16 =head1 METHODS
17
18 =head2 language
19
20 Accessor for the primary language of the tradition. Must correspond to one
21 of the Text::Tradition::Language::* modules in this package.
22
23 =cut
24
25 has 'language' => (
26         is => 'rw',
27         isa => 'Str',
28         predicate => 'has_language',
29         );
30         
31 before 'language' => sub {
32         my $self = shift;
33         if( @_ && $_[0] ne 'Default' ) {
34                 # We are trying to set the language; check that the corresponding
35                 # module exists.
36                 eval "require Text::Tradition::Language::".$_[0];
37                 if( $@ ) {
38                         throw( "Cannot load language module for @_: $@" );
39                 }
40         }
41 };
42     
43 =head2 lemmatize
44
45 Calls the appropriate lemmatization function for the language of the
46 tradition.
47
48 =cut
49
50 sub lemmatize {
51         my $self = shift;
52         unless( $self->has_language ) {
53                 throw( "Please set a language to lemmatize a tradition" );
54         }
55         my $mod = "Text::Tradition::Language::" . $self->language;
56         load( $mod );
57         $mod->can( 'lemmatize' )->( $self );
58 }
59
60 1;
61
62 =head1 LICENSE
63
64 This package is free software and is provided "as is" without express
65 or implied warranty.  You can redistribute it and/or modify it under
66 the same terms as Perl itself.
67
68 =head1 AUTHOR
69
70 Tara L Andrews E<lt>aurum@cpan.orgE<gt>