famibeib/Modifier.pm untested

filter: Statementstmt 0.0% 0 / 130 Branchbran 0.0% 0 / 70 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 27 Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 295 CC 36 SCAR 51.5
CC
36
Cov
0%
new
420.0
eq
42.0
cmp
42.0
CRAP 173.2
1 # Copyright (c) 2025-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: module to interact with the famibeib word modifiers
6  
7  
8 package Lingua::famibeib::Modifier;
9  
10 use v5.16;
11 use strict;
12 use warnings;
13  
14 use Carp;
15  
16 our $VERSION = v0.08;
17  
18 use parent qw(Data::Identifier::Interface::Simple Data::Identifier::Interface::Subobjects Data::Identifier::Interface::Known);
19  
20 use constant {
21 _GENERATOR => Data::Identifier->new(uuid => '306baa6e-e672-4327-a6b4-ba1d3de89a1e')->register,
22 };
23  
24 use overload (
25 '""' => sub { $_[0]->as_string },
26 'eq' => sub { $_[0]->eq($_[1]) },
27 'ne' => sub { !$_[0]->eq($_[1]) },
28 'cmp' => sub { $_[0]->cmp($_[1]) },
29 );
30  
31 my %_modifiers = (
32 (map {$_ => {max_repeat => 0, allow_word => undef}} qw(ab eb ib ub af ef ak ek ok uk al il ol am em im)),
33 );
34  
35 $_modifiers{$_}{max_repeat} = 1 foreach qw(eb ib);
36 $_modifiers{ab}{allow_word} = 1;
37  
38 my %_registered_by_string;
39 my %_registered_by_uuid;
40  
41 foreach my $key (keys %_modifiers) {
42 my $d = $_modifiers{$key};
43 __PACKAGE__->new(string => $key)->register;
44 }
45  
46  
47 sub new {
48 my ($pkg, $type, $value, @opts) = @_;
49 my $self = bless {}, $pkg;
50  
51 croak 'Stray options passed' if scalar @opts;
52 croak 'No type given' unless defined $type;
53 croak 'No value given' unless defined $value;
54  
55 if ($type eq 'from') {
56 if (ref $value) {
57 if ($value->isa(__PACKAGE__)) {
58 # TODO: handle this when @opts are non-empty
59 return $value;
60 } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) {
61 my $id = $value->as('Data::Identifier');
62 if (eval {$id->generator->eq(_GENERATOR)} && defined(my $request = $id->request(default => undef, no_defaults => 1))) {
63 $type = 'string';
64 $value = $request;
65 } else {
66 if (defined(my $o = $_registered_by_uuid{$id->uuid})) {
67 if (scalar @opts) {
68 $type = 'string';
69 $value = $o->as_string;
70 $self = $o;
71 } else {
72 return $o;
73 }
74 } else {
75 croak 'Unknown word (did you register it or a dictionary?): '.$value;
76 }
77 }
78 }
79 } else {
80 $type = 'string';
81 }
82 }
83  
84 if ($type eq 'string') {
85 my ($first, $rest);
86 my $d;
87  
88 $value =~ s/^-//;
89 $value = lc($value);
90  
91 if (defined(my $o = $_registered_by_string{$value})) {
92 return $o;
93 }
94  
95 ($first, $rest) = $value =~ /^(..)(.*)$/;
96 $d = $_modifiers{$first};
97  
98 croak 'Invalid modifier: Unknown mora: '.$first unless defined $d;
99  
100 if (defined($rest) && length($rest)) {
101 if ($rest =~ /^[aeiou][bfklmst]/) {
102 my $l = length($rest);
103  
104 croak 'Bad string: '.$value if $l & 1;
105  
106 $l /= 2;
107  
108 croak 'Bad repeat count' if $l > $d->{max_repeat};
109 croak 'Bad repeat pattern' if $rest ne ($first x $l);
110 } elsif ($rest =~ /^(?:[bfklmst][aeiou])+\z/ && $d->{allow_word}) {
111 # no-op
112 } else {
113 croak 'Bad string: '.$value;
114 }
115 }
116  
117 $self->{string} = $value;
118 } else {
119 croak 'Bad type: '.$type;
120 }
121  
122 return $self;
123 }
124  
125  
126 sub as_string {
127 my ($self, @opts) = @_;
128  
129 croak 'Stray options passed' if scalar @opts;
130  
131 return $self->{string};
132 }
133  
134  
135 sub master_mora {
136 my ($self, @opts) = @_;
137  
138 croak 'Stray options passed' if scalar @opts;
139  
140 return substr($self->{string}, 0, 2);
141 }
142  
143  
144 sub eq {
145 my ($self, $other, @opts) = @_;
146  
147 croak 'Stray options passed' if scalar @opts;
148  
149 return 1 if !defined($self) && !defined($other);
150 return undef unless defined($self) && defined($other);
151  
152 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
153 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
154  
155 return $self->as_string eq $other->as_string;
156 }
157  
158  
159 sub cmp {
160 my ($self, $other, @opts) = @_;
161  
162 croak 'Stray options passed' if scalar @opts;
163  
164 return 1 if !defined($self) && !defined($other);
165 return undef unless defined($self) && defined($other);
166  
167 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
168 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
169  
170 return $self->as_string cmp $other->as_string;
171 }
172  
173  
174 #@returns __PACKAGE__
175 sub register {
176 my ($self) = @_;
177  
178 $_registered_by_string{$self->as_string} //= $self;
179 $_registered_by_uuid{$self->as('uuid')} //= $self;
180  
181 $self->as('Data::Identifier')->register;
182  
183 return $self;
184 }
185  
186 # ---- Private helpers ----
187  
188 sub as {
189 my ($self, $as, @opts) = @_;
190 my $id = $self->{id} //= do {
191 require Data::Identifier::Generate;
192 my $str = $self->as_string;
193  
194 Data::Identifier::Generate->generic(
195 request => $str,
196 displayname => $str,
197 tagname => $str,
198 style => 'id-based',
199 namespace => '5c2b24f0-e0d9-4746-bd72-0d07061d0dd7',
200 generator => _GENERATOR,
201 )
202 };
203  
204 return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0;
205  
206 return $id->as($as, @opts);
207 }
208  
209 sub displayname {
210 my ($self, @opts) = @_;
211 return $self->as_string if scalar(@opts) == 0;
212 return $self->as('Data::Identifier')->displayname(@opts);
213 }
214  
215 sub _known_provider {
216 my ($pkg, $class, %opts) = @_;
217 croak 'Unsupported options passed' if scalar(keys %opts);
218 return ([values %_registered_by_string], rawtype => __PACKAGE__) if $class eq ':all' || $class eq 'registered';
219 croak 'Unsupported class';
220 }
221  
222 1;
223  
224 __END__