File Coverage

blib/lib/App/Codeowners/Formatter.pm
Criterion Covered Total %
statement 52 53 98.1
branch 12 18 66.6
condition 2 5 40.0
subroutine 15 15 100.0
pod 10 10 100.0
total 91 101 90.1


line stmt bran cond sub pod time code
1             package App::Codeowners::Formatter;
2             # ABSTRACT: Base class for formatting codeowners output
3              
4              
5 1     1   5 use warnings;
  1         2  
  1         28  
6 1     1   4 use strict;
  1         2  
  1         27  
7              
8             our $VERSION = '0.51'; # VERSION
9              
10 1     1   383 use Module::Load;
  1         940  
  1         4  
11              
12              
13             sub new {
14 3     3 1 18 my $class = shift;
15 3 50 33     70 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
  0         0  
16              
17 3         23 $args->{results} = [];
18              
19             # see if we can find a better class to bless into
20 3 50       113 ($class, my $format) = $class->_best_formatter($args->{format}) if $args->{format};
21 3         12 $args->{format} = $format;
22              
23 3         18 my $self = bless $args, $class;
24              
25 3         42 $self->start;
26              
27 3         10 return $self;
28             }
29              
30             ### _best_formatter
31             # Find a formatter that can handle the format requested.
32             sub _best_formatter {
33 3     3   17 my $class = shift;
34 3   50     17 my $type = shift || '';
35              
36 3 50       20 return ($class, $type) if $class ne __PACKAGE__;
37              
38 3         45 my ($name, $format) = $type =~ /^([A-Za-z]+)(?::(.*))?$/;
39 3 100       13 if (!$name) {
40 2         5 $name = '';
41 2         7 $format = '';
42             }
43              
44 3         9 $name = lc($name);
45 3         10 $name =~ s/:.*//;
46              
47 3         35 my @formatters = $class->formatters;
48              
49             # default to the string formatter since it has no dependencies
50 3         10 my $package = __PACKAGE__.'::String';
51              
52             # look for a formatter whose name matches the format
53 3         16 for my $formatter (@formatters) {
54 14         44 my $module = lc($formatter);
55 14         60 $module =~ s/.*:://;
56              
57 14 100       51 if ($module eq $name) {
58 1         10 $package = $formatter;
59 1         8 $type = $format;
60 1         5 last;
61             }
62             }
63              
64 3         26 load $package;
65 3         157 return ($package, $type);
66             }
67              
68              
69             sub DESTROY {
70 3     3   216 my $self = shift;
71 3         16 my $global_destruction = shift;
72              
73 3 50       7 return if $global_destruction;
74              
75 3         16 my $results = $self->{results};
76 3 50       34 $self->finish($results) if $results;
77 3         54 delete $self->{results};
78             }
79              
80              
81 7     7 1 520 sub handle { shift->{handle} }
82 7 100   7 1 88 sub format { shift->{format} || '' }
83 7 50   7 1 67 sub columns { shift->{columns} || [] }
84 3     3 1 28 sub results { shift->{results} }
85              
86              
87             sub add_result {
88 9     9 1 21 my $self = shift;
89 9         46 $self->stream($_) for @_;
90             }
91              
92              
93       3 1   sub start {}
94 3     3 1 15 sub stream { push @{$_[0]->results}, $_[1] }
  3         23  
95       2 1   sub finish {}
96              
97              
98             sub formatters {
99 3     3 1 34 return qw(
100             App::Codeowners::Formatter::CSV
101             App::Codeowners::Formatter::JSON
102             App::Codeowners::Formatter::String
103             App::Codeowners::Formatter::TSV
104             App::Codeowners::Formatter::Table
105             App::Codeowners::Formatter::YAML
106             );
107             }
108              
109             1;
110              
111             __END__