lib/Data/TagDB/Factory.pm

filter: Statementstmt 10.6% 28 / 263 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 85 MC/DCmcdc 0.0% 0 / 48 Subroutinesub 42.8% 9 / 21 Podpod 100% 11 / 11 totaltotal 8.3% 48 / 576 CC 108 SCAR 66.4
CC
108
Cov
6%
create_date
1482.0
generate
1122.0
create_character
182.0
CRAP 765.2
« lib/Data/TagDB/Base.pm ↑ Summary lib/Data/TagDB/Tag.pm »
1 # Copyright (c) 2024-2026 Philipp Schafft
2  
3 # licensed under Artistic License 2.0 (see LICENSE file)
4  
5 # ABSTRACT: Work with Tag databases
6  
7 package Data::TagDB::Factory;
8  
9 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 1 ▶use Data::Identifier::Generate;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use parent 'Data::TagDB::WeakBaseObject';
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 1 ▶use constant NS_DATE => 'fc43fbba-b959-4882-b4c8-90a288b7d416';
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use constant RE_UUID => qr/^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 1 our $VERSION = v0.16;
24  
25  
26  
27 0 ▶sub db {
Subroutine db: not called CC=1 SCAR=6.9
Pod documented
28 0 my ($self) = @_;
29 0 return $self->{db};
30 }
31  
32  
33 0 ▶sub cache {
Subroutine cache: not called CC=1 SCAR=6.9
Pod documented
34 0 my ($self) = @_;
35 0 return $self->{cache};
36 }
37  
38  
39 0 ▶sub wk {
Subroutine wk: not called CC=2 SCAR=17.9
Pod documented
40 0 my ($self) = @_;
41 0 ▶ return $self->{wk} //= $self->db->wk;
Condition0%0/3
$self->{"wk"} //= $self->db->wk
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"wk"} //= $self->db->wk
$self->{"wk"} $self->db->wk
Truth table0%0/3
A //= B
A= $self->{"wk"}B= $self->db->wk
ABresult
1X1
011
000
42 }
43  
44  
45 0 ▶sub create_namespace {
Subroutine create_namespace: not called CC=2 SCAR=17.9
Pod documented
46 0 my ($self, $ns, $tagname) = @_;
47 0 my Data::TagDB::Cache $cache = $self->cache;
48 0 my Data::TagDB $db;
49 my Data::TagDB::WellKnown $wk;
50 0 my Data::TagDB::Tag $tag;
51  
52 0 $tag = $cache->_get_by_key(__PACKAGE__, ns => $ns);
53 0 ▶ return $tag if defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 0 0 0
54  
55 0 $db = $self->db;
56 0 $wk = $self->wk;
57  
58 0 $tag = $db->create_tag([$wk->uuid => $ns], [$wk->tagname(1) => $tagname]);
59 0 $db->create_relation(tag => $tag, relation => $wk->has_type(1), related => $wk->namespace(1));
60  
61 0 $cache->_add_by_key(__PACKAGE__, ns => $ns => $tag);
62  
63 0 return $tag;
64 }
65  
66  
67 0 ▶sub create_generator {
Subroutine create_generator: not called CC=8 SCAR=42.8
Pod documented
68 0 my ($self, %opts) = @_;
69 0 my Data::TagDB::Cache $cache = $self->cache;
70 0 my Data::TagDB $db;
71 my Data::TagDB::WellKnown $wk;
72 0 my Data::TagDB::Tag $tag;
73 0 ▶ my $uuid = $opts{uuid} || croak 'No UUID given for this generator. Currently all generators require an UUID.';
Condition0%0/2
$opts{'uuid'} || croak('No UUID given for this generator. Currently all generators require an UUID.')
l!l
00
MC/DC0%0/1
$opts{'uuid'} || croak('No UUID given for this generator. Currently all generators require an UUID.')
$opts{'uuid'}
Truth table0%0/2
A
A= $opts{'uuid'}
Aresult
11
00
74  
75 0 $tag = $cache->_get_by_key(__PACKAGE__, gen => $uuid);
76 0 ▶ return $tag if defined $tag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag 0 0 0
77  
78 0 $db = $self->db;
79 0 $wk = $self->wk;
80  
81 0 $tag = $db->create_tag([$wk->uuid => $uuid], [$wk->tagname(1) => $opts{tagname}]);
82 0 $db->create_relation(tag => $tag, relation => $wk->has_type(1), related => $wk->generator(1));
83  
84 0 ▶ if (defined $opts{ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ns"}) 0 0 0
85 0 ▶ my Data::TagDB::Tag $ns = eval {$opts{ns}->isa('Data::TagDB::Tag')} ? $opts{ns} : $self->create_namespace($opts{ns});
Branch: 1 branch
BranchTrueFalseTotal
eval { do { $opts{'ns'}->isa('Data::TagDB::Tag') } } ? : 0 0 0
86 0 $db->create_relation(tag => $tag, relation => $wk->using_namespace(1), related => $ns);
87 0 $tag->attribute(using_namespace => set => $ns);
88 }
89  
90 0 ▶ if (defined $opts{for_type}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"for_type"}) 0 0 0
91 0 $db->create_relation(tag => $tag, relation => $wk->for_type(1), related => $opts{for_type});
92 0 $tag->attribute(for_type => set => $opts{for_type});
93 }
94  
95 0 foreach my $key (qw(style copy_names)) {
96 0 ▶ if (defined $opts{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{$key}) 0 0 0
97 0 $tag->attribute('generator_'.$key => set => $opts{$key});
98 }
99 }
100  
101 0 $cache->_add_by_key(__PACKAGE__, gen => $uuid => $tag);
102  
103 0 return $tag;
104 }
105  
106  
107 0 ▶sub create_wikidata {
Subroutine create_wikidata: not called CC=4 SCAR=30.0
Pod documented
108 0 my ($self, $qid) = @_;
109 0 my Data::TagDB $db = $self->db;
110 0 my Data::TagDB::WellKnown $wk = $self->wk;
111 0 my Data::TagDB::Tag $ns = $self->create_namespace('9e10aca7-4a99-43ac-9368-6cbfa43636df' => 'Wikidata-namespace');
112 0 my ($type) = $qid =~ /^([QPL])[1-9][0-9]*$/;
113 0 my %opts;
114  
115 0 ▶ if ($type eq 'Q') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'Q') { } 0 0 0
elsif ($type eq 'P') { } 0 0 0
elsif ($type eq 'L') { } 0 0 0
116 0 $opts{generator} = $self->create_generator(
117 uuid => '710412ba-dafd-4eca-91eb-4501e717af8f',
118 tagname => 'Wikidata-item-generator',
119 ns => $ns,
120 for_type => $db->create_tag([$wk->uuid => 'b1dfb9f7-1c56-4c33-8a0b-a3e9a9c9f707'], [$wk->tagname => 'Wikidata-item']),
121 style => 'id-based',
122 );
123 } elsif ($type eq 'P') {
124 0 $opts{generator} = $self->create_generator(
125 uuid => '8a3c765a-6b86-49ab-a369-2ef6ce761308',
126 tagname => 'Wikidata-property-generator',
127 ns => $ns,
128 for_type => $db->create_tag([$wk->uuid => 'cb05df0a-e949-42ef-9f59-66df784b0410'], [$wk->tagname => 'Wikidata-property']),
129 style => 'id-based',
130 );
131 } elsif ($type eq 'L') {
132 # TODO: Needs generator definition. However basic generation works without.
133 } else {
134 0 croak 'Unsupported qid: '.$qid;
135 }
136  
137 0 return $self->generate(%opts, ns => $ns, style => 'id-based', request => $qid);
138 }
139  
140  
141 0 ▶sub create_integer {
Subroutine create_integer: not called CC=4 SCAR=30.0
Pod documented
142 0 my ($self, $int) = @_;
143 0 my Data::TagDB $db = $self->db;
144 0 my Data::TagDB::WellKnown $wk = $self->wk;
145 0 my Data::TagDB::Tag $ns = $self->create_namespace('5dd8ddbb-13a8-4d6c-9264-36e6dd6f9c99' => 'integer-namespace');
146 0 my %opts;
147  
148 0 ▶ croak 'Invalid integer: '.$int unless $int =~ /^[\+\-]?[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $int =~ /^[\+\-]?[0-9]+$/ 0 0 0
149 0 $int = int($int);
150  
151 0 ▶ if ($int > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($int > 0) { } 0 0 0
152 0 $opts{generator} = $self->create_generator(
153 uuid => '53863a15-68d4-448d-bd69-a9b19289a191',
154 tagname => 'unsigned-integer-generator',
155 ns => $ns,
156 for_type => $db->create_tag([$wk->uuid => 'dea3782c-6bcb-4ce9-8a39-f8dab399d75d'], [$wk->tagname => 'unsigned-integer']),
157 style => 'integer-based',
158 );
159 } else {
160 0 $opts{generator} = $self->create_generator(
161 uuid => 'e8aa9e01-8d37-4b4b-8899-42ca0a2a906f',
162 tagname => 'signed-integer-generator',
163 ns => $ns,
164 for_type => $db->create_tag([$wk->uuid => 'd191954d-b30d-4d3b-94ea-babadb2f2901'], [$wk->tagname => 'signed-integer']),
165 style => 'integer-based',
166 );
167 }
168  
169 {
170 0 my Data::TagDB::Tag $tag = $self->generate(%opts, ns => $ns, style => 'integer-based', request => $int);
171  
172 0 ▶ unless ($int & 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($int & 1) 0 0 0
173 0 $db->create_relation(tag => $tag, relation => $wk->has_prime_factor(1), related => $wk->two(1));
174 }
175  
176 0 return $tag;
177 }
178 }
179  
180  
181 0 ▶sub create_character {
Subroutine create_character: not called CC=13 SCAR=52.0
Pod documented
182 0 my $self;
183 my %data;
184 0 my $unicode_cp;
185 0 my $unicode_cp_str;
186  
187 0 ▶ if (scalar(@_) == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 2) { } 0 0 0
188 0 ($self, $data{unicode}) = @_;
189 } else {
190 0 ($self, %data) = @_;
191 }
192  
193 0 ▶ if (defined $data{unicode}) {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $data{'unicode'}) { } 0 0 0
elsif (defined $data{'ascii'}) { } 0 0 0
elsif (defined $data{'raw'}) { } 0 0 0
194 0 ▶ if ($data{unicode} =~ /^[Uu]\+([0-9a-fA-F]+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data{'unicode'} =~ /^[Uu]\+([0-9a-fA-F]+)$/) { } 0 0 0
195 0 $unicode_cp = hex($1);
196 } else {
197 0 $unicode_cp = int($data{unicode});
198 }
199 } elsif (defined $data{ascii}) {
200 0 $unicode_cp = int($data{ascii});
201 0 ▶ croak 'US-ASCII character out of range: '.$unicode_cp if $unicode_cp > 0x7F;
Branch: 1 branch
BranchTrueFalseTotal
if $unicode_cp > 127 0 0 0
202 } elsif (defined $data{raw}) {
203 0 ▶ croak 'Raw value is not exactly one character long' unless length($data{raw}) == 1;
Branch: 1 branch
BranchTrueFalseTotal
unless length $data{"raw"} == 1 0 0 0
204 0 $unicode_cp = ord($data{raw});
205 }
206  
207 0 ▶ croak 'Unicode character out of range: '.$unicode_cp if $unicode_cp < 0 || $unicode_cp > 0x10FFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $unicode_cp < 0 or $unicode_cp > 1114111 0 0 0
Condition0%0/3
$unicode_cp < 0 or $unicode_cp > 1114111
l!l&&r!l&&!r
000
MC/DC0%0/2
$unicode_cp < 0 or $unicode_cp > 1114111
$unicode_cp < 0 $unicode_cp > 1114111
Truth table0%0/3
A or B
A= $unicode_cp < 0B= $unicode_cp > 1114111
ABresult
1X1
011
000
208  
209 0 $unicode_cp_str = sprintf('U+%04X', $unicode_cp);
210  
211 {
212 0 my Data::TagDB $db = $self->db;
213 0 my Data::TagDB::WellKnown $wk = $self->wk;
214 0 my Data::TagDB::Tag $asi = $wk->also_shares_identifier;
215 0 my Data::TagDB::Tag $ns = $self->create_namespace('132aa723-a373-48bf-a88d-69f1e00f00cf' => 'unicode-character-namespace');
216 0 my Data::TagDB::Tag $tag;
217 my %opts;
218  
219 0 require charnames;
220  
221 0 $opts{generator} = $self->create_generator(
222 uuid => 'd74f8c35-bcb8-465c-9a77-01010e8ed25c',
223 tagname => 'unicode-character-generator',
224 ns => $ns,
225 for_type => $db->create_tag([$wk->uuid => '5ee5f216-5e7a-443b-a234-db5c032d4710'], [$wk->tagname => 'unicode-character']),
226 style => 'id-based',
227 );
228  
229 0 $tag = $self->generate(%opts, ns => $ns, style => 'id-based', request => $unicode_cp_str);
230  
231 0 $db->create_metadata(tag => $tag, relation => $asi, type => $wk->unicode_code_point(1), data_raw => $unicode_cp_str);
232 0 ▶ if ($unicode_cp <= 0x7F) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unicode_cp <= 127) 0 0 0
233 0 $db->create_metadata(tag => $tag, relation => $asi, type => $wk->ascii_code_point(1), data_raw => $unicode_cp);
234 }
235 0 ▶ if ($unicode_cp > 0x20 && $unicode_cp < 0x7F) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unicode_cp > 32 and $unicode_cp < 127) 0 0 0
Condition0%0/3
$unicode_cp > 32 and $unicode_cp < 127
!ll&&!rl&&r
000
MC/DC0%0/2
$unicode_cp > 32 and $unicode_cp < 127
$unicode_cp > 32 $unicode_cp < 127
Truth table0%0/3
A and B
A= $unicode_cp > 32B= $unicode_cp < 127
ABresult
0X0
100
111
236 0 $db->create_metadata(tag => $tag, relation => $asi, type => $wk->tagname(1), data_raw => chr($unicode_cp));
237 }
238 0 $db->create_metadata(tag => $tag, relation => $asi, type => $wk->tagname(1), data_raw => charnames::viacode($unicode_cp));
239  
240 0 return $tag;
241 }
242 }
243  
244  
245 0 ▶sub create_colour {
Subroutine create_colour: not called CC=11 SCAR=48.8
Pod documented
246 0 my ($self, $colour) = @_;
247 0 my Data::TagDB $db = $self->db;
248 0 my Data::TagDB::WellKnown $wk = $self->wk;
249 0 my Data::TagDB::Tag $ns = $self->create_namespace('88d3944f-a13b-4e35-89eb-e3c1fbe53e76');
250 0 my Data::TagDB::Tag $tag;
251 my $rgb;
252 0 my %opts;
253  
254 0 ▶ if (ref $colour) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $colour) 0 0 0
255 # If it's a ref but not blessed we don't care if it fails
256 0 ▶ if ($colour->isa('Data::URIID::Colour')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($colour->isa('Data::URIID::Colour')) { } 0 0 0
elsif ($colour->isa('Color::Library::Color')) { } 0 0 0
elsif ($colour->isa('Graphics::Color::RGB')) { } 0 0 0
257 0 $colour = $colour->rgb;
258 } elsif ($colour->isa('Color::Library::Color')) {
259 0 $colour = $colour->css;
260 } elsif ($colour->isa('Graphics::Color::RGB')) {
261 0 $colour = $colour->as_css_hex;
262 } else {
263 0 croak 'Unsupported type';
264 }
265 }
266  
267 # Check basic format first, then normalise
268 0 ▶ croak 'Colour is in invalid format' unless $colour =~ /^#[0-9a-fA-F]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour =~ /^#[0-9a-fA-F]+$/ 0 0 0
269  
270 # Normalise
271 0 $colour = lc($colour);
272 0 ▶ $colour = "#$1$1$2$2$3$3" if $colour =~ /^#([0-9a-f])([0-9a-f])([0-9a-f])$/;
Branch: 1 branch
BranchTrueFalseTotal
if $colour =~ /^#([0-9a-f])([0-9a-f])([0-9a-f])$/ 0 0 0
273 0 ▶ $rgb = uc($colour) if $colour =~ /^#[0-9a-f]{6}$/;
Branch: 1 branch
BranchTrueFalseTotal
if $colour =~ /^#[0-9a-f]{6}$/ 0 0 0
274 0 ▶ $colour = sprintf('#%s%s%s', $1 x 6, $2 x 6, $3 x 6) if $colour =~ /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/;
Branch: 1 branch
BranchTrueFalseTotal
if $colour =~ /^#([a-f0-9]{2})([a-f0-9]{2})([a-f0-9]{2})$/ 0 0 0
275  
276 # And do a funal check
277 0 ▶ croak 'Invalid colour value' unless $colour =~ /^#[0-9a-f]{36}$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $colour =~ /^#[0-9a-f]{36}$/ 0 0 0
278  
279 0 $opts{generator} = $self->create_generator(
280 uuid => '55febcc4-6655-4397-ae3d-2353b5856b34',
281 tagname => 'rgb-colour-generator',
282 ns => $ns,
283 for_type => $db->create_tag([$wk->uuid => '5f281982-f9b8-4203-8c62-fb951a5989cc'], [$wk->tagname => 'rgb-colour']),
284 style => 'id-based',
285 );
286  
287 0 $tag = $self->generate(%opts, ns => $ns, style => 'id-based', input => $colour);
288  
289 0 ▶ $db->create_metadata(tag => $tag, relation => $wk->has_colour_value(1), data_raw => $rgb) if defined $rgb;
Branch: 1 branch
BranchTrueFalseTotal
if defined $rgb 0 0 0
290  
291 0 return $tag;
292 }
293  
294  
295 0 ▶sub create_date {
Subroutine create_date: not called CC=38 SCAR=73.0
Pod documented
296 0 my ($self, $in, %opts) = @_;
297 0 my ($year, $month, $day);
298 0 state $precision_to_int = {year => 0, month => 1, day => 2};
299 0 state $int_to_precision = {map {$precision_to_int->{$_} => $_} keys %{$precision_to_int}};
300 0 ▶ my $min_precision = $precision_to_int->{$opts{min_precision} || $opts{precision} || 'year'};
Condition0%0/5
$opts{'min_precision'} || $opts{'precision'} || 'year'
l!l
00
$opts{"min_precision"} or $opts{"precision"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'min_precision'} || $opts{'precision'} || 'year'
$opts{"min_precision"} $opts{"precision"}
Truth table0%0/3
A or B
A= $opts{"min_precision"}B= $opts{"precision"}
ABresult
1X1
011
000
301 0 ▶ my $max_precision = $precision_to_int->{$opts{max_precision} || $opts{precision} || 'day'};
Condition0%0/5
$opts{'max_precision'} || $opts{'precision'} || 'day'
l!l
00
$opts{"max_precision"} or $opts{"precision"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{'max_precision'} || $opts{'precision'} || 'day'
$opts{"max_precision"} $opts{"precision"}
Truth table0%0/3
A or B
A= $opts{"max_precision"}B= $opts{"precision"}
ABresult
1X1
011
000
302 0 my $req;
303  
304 0 ▶ if (!defined($min_precision) || !defined($max_precision) || $max_precision < $min_precision) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $min_precision && defined $max_precision or $max_precision < $min_precision) 0 0 0
Condition0%0/6
not defined $min_precision && defined $max_precision or $max_precision < $min_precision
l!l&&r!l&&!r
000
defined $min_precision && defined $max_precision
!ll&&!rl&&r
000
MC/DC0%0/3
not defined $min_precision && defined $max_precision or $max_precision < $min_precision
defined $min_precision defined $max_precision $max_precision < $min_precision
Truth table0%0/4
not(A && B) or C
A= defined $min_precisionB= defined $max_precisionC= $max_precision < $min_precision
ABCresult
0XX1
10X1
1111
1100
305 0 croak 'Invalid precision option';
306 }
307  
308 0 ▶ if (ref($in)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in) 0 0 0
309 0 ▶ if (eval {$in->can('epoch')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $in->can('epoch') } }) { } 0 0 0
310 0 $in = $in->epoch;
311 } else {
312 0 return $self->create_date(scalar($in->()), %opts);
313 }
314 }
315  
316 0 ($year, $month, $day) = $in =~ /^([12][0-9]{3})(?:-([01][0-9])(?:-([0-3][0-9]))?)?Z$/;
317  
318 0 ▶ unless (length($year // '') == 4) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length($year // "") == 4) 0 0 0
Condition0%0/2
$year // ""
l!l
00
MC/DC0%0/1
$year // ""
$year
Truth table0%0/2
A
A= $year
Aresult
11
00
319 0 ▶ if ($in eq 'now' || $in eq 'today') {
Branch: 2 branches
BranchTrueFalseTotal
if ($in eq 'now' or $in eq 'today') { } 0 0 0
elsif ($in =~ /^[1-9][0-9]*$/) { } 0 0 0
Condition0%0/3
$in eq "now" or $in eq "today"
l!l&&r!l&&!r
000
MC/DC0%0/2
$in eq "now" or $in eq "today"
$in eq "now" $in eq "today"
Truth table0%0/3
A or B
A= $in eq "now"B= $in eq "today"
ABresult
1X1
011
000
320 0 $in = time();
321 } elsif ($in =~ /^[1-9][0-9]*$/) {
322 0 $in = int($in);
323 0 ▶ if ($in > 32503680000) {
Branch: 1 branch
BranchTrueFalseTotal
if ($in > '32503680000') 0 0 0
324 0 croak 'Unlikely far date given. Likely miliseconds are passed as seconds?';
325 }
326 } else {
327 0 croak 'Invalid format';
328 }
329  
330 0 (undef,undef,undef,$day,$month,$year) = gmtime($in);
331 0 $year += 1900;
332 0 $month += 1;
333 }
334  
335 0 foreach my $entry ($year, $month, $day) {
336 0 ▶ $entry = int($entry // 0);
Condition0%0/2
$entry // 0
l!l
00
MC/DC0%0/1
$entry // 0
$entry
Truth table0%0/2
A
A= $entry
Aresult
11
00
337 }
338  
339 0 ▶ croak 'Invalid year' if $year && ($year < 1583 || $year > 9999);
Branch: 1 branch
BranchTrueFalseTotal
if $year and $year < 1583 || $year > 9999 0 0 0
Condition0%0/6
$year and $year < 1583 || $year > 9999
!ll&&!rl&&r
000
$year < 1583 or $year > 9999
l!l&&r!l&&!r
000
MC/DC0%0/3
$year and $year < 1583 || $year > 9999
$year $year < 1583 $year > 9999
Truth table0%0/4
A and B or C
A= $yearB= $year < 1583C= $year > 9999
ABCresult
0XX0
1000
11X1
1011
340 0 ▶ croak 'Invalid month' if $month && ($month < 1 || $month > 12);
Branch: 1 branch
BranchTrueFalseTotal
if $month and $month < 1 || $month > 12 0 0 0
Condition0%0/6
$month and $month < 1 || $month > 12
!ll&&!rl&&r
000
$month < 1 or $month > 12
l!l&&r!l&&!r
000
MC/DC0%0/3
$month and $month < 1 || $month > 12
$month $month < 1 $month > 12
Truth table0%0/4
A and B or C
A= $monthB= $month < 1C= $month > 12
ABCresult
0XX0
1000
11X1
1011
341 0 ▶ croak 'Invalid day' if $day && ($day < 1 || $day > 31);
Branch: 1 branch
BranchTrueFalseTotal
if $day and $day < 1 || $day > 31 0 0 0
Condition0%0/6
$day and $day < 1 || $day > 31
!ll&&!rl&&r
000
$day < 1 or $day > 31
l!l&&r!l&&!r
000
MC/DC0%0/3
$day and $day < 1 || $day > 31
$day $day < 1 $day > 31
Truth table0%0/4
A and B or C
A= $dayB= $day < 1C= $day > 31
ABCresult
0XX0
1000
11X1
1011
342  
343 0 ▶ $month = 0 unless $year;
Branch: 1 branch
BranchTrueFalseTotal
unless $year 0 0 0
344 0 ▶ $day = 0 unless $month;
Branch: 1 branch
BranchTrueFalseTotal
unless $month 0 0 0
345  
346 0 ▶ for (my $precision = $max_precision; !defined($req) && $precision >= $min_precision; $precision--) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $req and $precision >= $min_precision) 0 0 0
Condition0%0/3
not defined $req and $precision >= $min_precision
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $req and $precision >= $min_precision
not defined $req $precision >= $min_precision
Truth table0%0/3
not(A) and B
A= not defined $reqB= $precision >= $min_precision
ABresult
0X0
100
111
347 0 my $precision_name = $int_to_precision->{$precision};
348 0 ▶ if ($precision_name eq 'day' && $day) {
Branch: 3 branches
BranchTrueFalseTotal
if ($precision_name eq 'day' and $day) { } 0 0 0
elsif ($precision_name eq 'month' and $month) { } 0 0 0
elsif ($precision_name eq 'year' and $year) { } 0 0 0
Condition0%0/3
$precision_name eq "day" and $day
!ll&&!rl&&r
000
MC/DC0%0/2
$precision_name eq "day" and $day
$precision_name eq "day" $day
Truth table0%0/3
A and B
A= $precision_name eq "day"B= $day
ABresult
0X0
100
111
349 0 $req = sprintf('%04u-%02u-%02uZ', $year, $month, $day);
350 ▶ } elsif ($precision_name eq 'month' && $month) {
Condition0%0/3
$precision_name eq "month" and $month
!ll&&!rl&&r
000
MC/DC0%0/2
$precision_name eq "month" and $month
$precision_name eq "month" $month
Truth table0%0/3
A and B
A= $precision_name eq "month"B= $month
ABresult
0X0
100
111
351 0 $req = sprintf('%04u-%02uZ', $year, $month);
352 ▶ } elsif ($precision_name eq 'year' && $year) {
Condition0%0/3
$precision_name eq "year" and $year
!ll&&!rl&&r
000
MC/DC0%0/2
$precision_name eq "year" and $year
$precision_name eq "year" $year
Truth table0%0/3
A and B
A= $precision_name eq "year"B= $year
ABresult
0X0
100
111
353 0 $req = sprintf('%04uZ', $year);
354 }
355 }
356  
357 0 ▶ croak 'Cannot generate request at required precision' unless defined $req;;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $req 0 0 0
358  
359 0 return $self->generate(ns => NS_DATE, style => 'date', input => $req, request => $req,
360 generator => $self->create_generator(
361 uuid => '97b7f241-e1c5-4f02-ae3c-8e31e501e1dc',
362 tagname => 'gregorian-date-generator',
363 ns => NS_DATE,
364 style => 'date',
365 for_type => $self->db->create_tag([$self->wk->uuid => 'a414c87d-efe0-4376-8eae-66aefa78cf92'], [$self->wk->tagname => 'gregorian-date']),
366 ),
367 );
368 }
369  
370  
371 0 ▶sub generate {
Subroutine generate: not called CC=33 SCAR=70.2
Pod documented
372 0 my ($self, %opts) = @_;
373 0 my Data::TagDB $db = $self->db;
374 0 my Data::TagDB::WellKnown $wk = $self->wk;
375 0 my Data::TagDB::Tag $tagname = $wk->tagname(1);
376 0 my Data::TagDB::Tag $tag;
377 0 my Data::TagDB::Tag $gen_tag = $opts{generator};
378 0 my Data::TagDB::Tag $ns_tag;
379 my $ns_uuid;
380 0 my $input = $opts{input};
381 0 my $request = $opts{request};
382 0 my @tagnames = ($opts{tagname});
383 0 my $style = $opts{style};
384 0 my $uuid;
385  
386 0 ▶ if (defined $opts{ns}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $opts{"ns"}) 0 0 0
387 0 ▶ if (eval {$opts{ns}->isa('Data::TagDB::Tag')}) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { $opts{'ns'}->isa('Data::TagDB::Tag') } }) { } 0 0 0
388 0 $ns_tag = $opts{ns};
389 } else {
390 0 $ns_tag = $self->create_namespace($opts{ns});
391 0 $ns_uuid = $opts{ns};
392 }
393 }
394  
395 0 ▶ if (!defined($ns_uuid) && !defined($ns_tag) && defined($gen_tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ns_uuid || defined $ns_tag and defined $gen_tag) 0 0 0
Condition0%0/6
not defined $ns_uuid || defined $ns_tag and defined $gen_tag
!ll&&!rl&&r
000
defined $ns_uuid || defined $ns_tag
l!l&&r!l&&!r
000
MC/DC0%0/3
not defined $ns_uuid || defined $ns_tag and defined $gen_tag
defined $ns_uuid defined $ns_tag defined $gen_tag
Truth table0%0/4
not(A || B) and C
A= defined $ns_uuidB= defined $ns_tagC= defined $gen_tag
ABCresult
1XX0
01X0
0000
0011
396 0 $ns_tag = $gen_tag->attribute('using_namespace');
397 }
398  
399 0 ▶ if (!defined($ns_uuid) && defined($ns_tag)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $ns_uuid and defined $ns_tag) 0 0 0
Condition0%0/3
not defined $ns_uuid and defined $ns_tag
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $ns_uuid and defined $ns_tag
not defined $ns_uuid defined $ns_tag
Truth table0%0/3
not(A) and B
A= not defined $ns_uuidB= defined $ns_tag
ABresult
0X0
100
111
400 0 $ns_uuid = $ns_tag->uuid;
401 }
402  
403 0 ▶ if (defined $gen_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $gen_tag) 0 0 0
404 0 ▶ $style ||= $gen_tag->attribute('generator_style');
Condition0%0/3
$style ||= $gen_tag->attribute("generator_style")
l!l&&r!l&&!r
000
MC/DC0%0/2
$style ||= $gen_tag->attribute("generator_style")
$style $gen_tag->attribute("generator_style")
Truth table0%0/3
A ||= B
A= $styleB= $gen_tag->attribute("generator_style")
ABresult
1X1
011
000
405 0 ▶ $opts{copy_names} ||= $gen_tag->attribute('generator_copy_names');
Condition0%0/3
$opts{"copy_names"} ||= $gen_tag->attribute("generator_copy_names")
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"copy_names"} ||= $gen_tag->attribute("generator_copy_names")
$opts{"copy_names"} $gen_tag->attribute("generator_copy_names")
Truth table0%0/3
A ||= B
A= $opts{"copy_names"}B= $gen_tag->attribute("generator_copy_names")
ABresult
1X1
011
000
406 }
407  
408 0 ▶ croak 'No namespace provided (directly or indirectly)' unless defined $ns_uuid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ns_uuid 0 0 0
409  
410 0 ▶ croak 'No style defined' unless defined $style;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $style 0 0 0
411  
412 0 ▶ if (defined($request) && !defined($input)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request and not defined $input) 0 0 0
Condition0%0/3
defined $request and not defined $input
!ll&&!rl&&r
000
MC/DC0%0/2
defined $request and not defined $input
defined $request not defined $input
Truth table0%0/3
A and not(B)
A= defined $requestB= not defined $input
ABresult
0X0
100
111
413 # mimetype
414 # date
415 # gte
416 # gte-simple
417 0 ▶ if ($style eq 'name-based') {
Branch: 5 branches
BranchTrueFalseTotal
if ($style eq 'name-based') { } 0 0 0
elsif ($style eq 'id-based') { } 0 0 0
elsif ($style eq 'integer-based') { } 0 0 0
elsif ($style eq 'tag-based') { } 0 0 0
elsif ($style eq 'tagcombiner') { } 0 0 0
418 0 $input = encode('UTF-8', $request);
419 0 push(@tagnames, $request);
420 } elsif ($style eq 'id-based') {
421 0 my $name;
422  
423 0 ▶ if (($input, $name) = $request =~ /^(#?[a-zA-Z0-9\-\.\+]+) (.+)$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (($input, $name) = $request =~ /^(#?[a-zA-Z0-9\-\.\+]+) (.+)$/) { } 0 0 0
elsif (($input) = $request =~ /^(#?[a-zA-Z0-9\-\.\+]+)$/) { } 0 0 0
424 # noop
425 } elsif (($input) = $request =~ /^(#?[a-zA-Z0-9\-\.\+]+)$/) {
426 0 $name = undef;
427 } else {
428 0 croak 'Invalid format, expected: "id name", or "id", got: '.$request;
429 }
430  
431 0 $input = lc($input);
432 } elsif ($style eq 'integer-based') {
433 0 ▶ croak 'Invalid integer' unless $request =~ /^-?[0-9]+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $request =~ /^-?[0-9]+$/ 0 0 0
434 0 $input = int($request);
435 } elsif ($style eq 'tag-based') {
436 0 ▶ unless (ref $request) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request) 0 0 0
437 0 ▶ croak 'Invalid UUID: '.$request unless $request =~ RE_UUID;
Branch: 1 branch
BranchTrueFalseTotal
unless $request =~ /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/ 0 0 0
438 0 $request = $db->create_tag([$wk->uuid => $request]);
439 }
440  
441 0 $input = $request->uuid;
442  
443 0 ▶ if ($opts{copy_names}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"copy_names"}) 0 0 0
444 0 push(@tagnames, $db->metadata(tag => $request, relation => $wk->also_shares_identifier, type => $tagname)->collect('data_raw'));
445 }
446  
447 0 $request = $input;
448 } elsif ($style eq 'tagcombiner') {
449 0 my @uuids;
450  
451 0 ▶ unless (ref $request) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $request) 0 0 0
452 0 $request = [split /--/, $request];
453 }
454  
455 0 foreach my $entry (@{$request}) {
456 0 ▶ $entry = $db->create_tag([$wk->uuid => $entry]) unless ref $entry;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $entry 0 0 0
457 }
458  
459 0 @uuids = sort map {$_->uuid} @{$request};
460  
461 0 $input = join(',', @uuids);
462 0 $request = join('--', @uuids);
463 } else {
464 0 croak 'Unsupported generator style: '.$style;
465 }
466 }
467  
468 0 ▶ croak 'No input' unless defined $input;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $input 0 0 0
469  
470 0 $uuid = Data::Identifier::Generate->generic(namespace => $ns_uuid, input => $input)->uuid;
471  
472 0 $tag = $db->create_tag([$wk->uuid => $uuid], [(map {$tagname => $_} @tagnames)]);
473  
474 0 ▶ if (defined $request) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $request) 0 0 0
475 0 $db->create_metadata(tag => $tag, relation => $wk->generator_request(1), data_raw => $request);
476 }
477  
478 0 ▶ if ($gen_tag) {
Branch: 1 branch
BranchTrueFalseTotal
if ($gen_tag) 0 0 0
479 0 my Data::TagDB::Tag $for_type = $gen_tag->attribute('for_type');
480  
481 0 $db->create_relation(tag => $tag, relation => $wk->generated_by(1), related => $gen_tag);
482 0 ▶ $db->create_relation(tag => $tag, relation => $wk->has_type(1), related => $for_type) if defined $for_type;
Branch: 1 branch
BranchTrueFalseTotal
if defined $for_type 0 0 0
483 }
484  
485 0 return $tag;
486 }
487  
488 # ---- Private helpers ----
489  
490 0 ▶sub _new {
Subroutine _new: not called CC=2 SCAR=17.9
491 0 my ($pkg, %opts) = @_;
492  
493 0 ▶ $opts{cache} //= $opts{db}->create_cache;
Condition0%0/3
$opts{"cache"} //= $opts{"db"}->create_cache
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts{"cache"} //= $opts{"db"}->create_cache
$opts{"cache"} $opts{"db"}->create_cache
Truth table0%0/3
A //= B
A= $opts{"cache"}B= $opts{"db"}->create_cache
ABresult
1X1
011
000
494  
495 0 return $pkg->SUPER::_new(%opts);
496 }
497  
498 1 1;
499  
500 __END__
« lib/Data/TagDB/Base.pm ↑ Summary lib/Data/TagDB/Tag.pm »