AtteanX/Serializer/TextTable.pm

filter: Statementstmt 64.4% 38 / 59 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 92.3% 12 / 13 Podpod 100% 2 / 2 totaltotal 50.9% 52 / 102 CC 12 SCAR 43.6
CC
12
Cov
46%
_serialize_value
156.0
BEGIN
1.0
BEGIN
1.0
CRAP 78.5
« Attean/API/Term.pm ↑ Summary AtteanX/Parser/Turtle.pm »
1 =head1 NAME
2  
3 AtteanX::Serializer::TextTable - SPARQL Results TSV Serializer
4  
5 =head1 VERSION
6  
7 This document describes AtteanX::Serializer::TextTable version 0.041
8  
9 =head1 SYNOPSIS
10  
11 use Attean;
12 my $s = Attean->get_serializer('TextTable')->new();
13 $s->serialize_iter_to_io( $fh, $iter );
14  
15 =head1 DESCRIPTION
16  
17 ...
18  
19 =head1 ATTRIBUTES
20  
21 =over 4
22  
23 =item C<< canonical_media_type >>
24  
25 =item C<< file_extensions >>
26  
27 =back
28  
29 =head1 METHODS
30  
31 =over 4
32  
33 =cut
34  
35 2 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
37  
38 2 package AtteanX::Serializer::TextTable 0.041 {
39 2 ▶ use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
40 2 ▶ use Types::Standard qw(Str Bool ArrayRef);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 2 ▶ use Encode qw(encode);
Subroutine BEGIN: called CC=1 SCAR=0.0
42 2 ▶ use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
43 2 ▶ use Attean::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 2 ▶ use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
45 2 ▶ use Text::Table;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 2 ▶ use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 2 my @rule = qw(- +);
49 2 has 'canonical_media_type' => (is => 'ro', isa => Str, init_arg => undef, default => 'text/plain');
50 2 has 'number_rows' => (is => 'rw', isa => Bool, default => 0);
51  
52 =item C<< media_types >>
53  
54 Returns a list of media types that identify the format produced by this serializer.
55  
56 =cut
57  
58 12 ▶ sub media_types {
Subroutine media_types: called CC=1 SCAR=0.0
Pod documented
59 12 return [qw(text/plain)];
60 }
61  
62 =item C<< file_extensions >>
63  
64 Returns a list of file extensions associated with the serialized format.
65  
66 =cut
67  
68 3 ▶ sub file_extensions { return [qw(txt text)] };
Subroutine file_extensions: called CC=1 SCAR=0.0
Pod documented
69
70 =item C<< serialize_iter_to_io( $fh, $iterator ) >>
71  
72 Serializes the L<Attean::API::Binding> objects from C<< $iterator >> to the
73 L<IO::Handle> object C<< $fh >>.
74  
75 =cut
76  
77 sub serialize_iter_to_io {
78 my $self = shift;
79 my $io = shift;
80 my $iter = shift;
81
82 my @vars;
83 if ($iter->does('Attean::API::ResultOrTermIterator')) {
84 @vars = @{ $iter->variables };
85 } elsif ($iter->does('Attean::API::TripleIterator')) {
86 @vars = qw(subject predicate object);
87 } else {
88 @vars = qw(subject predicate object graph);
89 }
90  
91 my @header_names = @vars;
92 if ($self->number_rows) {
93 unshift(@header_names, '#');
94 }
95  
96 my @headers = (\q"| ");
97 push(@headers, map { $_ => \q" | " } @header_names);
98 pop @headers;
99 push @headers => (\q" |");
100  
101 my $table = Text::Table->new(@headers);
102  
103 my @rule = qw(- +);
104 my @rows;
105 my $row = 1;
106 while (my $t = $iter->next()) {
107 my @strings = map { $self->_serialize_value($_) } map { eval { $t->value($_) } } @vars;
108 if ($self->number_rows) {
109 unshift(@strings, $row++);
110 }
111 push(@rows, \@strings);
112 }
113 $table->load(@rows);
114
115 print {$io} join('',
116 $table->rule(@rule),
117 $table->title,
118 $table->rule(@rule),
119 map({ $table->body($_) } 0 .. @rows),
120 $table->rule(@rule)
121 );
122 }
123  
124 0 ▶ sub _serialize_value {
Subroutine _serialize_value: not called CC=12 SCAR=50.5
125 0 my $self = shift;
126 0 my $value = shift;
127 0 ▶ return '' unless (blessed($value));
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($value) 0 0 0
128 0 my $lex = $value->value;
129 0 ▶ if ($value->does('Attean::API::IRI')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($value->does('Attean::API::IRI')) { } 0 0 0
elsif ($value->does('Attean::API::Literal') and my $dt = $value->datatype) { } 0 0 0
130 # this shortening to something like a prefixed name is a heuristic
131 # suitable for texttable, but not for cases where the value must
132 # be able to round-tripl through a parser
133 0 ▶ if (my $ns = $self->namespaces) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ns = $self->namespaces) 0 0 0
134 0 my $qname;
135 my $len;
136 0 ▶ while (my ($prefix, $nsURI) = $ns->each_map) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = $ns->each_map) 0 0 0
137 0 my $base = $nsURI->iri->value;
138 0 ▶ if (substr($lex, 0, length($base)) eq $base) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($lex, 0, length $base) eq $base) 0 0 0
139 0 my $local = substr($lex, length($base));
140 0 ▶ if (not(defined($qname)) or length($local) < $len) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $qname or length $local < $len) 0 0 0
Condition0%0/3
not defined $qname or length $local < $len
l!l&&r!l&&!r
000
MC/DC0%0/2
not defined $qname or length $local < $len
not defined $qname length $local < $len
Truth table0%0/3
not(A) or B
A= not defined $qnameB= length $local < $len
ABresult
1X1
011
000
141 0 $qname = join(':', $prefix, $local);
142 0 $len = length($local);
143 }
144 }
145 }
146 0 ▶ return $qname if (defined($qname));
Branch: 1 branch
BranchTrueFalseTotal
if defined $qname 0 0 0
147 }
148 0 return $value->ntriples_string;
149 ▶ } elsif ($value->does('Attean::API::Literal') and my $dt = $value->datatype) {
Condition0%0/3
$value->does("Attean::API::Literal") and my $dt = $value->datatype
!ll&&!rl&&r
000
MC/DC0%0/2
$value->does("Attean::API::Literal") and my $dt = $value->datatype
$value->does("Attean::API::Literal") my $dt = $value->datatype
Truth table0%0/3
A and B
A= $value->does("Attean::API::Literal")B= my $dt = $value->datatype
ABresult
0X0
100
111
150 0 ▶ if ($dt->value eq 'http://www.w3.org/2001/XMLSchema#string') {
Branch: 1 branch
BranchTrueFalseTotal
if ($dt->value eq "http://www.w3.org/2001/XMLSchema#string") 0 0 0
151 0 return qq["$lex"];
152 }
153 0 my $dts = $self->_serialize_value($dt);
154 0 return '"' . $lex . '"^^' . $dts;
155 }
156 0 return $value->ntriples_string;
157 }
158
159 =item C<< serialize_iter_to_bytes( $iterator ) >>
160  
161 Serializes the L<Attean::API::Binding> objects from C<< $iterator >>
162 and returns the serialization as a UTF-8 encoded byte string.
163  
164 =cut
165  
166 sub serialize_iter_to_bytes {
167 my $self = shift;
168 my $iter = shift;
169
170 my @vars = ($iter->does('Attean::API::TripleIterator') or $iter->does('Attean::API::QuadIterator')) ? $iter->variables : @{ $iter->variables };
171 my @header_names = @vars;
172 if ($self->number_rows) {
173 unshift(@header_names, '#');
174 }
175  
176 my @headers = (\q"| ");
177 push(@headers, map { $_ => \q" | " } @header_names);
178 pop @headers;
179 push @headers => (\q" |");
180  
181 my $table = Text::Table->new(@headers);
182
183 my @rows;
184 my $row = 1;
185 while (my $t = $iter->next()) {
186 my @strings = map { $self->_serialize_value($_) } map { $t->value($_) } @vars;
187 if ($self->number_rows) {
188 unshift(@strings, $row++);
189 }
190 push(@rows, \@strings);
191 }
192 $table->load(@rows);
193  
194 my $data = join('',
195 $table->rule(@rule),
196 $table->title,
197 $table->rule(@rule),
198 map({ $table->body($_) } 0 .. @rows),
199 $table->rule(@rule)
200 );
201 return encode('UTF-8', $data);
202 }
203  
204 2 with 'Attean::API::ResultSerializer';
205 2 with 'Attean::API::AbbreviatingSerializer';
206 }
207  
208 2 1;
209  
210 __END__
« Attean/API/Term.pm ↑ Summary AtteanX/Parser/Turtle.pm »