famibeib/Prefix.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 54 Conditioncond 0.0% 0 / 42 MC/DCmcdc 0.0% 0 / 26 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 260 CC 28 SCAR 44.1
CC
28
Cov
0%
new
210.0
eq
42.0
cmp
42.0
CRAP 82.0
1 # Copyright (c) 2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: module to interact with famibeib word prefixes
6  
7  
8 package Lingua::famibeib::Prefix;
9  
10 use v5.16;
11 use strict;
12 use warnings;
13  
14 use Carp;
15 use Data::Identifier;
16  
17 our $VERSION = v0.08;
18  
19 use parent qw(Data::Identifier::Interface::Simple Data::Identifier::Interface::Subobjects Data::Identifier::Interface::Known);
20  
21 use constant {
22 _GENERATOR => Data::Identifier->new(uuid => '6574c0af-1389-4db7-96ac-3087b6202bd2')->register,
23 };
24  
25 use overload (
26 '""' => \&as_string,
27 'eq' => sub { $_[0]->eq($_[1]) },
28 'ne' => sub { !$_[0]->eq($_[1]) },
29 'cmp' => sub { $_[0]->cmp($_[1]) },
30 );
31  
32 my %_registered_by_string;
33 my %_registered_by_uuid;
34  
35 # Preregister most common prefixes:
36 __PACKAGE__->new(string => $_)->register foreach qw(ba be fa fe fi fo fu ta to tu);
37  
38  
39 sub new {
40 my ($pkg, $type, $value, @opts) = @_;
41 my $self = bless {}, $pkg;
42  
43 croak 'Stray options passed' if scalar @opts;
44 croak 'No type given' unless defined $type;
45 croak 'No value given' unless defined $value;
46  
47 if ($type eq 'from') {
48 if (ref $value) {
49 if ($value->isa(__PACKAGE__)) {
50 # TODO: handle this when @opts are non-empty
51 return $value;
52 } elsif ($value->isa('Data::Identifier') || $value->isa('Data::Identifier::Interface::Simple') || $value->isa('Data::URIID::Base')) {
53 my $id = $value->as('Data::Identifier');
54 my $generator = eval {$id->generator};
55 my $request;
56  
57 if (defined($generator) && $generator->eq(_GENERATOR) && defined($request = $id->request(default => undef, no_defaults => 1))) {
58 $type = 'string';
59 $value = $request;
60 } else {
61 if (defined(my $o = $_registered_by_uuid{$id->uuid})) {
62 if (scalar @opts) {
63 $type = 'string';
64 $value = $o->as_string;
65 $self = $o;
66 } else {
67 return $o;
68 }
69 } else {
70 croak 'Unknown prefix (did you register it or a dictionary?): '.$value;
71 }
72 }
73 }
74 } else {
75 $type = 'string';
76 }
77 }
78  
79 if ($type eq 'string') {
80 if ($value =~ /[\s\.,\!\?]/) {
81 croak 'Multi prefix string passed. Consider using Lingua::famibeib::Text if this is what you wanted';
82 }
83  
84 $value =~ s/-\z//;
85 $value = lc($value);
86  
87 if ($value =~ /^[bfklmst][aeiou](?:[bfklmst][aeiou])*\z/m) {
88 $self = $_registered_by_string{$value} // $self;
89 $self->{string} = $value;
90 } else {
91 croak 'Bad string: '.$value;
92 }
93 } else {
94 croak 'Bad type: '.$type;
95 }
96  
97  
98 return $self;
99 }
100  
101  
102 sub as_string {
103 my ($self) = @_;
104 return $self->{string};
105 }
106  
107  
108 sub eq {
109 my ($self, $other, @opts) = @_;
110  
111 croak 'Stray options passed' if scalar @opts;
112  
113 return 1 if !defined($self) && !defined($other);
114 return undef unless defined($self) && defined($other);
115  
116 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
117 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
118  
119 return $self->as_string eq $other->as_string;
120 }
121  
122  
123 sub cmp {
124 my ($self, $other, @opts) = @_;
125  
126 croak 'Stray options passed' if scalar @opts;
127  
128 return 1 if !defined($self) && !defined($other);
129 return undef unless defined($self) && defined($other);
130  
131 $self = __PACKAGE__->new(from => $self) unless eval {$self->isa(__PACKAGE__)};
132 $other = __PACKAGE__->new(from => $other) unless eval {$other->isa(__PACKAGE__)};
133  
134 {
135 my $str_self = $self->as_string;
136 my $str_other = $other->as_string;
137  
138 return $str_self cmp $str_other;
139 }
140  
141 croak 'BUG!';
142 }
143  
144  
145 #@returns __PACKAGE__
146 sub register {
147 my ($self) = @_;
148 my Data::Identifier $id = $self->as('Data::Identifier');
149 my $string = $self->as_string;
150  
151 $_registered_by_string{$string} //= $self;
152 $_registered_by_uuid{$id->uuid} //= $self;
153  
154 if (length($string) == 2) {
155 # If it's the root prefix, also register the Identifier.
156 $id->register;
157 }
158  
159 return $self;
160 }
161 # ---- Private helpers ----
162  
163 sub as {
164 my ($self, $as, @opts) = @_;
165  
166 my $id = $self->{id} //= do {
167 require Data::Identifier::Generate;
168 my $str = $self->as_string;
169  
170 Data::Identifier::Generate->generic(
171 request => $str,
172 displayname => $str,
173 tagname => $str,
174 style => 'id-based',
175 namespace => 'e75ce34a-a8a6-4f6c-aef1-6dad279118e0',
176 generator => _GENERATOR,
177 )
178 };
179  
180 return $id if $as eq 'Data::Identifier' && scalar(@opts) == 0;
181  
182 return $id->as($as, @opts);
183 }
184  
185 sub _known_provider {
186 my ($pkg, $class, %opts) = @_;
187 croak 'Unsupported options passed' if scalar(keys %opts);
188 return ([values %_registered_by_string], rawtype => __PACKAGE__) if $class eq ':all' || $class eq 'registered';
189 croak 'Unsupported class';
190 }
191  
192 1;
193  
194 __END__