Attean.pm

filter: Statementstmt 93.1% 287 / 308 Branchbran 82.4% 94 / 114 Conditioncond 68.7% 22 / 32 MC/DCmcdc 55.0% 11 / 20 Subroutinesub 93.7% 45 / 48 Podpod 100% 14 / 14 totaltotal 88.2% 473 / 536 CC 100 SCAR 24.8
CC
100
Cov
89%
negotiate_serializer
27.0
import
17.9
acceptable_parsers
15.0
CRAP 12.0
« Attean/TreeRewriter.pm ↑ Summary Attean/API/Model.pm »
1 =head1 NAME
2  
3 Attean - A Semantic Web Framework
4  
5 =head1 VERSION
6  
7 This document describes Attean version 0.040
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 use Attean::RDF qw(iri);
13
14 my $store = Attean->get_store('Memory')->new();
15 my $parser = Attean->get_parser('NTriples')->new();
16
17 # iterator of triples and quads
18 my $iter = $parser->parse_iter_from_io(\*STDIN);
19
20 # add a graph name to all triples
21 my $graph = iri('http://graph-name/');
22 my $quads = $iter->as_quads($graph);
23
24 $store->add_iter($quads);
25 my $model = Attean::QuadModel->new( store => $store );
26 my $iter = $model->get_quads();
27 while (my $quad = $iter->next) {
28 say $quad->object->ntriples_string;
29 }
30  
31 # run a SPARQL query and iterate over the results
32 my $sparql = 'SELECT * WHERE { ?s ?p ?o }';
33 my $s = Attean->get_parser('SPARQL')->new();
34 my ($algebra) = $s->parse($sparql);
35 my $results = $model->evaluate($algebra, $graph);
36 while (my $r = $results->next) {
37 say $r->as_string;
38 }
39  
40 =head1 DESCRIPTION
41  
42 Attean provides APIs for parsing, storing, querying, and serializing
43 Semantic Web (RDF and SPARQL) data.
44  
45 =head1 METHODS
46  
47 =over 4
48  
49 =cut
50  
51 50 package Attean {
52 50 ▶ use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 50 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
54 50 our $VERSION = '0.040';
55 50 ▶ use Attean::API;
Subroutine BEGIN: called CC=1 SCAR=0.0
56
57 50 ▶ use Attean::Blank;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 50 ▶ use Attean::Literal;
Subroutine BEGIN: called CC=1 SCAR=0.0
59 50 ▶ use Attean::Variable;
Subroutine BEGIN: called CC=1 SCAR=0.0
60 50 ▶ use Attean::IRI;
Subroutine BEGIN: called CC=1 SCAR=0.0
61
62 50 ▶ use Attean::Triple;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 50 ▶ use Attean::Quad;
Subroutine BEGIN: called CC=1 SCAR=0.0
64 50 ▶ use Attean::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
65
66 50 ▶ use Attean::QuadModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
67 50 ▶ use Attean::TripleModel;
Subroutine BEGIN: called CC=1 SCAR=0.0
68 50 ▶ use Attean::BindingEqualityTest;
Subroutine BEGIN: called CC=1 SCAR=0.0
69
70 50 ▶ use Attean::CodeIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
71 50 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
72 50 ▶ use Attean::IteratorSequence;
Subroutine BEGIN: called CC=1 SCAR=0.0
73  
74 50 ▶ use Attean::IDPQueryPlanner;
Subroutine BEGIN: called CC=1 SCAR=0.0
75
76 50 ▶ use Attean::TermMap;
Subroutine BEGIN: called CC=1 SCAR=0.0
77
78 50 ▶ use HTTP::Negotiate qw(choose);
Subroutine BEGIN: called CC=1 SCAR=0.0
79 50 ▶ use List::Util qw(any all);
Subroutine BEGIN: called CC=1 SCAR=0.0
80 50 ▶ use Module::Load::Conditional qw(can_load);
Subroutine BEGIN: called CC=1 SCAR=0.0
81 50 ▶ use Role::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
82 50 ▶ use Sub::Util qw(set_subname);
Subroutine BEGIN: called CC=1 SCAR=0.0
83 50 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
84
85 50 ▶ use Module::Pluggable search_path => 'AtteanX::Parser', sub_name => 'parsers', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 50 ▶ use Module::Pluggable search_path => 'AtteanX::Serializer', sub_name => 'serializers', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 50 ▶ use Module::Pluggable search_path => 'AtteanX::Store', sub_name => 'stores', max_depth => 3;
Subroutine BEGIN: called CC=1 SCAR=0.0
88
89 136 ▶ sub import {
Subroutine import: called CC=11 SCAR=28.8
90 136 my $class = shift;
91 136 ▶ if (scalar(@_)) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_) 2 134 136
92 2 my %args = @_;
93 2 ▶ foreach my $p (@{ $args{parsers} || [] }) {
Condition50%1/2
$args{'parsers'} || []
l!l
20
MC/DC0%0/1
$args{'parsers'} || []
$args{'parsers'}
Truth table50%1/2
A
A= $args{'parsers'}
Aresult
11
00
94 # warn "Loading $p parser...";
95 2 ▶ $class->get_parser($p) || die "Failed to load parser: $p";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_parser($p) or die "Failed to load parser: $p" 2 0 2
96 }
97 2 ▶ foreach my $s (@{ $args{serializers} || [] }) {
Condition50%1/2
$args{'serializers'} || []
l!l
02
MC/DC0%0/1
$args{'serializers'} || []
$args{'serializers'}
Truth table50%1/2
A
A= $args{'serializers'}
Aresult
11
00
98 # warn "Loading $s serializer...";
99 0 ▶ $class->get_serializer($s) || die "Failed to load serializer: $s";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_serializer($s) or die "Failed to load serializer: $s" 0 0 0
100 }
101 2 ▶ foreach my $s (@{ $args{stores} || [] }) {
Condition50%1/2
$args{'stores'} || []
l!l
02
MC/DC0%0/1
$args{'stores'} || []
$args{'stores'}
Truth table50%1/2
A
A= $args{'stores'}
Aresult
11
00
102 # warn "Loading $s store...";
103 0 ▶ $class->get_store($s) || die "Failed to load store: $s";
Branch: 1 branch
BranchTrueFalseTotal
$class->get_store($s) or die "Failed to load store: $s" 0 0 0
104 }
105 }
106 }
107
108 =item C<< get_store( $NAME ) >>
109  
110 Attempts to find a L<Attean::API::Store> implementation with the
111 given C<< $NAME >>. This is done using L<Module::Pluggable> and will generally
112 be searching for class names C<< AtteanX::Store::$NAME >>.
113  
114 Returns the full class name if a matching implementation is found, otherwise
115 returns undef.
116  
117 =cut
118  
119 76 ▶ sub get_store {
Subroutine get_store: called CC=1 SCAR=0.0
Pod documented
120 76 my $self = shift;
121 76 return $self->_get_plugin('stores', shift);
122 }
123  
124 =item C<< temporary_model >>
125  
126 Returns a temporary, mutable quad model based on a L<AtteanX::Store::Memory> store.
127  
128 =cut
129  
130 3 ▶ sub temporary_model {
Subroutine temporary_model: called CC=1 SCAR=0.0
Pod documented
131 3 my $self = shift;
132 3 return Attean::MutableQuadModel->new( store => $self->get_store('Memory')->new() )
133 }
134  
135  
136  
137 =item C<< get_serializer( $NAME ) >>
138  
139 =item C<< get_serializer( filename => $FILENAME ) >>
140  
141 =item C<< get_serializer( media_type => $MEDIA_TYPE ) >>
142  
143 Attempts to find a L<Attean::API::Serializer> serializer class with the given
144 C<< $NAME >>, or that can serialize files with the C<< $MEDIA_TYPE >> media
145 type.
146  
147 Returns the full class name if a matching implementation is found, otherwise
148 returns undef.
149  
150 =cut
151  
152 67 ▶ sub get_serializer {
Subroutine get_serializer: called CC=12 SCAR=24.9
Pod documented
153 67 my $self = shift;
154 67 my $role = 'Attean::API::Serializer';
155  
156 67 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 48 19 67
157 48 my $name = shift;
158 48 my $p = $self->_get_plugin('serializers', $name, $role);
159 48 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 33 15 48
160
161 15 foreach my $type (qw'filename media_type') {
162 15 my $p = $self->get_serializer($type => $name);
163 15 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 15 0 15
164 }
165 0 return;
166 }
167 19 my $type = shift;
168 19 my %method = (filename => 'file_extensions', media_type => 'media_types');
169 19 ▶ if (my $method = $method{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $method{$type}) { } 18 1 19
170 18 my $value = shift;
171 18 ▶ $value =~ s/^.*[.]// if ($type eq 'filename');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "filename" 15 3 18
172 18 ▶ $value =~ s/;.*$// if ($type eq 'media_type');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "media_type" 3 15 18
173 18 foreach my $p ($self->serializers()) {
174 127 ▶ if (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {$p, 0})) 127 0 127
175 127 ▶ next unless ($p->does($role));
Branch: 1 branch
BranchTrueFalseTotal
unless $p->does($role) 14 113 127
176 113 my @exts = @{ $p->$method() };
177 113 ▶ return $p if (any { $value eq $_ } @exts);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $value eq $_; } , @exts) 18 95 113
178 }
179 }
180 0 return;
181 } else {
182 1 die "Not a valid constraint in get_serializer call: $type";
183 }
184 }
185
186 =item C<< get_parser( $NAME ) >>
187  
188 =item C<< get_parser( filename => $FILENAME ) >>
189  
190 =item C<< get_parser( media_type => $MEDIA_TYPE ) >>
191  
192 Attempts to find a L<Attean::API::Parser> parser class with the given
193 C<< $NAME >>, or that can parse files with the same extension as
194 C<< $FILENAME >>, or that can parse files with the C<< $MEDIA_TYPE >> media
195 type.
196  
197 Returns the full class name if a matching implementation is found, otherwise
198 returns undef.
199  
200 =cut
201  
202 188 ▶ sub get_parser {
Subroutine get_parser: called CC=14 SCAR=26.7
Pod documented
203 188 my $self = shift;
204 188 my $role = 'Attean::API::Parser';
205
206 188 ▶ if (scalar(@_) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 1) 173 15 188
207 173 my $name = shift;
208 173 my $p = $self->_get_plugin('parsers', $name, $role);
209 173 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 162 11 173
210
211 11 foreach my $type (qw'filename media_type') {
212 11 my $p = $self->get_parser($type => $name);
213 11 ▶ return $p if $p;
Branch: 1 branch
BranchTrueFalseTotal
if $p 11 0 11
214 }
215 0 return;
216 }
217
218 15 ▶ while (my $type = shift) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $type = shift()) 15 0 15
219 15 my %method = (filename => 'file_extensions', media_type => 'media_types');
220 15 ▶ if (my $method = $method{ $type }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $method = $method{$type}) { } 14 1 15
221 14 my $value = shift;
222 14 ▶ $value =~ s/^.*[.]// if ($type eq 'filename');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "filename" 12 2 14
223 14 ▶ $value =~ s/;.*$// if ($type eq 'media_type');
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "media_type" 2 12 14
224 14 foreach my $p ($self->parsers()) {
225 73 ▶ if (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
if (can_load("modules", {$p, 0})) 73 0 73
226 73 ▶ next unless ($p->can('does') and $p->does($role));
Branch: 1 branch
BranchTrueFalseTotal
unless $p->can("does") and $p->does($role) 10 63 73
Condition66%2/3
$p->can("does") and $p->does($role)
!ll&&!rl&&r
01063
MC/DC50%1/2
$p->can("does") and $p->does($role)
$p->can("does") $p->does($role)
Truth table66%2/3
A and B
A= $p->can("does")B= $p->does($role)
ABresult
0X0
100
111
227 63 my @exts = @{ $p->$method() };
228 63 ▶ return $p if (any { $value eq $_ } @exts);
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
if &any(sub { $value eq $_; } , @exts) 14 49 63
229 }
230 }
231 } else {
232 1 die "Not a valid constraint in get_parser call: $type";
233 }
234 }
235 0 return;
236 }
237
238 {
239 50 my %roles = (
240 serializers => 'Attean::API::Serializer',
241 parsers => 'Attean::API::Parser',
242 stores => 'Attean::API::Store',
243 );
244 50 for my $method (keys %roles) {
245 150 my $role = $roles{$method};
246 0 ▶ my $code = sub {
Subroutine list_parsers: not called CC=5 SCAR=22.8
Subroutine list_serializers: called CC=5 SCAR=22.8
247 0 my $self = shift;
248 0 my @classes;
249 0 foreach my $class ($self->$method()) {
250 0 ▶ next unless (can_load( modules => { $class => 0 }));
Branch: 2 branches
BranchTrueFalseTotal
unless can_load("modules", {$class, 0}) 0 0 0
unless can_load("modules", {$class, 0}) 0 234 234
251 0 ▶ push(@classes, $class) if ($class->can('does') and $class->does($role));
Branch: 2 branches
BranchTrueFalseTotal
if $class->can("does") and $class->does($role) 0 0 0
if $class->can("does") and $class->does($role) 216 18 234
Condition33%2/6
$class->can("does") and $class->does($role)
!ll&&!rl&&r
000
$class->can("does") and $class->does($role)
!ll&&!rl&&r
018216
MC/DC0%0/2
$class->can("does") and $class->does($role)
$class->can("does") $class->does($role)
MC/DC50%1/2
$class->can("does") and $class->does($role)
$class->can("does") $class->does($role)
Truth table0%0/3
A and B
A= $class->can("does")B= $class->does($role)
ABresult
0X0
100
111
Truth table66%2/3
A and B
A= $class->can("does")B= $class->does($role)
ABresult
0X0
100
111
252 }
253 0 return @classes;
254 150 };
255 150 Sub::Install::install_sub({
256 code => set_subname("list_${method}", $code),
257 as => "list_${method}"
258 });
259 }
260 }
261
262 297 ▶ sub _get_plugin {
Subroutine _get_plugin: called CC=6 SCAR=19.0
263 297 my $self = shift;
264 297 my $type = shift;
265 297 my $name = shift;
266 297 my @roles = @_;
267 297 foreach my $p ($self->$type()) {
268 1859 ▶ if (lc(substr($p, -(length($name)+2))) eq lc("::$name")) {
Branch: 1 branch
BranchTrueFalseTotal
if (lc substr($p, -(length($name) + 2)) eq lc "::$name") 271 1588 1859
269 271 ▶ unless (can_load( modules => { $p => 0 })) {
Branch: 1 branch
BranchTrueFalseTotal
unless (can_load("modules", {$p, 0})) 0 271 271
270 0 warn $Module::Load::Conditional::ERROR;
271 0 return;
272 }
273
274 271 foreach (@roles) {
275 195 ▶ unless ($p->does($_)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p->does($_)) 0 195 195
276 0 die ucfirst($type) . " class $p failed validation for role $_";
277 }
278 }
279 271 return $p;
280 }
281 }
282 }
283
284 =item C<< negotiate_serializer ( request_headers => $request_headers, restrict => \@serializer_names, extend => \%media_types, role => $role ) >>
285  
286 Returns a two-element list containing an appropriate media type and
287 L<Attean::API::Serializer> class as decided by L<HTTP::Negotiate>. If the
288 C<< 'request_headers' >> key-value is supplied, the C<< $request_headers >> is
289 passed to C<< HTTP::Negotiate::choose >>.
290  
291 The option C<< 'restrict' >>, set to a list of serializer names, can be used to
292 limit the serializers to choose from.
293  
294 The option C<< 'role' >> can be used to limit the serializers to only ones that
295 consume the $role. For example, C<< 'Attean::API::ResultSerializer' >> would
296 limit negotiation to result serializers such as SPARQL/XML and SPARQL/JSON,
297 while C<< 'Attean::API::TripleSerializer' >> would limit negotiation to RDF
298 serializers.
299  
300 Finally, an C<<'extend'>> option can be set to a hashref that contains
301 MIME-types as keys and a custom variant as value. This will enable the user to
302 use this negotiator to return a type that isn't supported by any serializers.
303 The subsequent code will have to find out how to return a representation.
304  
305 =cut
306  
307 12 ▶ sub negotiate_serializer {
Subroutine negotiate_serializer: called CC=27 SCAR=33.0
Pod documented
308 12 my $class = shift;
309 12 my %options = @_;
310 12 my $headers = delete $options{ 'request_headers' };
311 12 my $role = delete $options{ 'role' };
312 12 my $restrict = delete $options{ 'restrict' };
313 12 ▶ my $extend = delete $options{ 'extend' } || {};
Condition100%2/2
delete $options{'extend'} || {}
l!l
210
MC/DC100%1/1
delete $options{'extend'} || {}
delete $options{'extend'}
Truth table100%2/2
A
A= delete $options{'extend'}
Aresult
11
00
314 12 my %serializer_names;
315 my %media_types;
316 12 foreach my $sclass ($class->list_serializers) {
317 156 my $name = $sclass =~ s/^.*://r;
318 156 $serializer_names{lc($name)} = $sclass;
319 156 for (@{ $sclass->media_types }) {
320 180 push(@{ $media_types{$_} }, $sclass);
321 }
322 }
323 12 my %sclasses;
324 12 ▶ if (ref($restrict) && ref($restrict) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $restrict and ref $restrict eq 'ARRAY') { } 3 9 12
Condition66%2/3
ref $restrict and ref $restrict eq "ARRAY"
!ll&&!rl&&r
903
MC/DC50%1/2
ref $restrict and ref $restrict eq "ARRAY"
ref $restrict ref $restrict eq "ARRAY"
Truth table66%2/3
A and B
A= ref $restrictB= ref $restrict eq "ARRAY"
ABresult
0X0
100
111
325 3 foreach (@$restrict) {
326 4 ▶ if (my $sclass = $serializer_names{lc($_)}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sclass = $serializer_names{lc $_}) 3 1 4
327 3 $sclasses{ $sclass } = 1;
328 }
329 }
330 } else {
331 9 %sclasses = reverse %serializer_names;
332 }
333 12 ▶ if (defined($role)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $role) 3 9 12
334 3 foreach my $sclass (keys %sclasses) {
335 39 ▶ unless ($sclass->does($role)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($sclass->does($role)) 25 14 39
336 25 delete $sclasses{ $sclass };
337 }
338 }
339 }
340
341 12 my @default_variants;
342 12 ▶ while (my($type, $sclasses) = each(%media_types)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %media_types) 132 12 144
343 132 foreach my $sclass (@$sclasses) {
344 180 ▶ next unless $sclasses{$sclass};
Branch: 1 branch
BranchTrueFalseTotal
unless $sclasses{$sclass} 67 113 180
345 113 my $qv;
346 # slightly prefer turtle as a readable format to others
347 # try hard to avoid using ntriples as 'text/plain' isn't very useful for conneg
348 113 ▶ if ($type eq 'application/n-triples') {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'application/n-triples') { } 18 95 113
elsif ($type eq 'text/plain') { } 25 70 95
349 18 $qv = 1.0;
350 } elsif ($type eq 'text/plain') {
351 25 $qv = 0.2;
352 } else {
353 70 $qv = 0.99;
354 70 ▶ $qv -= 0.02 if ($type =~ m#/html#); # prefer data formats to HTML
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[/html]u 7 63 70
355 70 ▶ $qv -= 0.05 if ($type =~ m#/x-#); # prefer non experimental media types
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[/x-]u 0 70 70
356 70 ▶ $qv -= 0.01 if ($type =~ m#^application/(?!rdf[+]xml|sparql)#); # prefer standard rdf/xml to other application/* formats
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^application/(?!rdf[+]xml|sparql)]u 6 64 70
357 70 ▶ $qv -= 0.04 if ($type =~ m#^text/(?!turtle)#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/(?!turtle)]u 21 49 70
358 70 ▶ $qv += 0.01 if ($type =~ m#^text/turtle#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^text/turtle]u 15 55 70
359 70 ▶ $qv += 0.01 if ($type =~ m#^application/sparql-results[+]json#);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ m[^application/sparql-results[+]json]u 7 63 70
360 }
361 113 push(@default_variants, [$type, $qv, $type]);
362 }
363 }
364
365 12 my %custom_thunks;
366 my @custom_variants;
367 12 ▶ while (my($type,$thunk) = each(%$extend)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$extend) 3 12 15
368 3 push(@custom_variants, [$thunk, 1.0, $type]);
369 3 $custom_thunks{ $thunk } = [$type, $thunk];
370 }
371
372 # remove variants with media types that are in custom_variants from @variants
373 12 my @variants = grep { not exists $extend->{ $_->[2] } } @default_variants;
374 12 push(@variants, @custom_variants);
375
376 12 my $stype = choose( \@variants, $headers );
377 12 ▶ if (defined($stype) and $custom_thunks{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and $custom_thunks{$stype}) 2 10 12
Condition100%3/3
defined $stype and $custom_thunks{$stype}
!ll&&!rl&&r
282
MC/DC100%2/2
defined $stype and $custom_thunks{$stype}
defined $stype $custom_thunks{$stype}
Truth table100%3/3
A and B
A= defined $stypeB= $custom_thunks{$stype}
ABresult
0X0
100
111
378 2 my $thunk = $stype;
379 2 my $type = $custom_thunks{ $stype }[0];
380 2 return ($type, $thunk);
381 }
382
383 10 ▶ if (defined($stype) and my $sclasses = $media_types{ $stype }) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $stype and my $sclasses = $media_types{$stype}) { } 8 2 10
Condition66%2/3
defined $stype and my $sclasses = $media_types{$stype}
!ll&&!rl&&r
208
MC/DC50%1/2
defined $stype and my $sclasses = $media_types{$stype}
defined $stype my $sclasses = $media_types{$stype}
Truth table66%2/3
A and B
A= defined $stypeB= my $sclasses = $media_types{$stype}
ABresult
0X0
100
111
384 8 return ($stype, $sclasses->[0]);
385 } else {
386 2 die "No appropriate serializer found for content-negotiation: " . Data::Dumper->Dump([$headers, $restrict, $extend], [qw(headers restrict extend)]);
387 }
388 }
389
390 =item C<< acceptable_parsers ( handles => $item_role, prefer => $parser_role ) >>
391  
392 Returns a string value expressing the media types that are acceptable to the
393 parsers available to the system. This string may be used as an 'Accept' HTTP
394 header value.
395  
396 If a C<< handles >> role is supplied, only parsers that produce objects that
397 conform to C<< $item_role >> will be included.
398  
399 If a C<< prefer >> role is supplied, only parsers that conform to
400 C<< $parser_role >> will be included.
401  
402 Parsers are given a quality-value (expressing a preferred order or use) based
403 on the roles each parser consumes. Parsers consuming L<Attean::API::PullParser>
404 are preferred, while those consuming L<Attean::API::AtOnceParser> are not
405 preferred. An exact ordering between parsers consuming similar roles is
406 currently undefined.
407  
408 =cut
409  
410 6 ▶ sub acceptable_parsers {
Subroutine acceptable_parsers: called CC=15 SCAR=27.1
Pod documented
411 6 my $class = shift;
412 6 my %options = @_;
413 6 my $handles = delete $options{ 'handles' };
414 6 my $prefer = delete $options{ 'prefer' };
415  
416 6 ▶ if (defined($handles) and $handles !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handles and not $handles =~ /::/u) 1 5 6
Condition100%3/3
defined $handles and not $handles =~ /::/u
!ll&&!rl&&r
411
MC/DC100%2/2
defined $handles and not $handles =~ /::/u
defined $handles not $handles =~ /::/u
Truth table100%3/3
A and not(B)
A= defined $handlesB= not $handles =~ /::/u
ABresult
0X0
100
111
417 1 $handles = ucfirst(lc($handles));
418 1 $handles = "Attean::API::$handles";
419 }
420 6 ▶ if (defined($prefer) and $prefer !~ /::/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefer and not $prefer =~ /::/u) 2 4 6
Condition100%3/3
defined $prefer and not $prefer =~ /::/u
!ll&&!rl&&r
312
MC/DC100%2/2
defined $prefer and not $prefer =~ /::/u
defined $prefer not $prefer =~ /::/u
Truth table100%3/3
A and not(B)
A= defined $preferB= not $prefer =~ /::/u
ABresult
0X0
100
111
421 2 $prefer = "Attean::API::" . ucfirst($prefer);
422 2 ▶ $prefer = "${prefer}Parser" unless ($prefer =~ /Parser$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $prefer =~ /Parser$/u 2 0 2
423 }
424
425 6 my %media_types;
426 6 foreach my $pclass ($class->list_parsers) {
427 60 ▶ if (defined($handles)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $handles) 20 40 60
428 20 my $type = $pclass->handled_type;
429 20 ▶ next unless ($type->can('role'));
Branch: 1 branch
BranchTrueFalseTotal
unless $type->can("role") 0 20 20
430 20 my $role = $type->role;
431 20 ▶ next unless Role::Tiny::does_role($handles, $role);
Branch: 1 branch
BranchTrueFalseTotal
unless Role::Tiny::does_role($handles, $role) 12 8 20
432 }
433
434 48 ▶ if (defined($prefer)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prefer) 30 18 48
435 30 ▶ next unless ($pclass->does($prefer));
Branch: 1 branch
BranchTrueFalseTotal
unless $pclass->does($prefer) 20 10 30
436 }
437
438 28 my $q = 0.5;
439 28 ▶ if ($pclass->does('Attean::API::PullParser')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($pclass->does('Attean::API::PullParser')) { } 11 17 28
elsif ($pclass->does('Attean::API::AtOnceParser')) { } 5 12 17
440 11 $q += 0.25;
441 } elsif ($pclass->does('Attean::API::AtOnceParser')) {
442 5 $q -= 0.25;
443 }
444
445 28 for (@{ $pclass->media_types }) {
446 39 my $mt = "$_;q=$q";
447 39 $media_types{$mt} = $q;
448 }
449 }
450
451 6 my @sorted = sort { $media_types{$b} <=> $media_types{$a} } keys %media_types;
452 6 return join(',', @sorted);
453 }
454
455
456 # Global registry for extension functions that can be invoked via BIND, FILTER, etc.
457 {
458 50 our %global_functions;
459  
460 =item C<< register_global_function( %uri_to_func ) >>
461  
462 =cut
463 46 ▶ sub register_global_function {
Subroutine register_global_function: called CC=2 SCAR=6.9
Pod documented
464 46 my $class = shift;
465 46 my %args = @_;
466 46 foreach my $uri (keys %args) {
467 483 my $func = $args{ $uri };
468 483 $global_functions{ $uri } = $func;
469 }
470 }
471  
472 =item C<< get_global_function( $uri ) >>
473  
474 =cut
475 0 ▶ sub get_global_function {
Subroutine get_global_function: not called CC=1 SCAR=6.9
Pod documented
476 0 my $class = shift;
477 0 my $uri = shift;
478 0 return $global_functions{ $uri };
479 }
480 }
481
482 # Global registry for extension "functional forms" that can be invoked via BIND, FILTER, etc.
483 # These differ from extension "functions" in that they can be passed undef values as arguments
484 # for expressions whose evaluation resulted in an error.
485 {
486 50 our %global_functional_forms;
487  
488 =item C<< register_global_functional_form( %uri_to_func ) >>
489  
490 =cut
491 46 ▶ sub register_global_functional_form {
Subroutine register_global_functional_form: called CC=2 SCAR=6.9
Pod documented
492 46 my $class = shift;
493 46 my %args = @_;
494 46 foreach my $uri (keys %args) {
495 115 my $func = $args{ $uri };
496 115 $global_functional_forms{ $uri } = $func;
497 }
498 }
499  
500 =item C<< get_global_functional_form( $uri ) >>
501  
502 =cut
503 0 ▶ sub get_global_functional_form {
Subroutine get_global_functional_form: not called CC=1 SCAR=6.9
Pod documented
504 0 my $class = shift;
505 0 my $uri = shift;
506 0 return $global_functional_forms{ $uri };
507 }
508 }
509
510 # Global registry for extension aggregates
511 {
512 50 our %global_aggregates;
513  
514 =item C<< register_global_aggregate( %uri_to_hash ) >>
515  
516 =cut
517 46 ▶ sub register_global_aggregate {
Subroutine register_global_aggregate: called CC=2 SCAR=6.9
Pod documented
518 46 my $class = shift;
519 46 my %args = @_;
520 46 foreach my $uri (keys %args) {
521 46 my $funcs = $args{ $uri };
522 46 $global_aggregates{ $uri } = $funcs;
523 }
524 }
525  
526 =item C<< get_global_aggregate( $uri ) >>
527  
528 =cut
529 2 ▶ sub get_global_aggregate {
Subroutine get_global_aggregate: called CC=1 SCAR=0.0
Pod documented
530 2 my $class = shift;
531 2 my $uri = shift;
532 2 return $global_aggregates{ $uri };
533 }
534 }
535
536 # Global registry for extension datatypes.
537 # When literals of this datatype are constructed, they will have the registered Moo role applied to them.
538 {
539 50 our %datatype_roles;
540  
541 =item C<< register_datatype_role( %uri_to_role ) >>
542  
543 =cut
544 46 ▶ sub register_datatype_role {
Subroutine register_datatype_role: called CC=2 SCAR=6.9
Pod documented
545 46 my $class = shift;
546 46 my %args = @_;
547 46 foreach my $uri (keys %args) {
548 46 my $func = $args{ $uri };
549 46 $datatype_roles{ $uri } = $func;
550 }
551 }
552  
553 =item C<< get_datatype_role( $uri ) >>
554  
555 =cut
556 297 ▶ sub get_datatype_role {
Subroutine get_datatype_role: called CC=1 SCAR=0.0
Pod documented
557 297 my $class = shift;
558 297 my $uri = shift;
559 297 return $datatype_roles{ $uri };
560 }
561 }
562 50 }
563  
564 50 ▶use AtteanX::Functions::CompositeLists;
Subroutine BEGIN: called CC=1 SCAR=0.0
565  
566 50 1;
567  
568 __END__
« Attean/TreeRewriter.pm ↑ Summary Attean/API/Model.pm »