| 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 |  | 7 | use warnings; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 32 |  | 
| 6 | 1 |  |  | 1 |  | 6 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 33 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 |  |  |  |  |  |  | our $VERSION = '0.49'; # VERSION | 
| 9 |  |  |  |  |  |  |  | 
| 10 | 1 |  |  | 1 |  | 473 | use Module::Load; | 
|  | 1 |  |  |  |  | 1090 |  | 
|  | 1 |  |  |  |  | 5 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub new { | 
| 14 | 3 |  |  | 3 | 1 | 44 | my $class = shift; | 
| 15 | 3 | 50 | 33 |  |  | 95 | my $args  = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_}; | 
|  | 0 |  |  |  |  | 0 |  | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 3 |  |  |  |  | 41 | $args->{results} = []; | 
| 18 |  |  |  |  |  |  |  | 
| 19 |  |  |  |  |  |  | # see if we can find a better class to bless into | 
| 20 | 3 | 50 |  |  |  | 34 | ($class, my $format) = $class->_best_formatter($args->{format}) if $args->{format}; | 
| 21 | 3 |  |  |  |  | 13 | $args->{format} = $format; | 
| 22 |  |  |  |  |  |  |  | 
| 23 | 3 |  |  |  |  | 22 | my $self = bless $args, $class; | 
| 24 |  |  |  |  |  |  |  | 
| 25 | 3 |  |  |  |  | 41 | $self->start; | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 3 |  |  |  |  | 11 | return $self; | 
| 28 |  |  |  |  |  |  | } | 
| 29 |  |  |  |  |  |  |  | 
| 30 |  |  |  |  |  |  | ### _best_formatter | 
| 31 |  |  |  |  |  |  | #   Find a formatter that can handle the format requested. | 
| 32 |  |  |  |  |  |  | sub _best_formatter { | 
| 33 | 3 |  |  | 3 |  | 8 | my $class = shift; | 
| 34 | 3 |  | 50 |  |  | 12 | my $type  = shift || ''; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 3 | 50 |  |  |  | 15 | return ($class, $type) if $class ne __PACKAGE__; | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 3 |  |  |  |  | 47 | my ($name, $format) = $type =~ /^([A-Za-z]+)(?::(.*))?$/; | 
| 39 | 3 | 100 |  |  |  | 11 | if (!$name) { | 
| 40 | 2 |  |  |  |  | 5 | $name   = ''; | 
| 41 | 2 |  |  |  |  | 4 | $format = ''; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 3 |  |  |  |  | 6 | $name = lc($name); | 
| 45 | 3 |  |  |  |  | 17 | $name =~ s/:.*//; | 
| 46 |  |  |  |  |  |  |  | 
| 47 | 3 |  |  |  |  | 28 | 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 |  |  |  |  | 23 | for my $formatter (@formatters) { | 
| 54 | 14 |  |  |  |  | 37 | my $module = lc($formatter); | 
| 55 | 14 |  |  |  |  | 61 | $module =~ s/.*:://; | 
| 56 |  |  |  |  |  |  |  | 
| 57 | 14 | 100 |  |  |  | 42 | if ($module eq $name) { | 
| 58 | 1 |  |  |  |  | 18 | $package = $formatter; | 
| 59 | 1 |  |  |  |  | 8 | $type    = $format; | 
| 60 | 1 |  |  |  |  | 7 | last; | 
| 61 |  |  |  |  |  |  | } | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 | 3 |  |  |  |  | 16 | load $package; | 
| 65 | 3 |  |  |  |  | 201 | return ($package, $type); | 
| 66 |  |  |  |  |  |  | } | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub DESTROY { | 
| 70 | 3 |  |  | 3 |  | 10 | my $self = shift; | 
| 71 | 3 |  |  |  |  | 6 | my $global_destruction = shift; | 
| 72 |  |  |  |  |  |  |  | 
| 73 | 3 | 50 |  |  |  | 9 | return if $global_destruction; | 
| 74 |  |  |  |  |  |  |  | 
| 75 | 3 |  |  |  |  | 5 | my $results = $self->{results}; | 
| 76 | 3 | 50 |  |  |  | 46 | $self->finish($results) if $results; | 
| 77 | 3 |  |  |  |  | 59 | delete $self->{results}; | 
| 78 |  |  |  |  |  |  | } | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  |  | 
| 81 | 7 |  |  | 7 | 1 | 645 | sub handle  { shift->{handle}  } | 
| 82 | 7 | 100 |  | 7 | 1 | 73 | sub format  { shift->{format}  || '' } | 
| 83 | 7 | 50 |  | 7 | 1 | 72 | sub columns { shift->{columns} || [] } | 
| 84 | 3 |  |  | 3 | 1 | 41 | sub results { shift->{results} } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  |  | 
| 87 |  |  |  |  |  |  | sub add_result { | 
| 88 | 9 |  |  | 9 | 1 | 21 | my $self = shift; | 
| 89 | 9 |  |  |  |  | 63 | $self->stream($_) for @_; | 
| 90 |  |  |  |  |  |  | } | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  | 3 | 1 |  | sub start  {} | 
| 94 | 3 |  |  | 3 | 1 | 19 | sub stream { push @{$_[0]->results}, $_[1] } | 
|  | 3 |  |  |  |  | 32 |  | 
| 95 |  |  |  | 2 | 1 |  | sub finish {} | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | sub formatters { | 
| 99 | 3 |  |  | 3 | 1 | 39 | 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__ |