blib/lib/Mojolicious/Plugin/ReplyTable.pm

filter: Statementstmt 94.3% 67 / 71 Branchbran 72.7% 16 / 22 Conditioncond 71.4% 5 / 7 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 100% 13 / 13 Podpod 50.0% 1 / 2 totaltotal 87.3% 104 / 119 CC 16 SCAR 12.4
CC
16
Cov
88%
_to_csv
6.0
_reply_table
5.0
_to_xls
3.1
CRAP 3.5
↑ Summary
1 package Mojolicious::Plugin::ReplyTable;
2  
3 1 ▶use Mojo::Base 'Mojolicious::Plugin';
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 our $VERSION = '0.13';
6 1 $VERSION = eval $VERSION;
7  
8 1 ▶use Mojo::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶sub register {
Subroutine register: called CC=1 SCAR=0.0
Pod documented
11 1 my ($plugin, $app, $config) = @_;
12 1 $plugin->setup_types($app);
13 1 push @{$app->renderer->classes}, __PACKAGE__;
14 1 $app->helper( 'reply.table' => \&_reply_table );
15 }
16  
17 18 ▶sub _reply_table {
Subroutine _reply_table: called CC=5 SCAR=16.1
18 18 my $c = shift;
19 18 ▶ my $default = ref $_[0] ? undef : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 3 18
20 18 ▶ my $data = shift || die 'table data is required';
Condition50%1/2
shift() || die('table data is required')
l!l
180
MC/DC0%0/1
shift() || die('table data is required')
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
21 my %respond = (
22 json => { json => $data },
23 html => { template => 'reply_table', 'reply_table.table' => $data },
24 3 ▶ csv => sub { $_[0]->render(text => _to_csv($_[0], $data)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
25 3 ▶ txt => sub { $_[0]->render(text => _to_txt($_[0], $data)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 2 ▶ xls => sub { $_[0]->render(data => _to_xls($_[0], $data)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 2 ▶ xlsx => sub { $_[0]->render(data => _to_xlsx($_[0], $data)) },
Subroutine __ANON__: called CC=1 SCAR=0.0
28 @_
29 18 );
30 18 ▶ if ($default) {
Branch: 1 branch
BranchTrueFalseTotal
if ($default) 3 15 18
31 3 ▶ $c->stash(format => $default) unless @{$c->accepts};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$c->accepts;} 2 1 3
32 }
33 18 $c->respond_to(%respond);
34 }
35  
36 3 ▶sub _to_csv {
Subroutine _to_csv: called CC=6 SCAR=18.0
37 3 my ($c, $data) = @_;
38 3 require Text::CSV;
39 3 ▶ my $csv_options = $c->stash('reply_table.csv_options') || {};
Condition100%2/2
$c->stash('reply_table.csv_options') || {}
l!l
21
MC/DC100%1/1
$c->stash('reply_table.csv_options') || {}
$c->stash('reply_table.csv_options')
Truth table100%2/2
A
A= $c->stash('reply_table.csv_options')
Aresult
11
00
40 3 ▶ $csv_options->{binary} = 1 unless exists $csv_options->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $csv_options->{"binary"} 3 0 3
41 3 ▶ my $csv = Text::CSV->new($csv_options)
Branch: 1 branch
BranchTrueFalseTotal
my $csv = "Text::CSV"->new($csv_options) or die "Text::CSV"->error_diag 2 1 3
42 or die Text::CSV->error_diag();
43 2 my $string = '';
44 2 for my $row (@$data) {
45 6 ▶ $csv->combine(@$row) || die $csv->error_diag;
Branch: 1 branch
BranchTrueFalseTotal
$csv->combine(@$row) or die $csv->error_diag 6 0 6
46 6 $string .= $csv->string . "\n";
47 }
48 2 return $string;
49 }
50  
51 3 ▶sub _to_txt {
Subroutine _to_txt: called CC=3 SCAR=11.0
52 3 my ($c, $data) = @_;
53 3 ▶ if (!$c->stash('reply_table.tablify') && eval{ require Text::Table::Tiny; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c->stash('reply_table.tablify') and eval { do { require Text::Table::Tiny; 1 } }) { } 2 1 3
Condition66%2/3
not $c->stash("reply_table.tablify") and eval { do { require Text::Table::Tiny; 1 } }
!ll&&!rl&&r
102
MC/DC50%1/2
not $c->stash("reply_table.tablify") and eval { do { require Text::Table::Tiny; 1 } }
not $c->stash("reply_table.tablify") eval { do { require Text::Table::Tiny; 1 } }
Truth table66%2/3
not(A) and B
A= not $c->stash("reply_table.tablify")B= eval { do { require Text::Table::Tiny; 1 } }
ABresult
0X0
100
111
54 2 return Text::Table::Tiny::table(
55 rows => $data,
56 header_row => $c->stash('reply_table.header_row'),
57 separate_rows => $c->stash('reply_table.separate_rows'),
58 );
59 } else {
60 1 return Mojo::Util::tablify($data);
61 }
62 }
63  
64 2 ▶sub _to_xls {
Subroutine _to_xls: called CC=3 SCAR=11.4
65 2 my ($c, $data) = @_;
66 2 ▶ unless (eval{ require Spreadsheet::WriteExcel; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Spreadsheet::WriteExcel; 1 } }) 0 2 2
67 0 $c->rendered(406);
68 0 return '';
69 }
70 2 ▶ open my $xfh, '>', \my $fdata or die "Failed to open filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $xfh, ">", \my $fdata or die "Failed to open filehandle: $!" 2 0 2
71 2 my $workbook = Spreadsheet::WriteExcel->new( $xfh );
72 2 my $worksheet = $workbook->add_worksheet();
73 2 $worksheet->write_col('A1', $data);
74 2 $workbook->close();
75 2 return $fdata;
76 };
77  
78 2 ▶sub _to_xlsx {
Subroutine _to_xlsx: called CC=3 SCAR=11.4
79 2 my ($c, $data) = @_;
80 2 ▶ unless (eval{ require Excel::Writer::XLSX; 1 }) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval { do { require Excel::Writer::XLSX; 1 } }) 0 2 2
81 0 $c->rendered(406);
82 0 return '';
83 }
84 2 ▶ open my $xfh, '>', \my $fdata or die "Failed to open filehandle: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $xfh, ">", \my $fdata or die "Failed to open filehandle: $!" 2 0 2
85 2 my $workbook = Excel::Writer::XLSX->new( $xfh );
86 2 my $worksheet = $workbook->add_worksheet();
87 2 $worksheet->write_col('A1', $data);
88 2 $workbook->close();
89 2 return $fdata;
90 };
91  
92 1 ▶sub setup_types {
Subroutine setup_types: called CC=1 SCAR=0.0
Pod undocumented
93 1 my ($plugin, $app) = @_;
94 1 my $types = $app->types;
95 1 $types->type(csv => [qw{text/csv application/csv}]);
96 1 $types->type(xls => [qw{
97 application/vnd.ms-excel application/msexcel application/x-msexcel application/x-ms-excel
98 application/x-excel application/x-dos_ms_excel application/xls
99 }]);
100 1 $types->type(xlsx => ['application/vnd.openxmlformats-officedocument.spreadsheetml.sheet']);
101 }
102  
103 1 1;
104  
105 =encoding utf8
106  
107 =head1 NAME
108  
109 Mojolicious::Plugin::ReplyTable - Easily render rectangular data in many formats using Mojolicious
110  
111 =head1 SYNOPSIS
112  
113 use Mojolicious::Lite;
114 plugin 'ReplyTable';
115  
116 my $format = [format => [qw(
117 txt csv html json
118 xls xlsx
119 )]];
120 any '/table' => $format => sub {
121 my $c = shift;
122 my $data = [
123 [qw/a b c d/],
124 [qw/e f g h/],
125 ];
126 $c->reply->table($data);
127 };
128  
129 app->start;
130  
131 =head1 DESCRIPTION
132  
133 L<Mojolicious::Plugin::ReplyTable> adds the C<< reply->table >> helper which can render a table of data in one of several user-selected formats.
134 The format is selected by the client via the usual Mojolicious L<Content Negotiation|Mojolicious::Guides::Rendering/"Content negotiation"> mechanisms.
135  
136 Loading the plugin also sets up several MIME types (using L<Mojolicious::Types>, see L<Mojolicious/types>), and appends the module to the list of rendering classes (See L<Mojolicious/renderer>).
137  
138 =head1 HELPERS
139  
140 =head2 reply->table
141  
142 $c->reply->table([[...], [...], ... ]]);
143 $c->reply->table($default => $data, html => sub { ... });
144  
145 Renders an arrayref of arrayrefs (the inner arrayref being a row) in one of several formats listed below.
146 An optional leading argument is used as the default format when one is not otherwise requested.
147 Optional trailing key-value pairs are merged into the arguments to L<Mojolicious::Controller/respond_to>.
148  
149 Any additional options, particularly those governing formatting details, are via stash keys prefixed by C<reply_table.>.
150 Note that the prefix C<reply_table.private.> is reserved for internal use.
151  
152 The formats currently include:
153  
154 =head3 csv
155  
156  
157 Implemented via L<Text::CSV> using the default values with C<binary> enabled.
158 To override these defaults set the stash key C<reply_table.csv_options> to a hashref containing attributes to pass to Text::CSV.
159 For example, to create a PSV (pipe delimited) file:
160  
161 $c->stash('reply_table.csv_options' => { sep_char => "|" });
162  
163 See L<Text::CSV/new> for available options.
164  
165 =head3 html
166  
167 Implemented via the standard L<Mojolicious> rendering functionality and a template named C<reply_table>.
168 Setting the stash key C<reply_table.header_row> to a true value will cause the default template to use the first row as header values.
169 This default template may be overloaded to change the formatting, the table is available to the template via the stash key C<reply_table.table>.
170  
171 =head3 json
172  
173 Implemented via the standard L<Mojo::JSON> handling.
174  
175 =head3 txt
176  
177 A textual representation of the table.
178 This format is intended for human consumption and the specific formatting should not be relied upon.
179  
180 If L<Text::Table::Tiny> is available, it will be used to format the data (can be overridden with C<reply_table.tablify>).
181 It can be controlled via the stash keys C<reply_table.header_row> and C<reply_table.separate_rows> as noted in that module's documentation.
182 Otherwise it is generated via L<Mojo::Util::tablify>.
183  
184 =head3 xls
185  
186 Binary Microsoft Excel format (for older editions of Excel), provided by optional module L<Spreadsheet::WriteExcel>.
187 If that module is not installed, the client will receive an error status 406.
188  
189 =head3 xlsx
190  
191 XML Microsoft Excel format (for newer editions of Excel), provided by optional module L<Excel::Writer::XLSX>.
192 If that module is not installed, the client will receive an error status 406.
193  
194 =head1 METHODS
195  
196 This module inherits all the methods from L<Mojolicious::Plugin> and implements the following new ones
197  
198 =head2 register
199  
200 The typical mechanism of loading a L<Mojolicious::Plugin>.
201 No pass-in options are currently available.
202  
203 =head1 FUTURE WORK
204  
205 Beyond what is mentioned in the specifc formats above, the following work is planned.
206 If any of it tickles your fancy, pull-requests are always welcome.
207  
208 =over
209  
210 =item *
211  
212 Better tests for generated Excel documents.
213  
214 =item *
215  
216 Exposing the formatters so that they can be used directly.
217  
218 =item *
219  
220 Add additional formats, like OpenOffice/LibreOffice.
221 If needed these can be appended via additional handlers to the helper.
222  
223 =back
224  
225 =head1 A NOTE ON FORMAT DETECTION
226  
227 As of L<Mojolicious> version 9.11, format detection is disabled by default.
228 To enable it you can pass an array reference of C<< [format=>\@formats] >> to the route, where C<@formats> is the supported file extensions.
229 You may also use the shortcut C< [format => 1] >> to enable detection of any format, though that may change in the future.
230  
231 As of Mojolicious 9.16 you can inherit these formats from a parent route:
232  
233 my $with_formats = $app->routes->any([format => \@formats]);
234 $with_formats->get('/data')->to('MyController#my_action');
235  
236 =head1 SEE ALSO
237  
238 =over
239  
240 =item L<Mojolicious>
241  
242 =item L<https://metacpan.org/pod/Mojolicious::Plugin::WriteExcel>
243  
244 =back
245  
246 =head1 SOURCE REPOSITORY
247  
248 L<http://github.com/jberger/Mojolicious-Plugin-ReplyTable>
249  
250 =head1 SPECIAL THANKS
251  
252 Pharmetika Software, L<http://pharmetika.com>
253  
254 =head1 AUTHOR
255  
256 Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
257  
258 =head1 CONTRIBUTORS
259  
260 =over
261  
262 Nils Diewald (Akron)
263  
264 Красимир Беров (kberov)
265  
266 Ryan Perry
267  
268 Ilya Chesnokov (ichesnokov)
269  
270 =back
271  
272 =head1 COPYRIGHT AND LICENSE
273  
274 Copyright (C) 2015 by L</AUTHOR> and L</CONTRIBUTORS>.
275 This library is free software; you can redistribute it and/or modify
276 it under the same terms as Perl itself.
277  
278 =cut
279  
280 __DATA__
↑ Summary