]> Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Formatter/CSV.pm
refactor formatters
[chaz/git-codeowners] / lib / App / Codeowners / Formatter / CSV.pm
1 package App::Codeowners::Formatter::CSV;
2 # ABSTRACT: Format codeowners output as comma-separated values
3
4 =head1 DESCRIPTION
5
6 This is a L<App::Codeowners::Formatter> that formats output using L<Text::CSV>.
7
8 =cut
9
10 use warnings;
11 use strict;
12
13 our $VERSION = '9999.999'; # VERSION
14
15 use parent 'App::Codeowners::Formatter';
16
17 use App::Codeowners::Util qw(stringify);
18 use Encode qw(encode);
19
20 sub start {
21 my $self = shift;
22
23 $self->text_csv->print($self->handle, $self->columns);
24 }
25
26 sub stream {
27 my $self = shift;
28 my $result = shift;
29
30 $self->text_csv->print($self->handle, [map { encode('UTF-8', stringify($_)) } @$result]);
31 }
32
33 =attr text_csv
34
35 Get the L<Text::CSV> instance.
36
37 =cut
38
39 sub text_csv {
40 my $self = shift;
41
42 $self->{text_csv} ||= do {
43 eval { require Text::CSV } or die "Missing dependency: Text::CSV\n";
44
45 my %options;
46 $options{escape_char} = $self->escape_char if $self->escape_char;
47 $options{quote} = $self->quote if $self->quote;
48 $options{sep} = $self->sep if $self->sep;
49 if ($options{sep} && $options{sep} eq ($options{quote} || '"')) {
50 die "Invalid separator value for CSV format.\n";
51 }
52
53 Text::CSV->new({binary => 1, eol => $/, %options});
54 } or die "Failed to construct Text::CSV object";
55 }
56
57 =attr sep
58
59 Get the value used for L<Text::CSV/sep>.
60
61 =attr quote
62
63 Get the value used for L<Text::CSV/quote>.
64
65 =attr escape_char
66
67 Get the value used for L<Text::CSV/escape_char>.
68
69 =cut
70
71 sub sep { $_[0]->{sep} || $_[0]->format }
72 sub quote { $_[0]->{quote} }
73 sub escape_char { $_[0]->{escape_char} }
74
75 1;
This page took 0.036215 seconds and 4 git commands to generate.