AtteanX/Serializer/TextTable.pm

filter: Statementstmt 100% 37 / 37 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 100% 51 / 51 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« AtteanX/Serializer/NTriples.pm ↑ Summary Test/Attean/ETagCacheableQuadStore.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.040
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.040 {
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 { blessed($_) ? $_->as_string : '' } 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 =item C<< serialize_iter_to_bytes( $iterator ) >>
125  
126 Serializes the L<Attean::API::Binding> objects from C<< $iterator >>
127 and returns the serialization as a UTF-8 encoded byte string.
128  
129 =cut
130  
131 sub serialize_iter_to_bytes {
132 my $self = shift;
133 my $iter = shift;
134
135 my @vars = ($iter->does('Attean::API::TripleIterator') or $iter->does('Attean::API::QuadIterator')) ? $iter->variables : @{ $iter->variables };
136 my @header_names = @vars;
137 if ($self->number_rows) {
138 unshift(@header_names, '#');
139 }
140  
141 my @headers = (\q"| ");
142 push(@headers, map { $_ => \q" | " } @header_names);
143 pop @headers;
144 push @headers => (\q" |");
145  
146 my $table = Text::Table->new(@headers);
147
148 my @rows;
149 my $row = 1;
150 while (my $t = $iter->next()) {
151 my @strings = map { blessed($_) ? $_->ntriples_string : '' } map { $t->value($_) } @vars;
152 if ($self->number_rows) {
153 unshift(@strings, $row++);
154 }
155 push(@rows, \@strings);
156 }
157 $table->load(@rows);
158  
159 my $data = join('',
160 $table->rule(@rule),
161 $table->title,
162 $table->rule(@rule),
163 map({ $table->body($_) } 0 .. @rows),
164 $table->rule(@rule)
165 );
166 return encode('UTF-8', $data);
167 }
168  
169 2 with 'Attean::API::ResultSerializer';
170 }
171  
172 2 1;
173  
174 __END__
« AtteanX/Serializer/NTriples.pm ↑ Summary Test/Attean/ETagCacheableQuadStore.pm »