FileSpec/OutputBindings.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 0.0% 0 / 15 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 122 CC 16 SCAR 38.5
CC
16
Cov
0%
_build_format
90.0
serialize_iterator
72.0
BEGIN
2.0
CRAP 47.1
1 package App::perlrdf::FileSpec::OutputBindings;
2  
3 use 5.010;
4 use autodie;
5 use strict;
6 use warnings;
7 use utf8;
8  
9 BEGIN {
10 $App::perlrdf::FileSpec::OutputBindings::AUTHORITY = 'cpan:TOBYINK';
11 $App::perlrdf::FileSpec::OutputBindings::VERSION = '0.004';
12 }
13  
14 use Any::Moose;
15 use File::Temp qw(tempfile);
16 use JSON qw(from_json);
17 use RDF::Trine;
18 use Spreadsheet::Wright;
19 use YAML::XS qw(Dump);
20  
21 use namespace::clean;
22  
23 extends 'App::perlrdf::FileSpec::OutputFile';
24  
25 sub _build_format
26 {
27 my $self = shift;
28 return 'YAML' if $self->uri =~ /\.ya?ml/i;
29 return 'XML' if $self->uri =~ /\.xml/i;
30 return 'JSON' if $self->uri =~ /\.json/i;
31 return 'CSV' if $self->uri =~ /\.csv/i;
32 return 'XLS' if $self->uri =~ /\.xls/i;
33 return 'HTML' if $self->uri =~ /\.html?/i;
34 return 'XHTML' if $self->uri =~ /\.xhtml/i;
35 return 'ODS' if $self->uri =~ /\.ods/i;
36 return 'Text';
37 }
38  
39 sub serialize_iterator
40 {
41 my ($self, $iter) = @_;
42  
43 if ($self->format =~ /json/i)
44 {
45 $self->handle->print($iter->as_json);
46 }
47 elsif ($self->format =~ /yaml/i)
48 {
49 $self->handle->print(Dump from_json($iter->as_json));
50 }
51 elsif ($self->format =~ /xml/i)
52 {
53 $iter->print_xml($self->handle);
54 }
55 elsif ($self->format =~ /te?xt/i)
56 {
57 $self->handle->print($iter->as_string);
58 }
59 else
60 {
61 my ($dummyfh, $filename) = tempfile();
62 $dummyfh->close;
63 my $s = Spreadsheet::Wright->new(
64 filename => $filename,
65 format => $self->format,
66 );
67 $s->addrow(map {+{
68 font_weight => 'bold',
69 font_style => 'italic',
70 content => $_,
71 }} $iter->binding_names);
72 while (my $row = $iter->next)
73 {
74 my %row = %$row;
75 $s->addrow(map
76 {
77 if ($_->is_resource)
78 {
79 +{
80 color => 'blue',
81 content => $_->as_ntriples,
82 }
83 }
84 elsif ($_->is_literal)
85 {
86 +{
87 color => 'black',
88 content => $_->as_ntriples,
89 }
90 }
91 elsif (defined $_)
92 {
93 +{
94 color => 'green',
95 content => $_->as_ntriples,
96 }
97 }
98 else
99 {
100 +{
101 color => 'black',
102 content => q(),
103 }
104 }
105 }
106 @row{ $iter->binding_names });
107 }
108 $s->close;
109
110 local @ARGV = $filename;
111 while (<>)
112 {
113 $self->handle->print($_);
114 }
115 }
116
117 $self->handle->close;
118 }
119
120 1;