1 package App
::Codeowners
::Formatter
;
2 # ABSTRACT: Base class for formatting codeowners output
6 my $formatter = App::Codeowners::Formatter->new(handle => *STDOUT);
7 $formatter->add_result($_) for @results;
11 This is a base class for formatters. A formatter is a class that takes data records, stringifies
12 them, and prints them to an IO handle.
14 This class is mostly abstract, though it is also usable as a null formatter where results are simply
15 discarded if it is instantiated directly. These other formatters do more interesting things:
18 * L<App::Codeowners::Formatter::CSV>
19 * L<App::Codeowners::Formatter::String>
20 * L<App::Codeowners::Formatter::JSON>
21 * L<App::Codeowners::Formatter::TSV>
22 * L<App::Codeowners::Formatter::Table>
23 * L<App::Codeowners::Formatter::YAML>
30 our $VERSION = '9999.999'; # VERSION
36 $formatter = App
::Codeowners
::Formatter-
>new;
37 $formatter = App
::Codeowners
::Formatter-
>new(%attributes);
39 Construct a new formatter
.
45 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
47 $args->{results
} = [];
49 # see if we can find a better class to bless into
50 ($class, my $format) = $class->_best_formatter($args->{format
}) if $args->{format
};
51 $args->{format
} = $format;
53 my $self = bless $args, $class;
61 # Find a formatter that can handle the format requested.
64 my $type = shift || '';
66 return ($class, $type) if $class ne __PACKAGE__
;
68 my ($name, $format) = $type =~ /^([A-Za-z]+)(?::(.*))?$/;
77 my @formatters = $class->formatters;
79 # default to the string formatter since it has no dependencies
80 my $package = __PACKAGE__
.'::String';
82 # look for a formatter whose name matches the format
83 for my $formatter (@formatters) {
84 my $module = lc($formatter);
87 if ($module eq $name) {
88 $package = $formatter;
95 return ($package, $type);
100 Destructor calls L
</finish
>.
106 my $global_destruction = shift;
108 return if $global_destruction;
110 my $results = $self->{results
};
111 $self->finish($results) if $results;
112 delete $self->{results
};
117 Get the IO handle associated with a formatter
.
121 Get the format string
, which may be used to customize the formatting
.
125 Get an arrayref of column headings
.
129 Get an arrayref of all the results that have been provided to the formatter using L
</add_result
> but
130 have
not yet been formatted
.
134 sub handle
{ shift-
>{handle
} }
135 sub format
{ shift-
>{format
} || '' }
136 sub columns
{ shift-
>{columns
} || [] }
137 sub results
{ shift-
>{results
} }
141 $formatter->add_result($result);
143 Provide an additional lint result to be formatted
.
149 $self->stream($_) for @_;
156 Begin formatting results
. Called before any results are passed to the L
</stream
> method.
158 This
method may
print a header to the L
</handle
>. This
method is used by subclasses
and should
159 typically
not be called explicitly
.
163 $formatter->stream(\
@result, ...);
167 This
method is expected to
print a string representation of the result to the L
</handle
>. This
168 method is used by subclasses
and should typically
not called be called explicitly
.
170 The
default implementation simply stores the L
</results> so they will be available to L</finish
>.
176 End formatting results
. Called after all results are passed to the L
</stream
> method.
178 This
method may
print a footer to the L
</handle
>. This
method is used by subclasses
and should
179 typically
not be called explicitly
.
184 sub stream
{ push @{$_[0]->results}, $_[1] }
189 @formatters = App
::Codeowners
::Formatter-
>formatters;
191 Get a list of
package names of potential formatters within the C
<App
::Codeowners
::Formatter
>
198 App::Codeowners::Formatter::CSV
199 App::Codeowners::Formatter::JSON
200 App::Codeowners::Formatter::String
201 App::Codeowners::Formatter::TSV
202 App::Codeowners::Formatter::Table
203 App::Codeowners::Formatter::YAML