| 1 | # ABSTRACT: App::Spec Plugin for formatting data structures | ||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | package App::Spec::Plugin::Format; | ||||||||||||||||||||||
| 5 | 5 | our $VERSION = 'v0.15.1'; # TRIAL VERSION | |||||||||||||||||||||
| 6 | |||||||||||||||||||||||
| 7 | 5 | ▶ | use YAML::PP; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | 5 | ▶ | use Ref::Util qw/ is_arrayref /; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 9 | 5 | ▶ | use Encode; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 5 | ▶ | use Moo; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 12 | 5 | with 'App::Spec::Role::Plugin::GlobalOptions'; | |||||||||||||||||||||
| 13 | |||||||||||||||||||||||
| 14 | 5 | my $yaml; | |||||||||||||||||||||
| 15 | my $options; | ||||||||||||||||||||||
| 16 | 29 | ▶ | sub _read_data { | ||||||||||||||||||||
|
Subroutine
_read_data: called CC=2 SCAR=6.9
| |||||||||||||||||||||||
| 17 | 29 | ▶ | unless ($yaml) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 18 | 4 | $yaml = do { local $/; <DATA> }; | |||||||||||||||||||||
| 19 | 4 | ($options) = YAML::PP::Load($yaml); | |||||||||||||||||||||
| 20 | } | ||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | |||||||||||||||||||||||
| 24 | 29 | ▶ | sub install_options { | ||||||||||||||||||||
|
Subroutine
install_options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 25 | 29 | my ($class, %args) = @_; | |||||||||||||||||||||
| 26 | 29 | _read_data(); | |||||||||||||||||||||
| 27 | 29 | return $options; | |||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | 28 | ▶ | sub init_run { | ||||||||||||||||||||
|
Subroutine
init_run: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 31 | 28 | my ($self, $run) = @_; | |||||||||||||||||||||
| 32 | 28 | $run->subscribe( | |||||||||||||||||||||
| 33 | print_output => { | ||||||||||||||||||||||
| 34 | plugin => $self, | ||||||||||||||||||||||
| 35 | method => "print_output", | ||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||
| 38 | } | ||||||||||||||||||||||
| 39 | |||||||||||||||||||||||
| 40 | 2 | ▶ | sub print_output { | ||||||||||||||||||||
|
Subroutine
print_output: called CC=9 SCAR=37.4
Pod
documented
| |||||||||||||||||||||||
| 41 | 2 | my ($self, %args) = @_; | |||||||||||||||||||||
| 42 | 2 | my $run = $args{run}; | |||||||||||||||||||||
| 43 | 2 | my $opt = $run->options; | |||||||||||||||||||||
| 44 | 2 | ▶ | my $format = $opt->{format} || ''; | ||||||||||||||||||||
|
Condition50%1/2
$opt->{'format'} || ''
MC/DC0%0/1
$opt->{'format'} || ''
$opt->{'format'}
Truth table50%1/2
A
A= $opt->{'format'}
| |||||||||||||||||||||||
| 45 | |||||||||||||||||||||||
| 46 | 2 | my $res = $run->response; | |||||||||||||||||||||
| 47 | 2 | my $outputs = $res->outputs; | |||||||||||||||||||||
| 48 | 2 | for my $out (@$outputs) { | |||||||||||||||||||||
| 49 | 2 | ▶ | next unless $out->type eq 'data'; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 50 | 0 | my $content = $out->content; | |||||||||||||||||||||
| 51 | 0 | ▶ | if ($format eq 'YAML') { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 52 | 0 | $content = encode_utf8 YAML::PP::Dump($content); | |||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||
| 54 | elsif ($format eq 'JSON') { | ||||||||||||||||||||||
| 55 | 0 | require JSON::XS; | |||||||||||||||||||||
| 56 | 0 | my $coder = JSON::XS->new->ascii->pretty->allow_nonref; | |||||||||||||||||||||
| 57 | 0 | $content = encode_utf8 $coder->encode($content) . "\n"; | |||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||
| 59 | ▶ | elsif ($format eq 'Table' and is_arrayref($content)) { | |||||||||||||||||||||
|
Condition0%0/3
$format eq "Table" and is_arrayref($content)
MC/DC0%0/2
$format eq "Table" and is_arrayref($content)
$format eq "Table"
is_arrayref($content)
Truth table0%0/3
A and B
A= $format eq "Table"B= is_arrayref($content)
| |||||||||||||||||||||||
| 60 | 0 | require Text::Table; | |||||||||||||||||||||
| 61 | 0 | my $header = shift @$content; | |||||||||||||||||||||
| 62 | 0 | my $tb = Text::Table->new( @$header ); | |||||||||||||||||||||
| 63 | 0 | $tb->load(@$content); | |||||||||||||||||||||
| 64 | 0 | $content = encode_utf8 "$tb"; | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | elsif ($format eq 'Data::Dump') { | ||||||||||||||||||||||
| 67 | 0 | require Data::Dump; | |||||||||||||||||||||
| 68 | 0 | $content = Data::Dump::dump($content) . "\n"; | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | else { | ||||||||||||||||||||||
| 71 | 0 | $content = Data::Dumper->Dump([$content], ['output']); | |||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||
| 73 | 0 | $out->content( $content ); | |||||||||||||||||||||
| 74 | 0 | $out->type( "plain" ); | |||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||
| 76 | |||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | |||||||||||||||||||||||
| 80 | 5 | 1; | |||||||||||||||||||||
| 81 | |||||||||||||||||||||||
| 82 | =pod | ||||||||||||||||||||||
| 83 | |||||||||||||||||||||||
| 84 | =head1 NAME | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | App::Spec::Plugin::Format - App::Spec Plugin for formatting data structures | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | =head1 DESCRIPTION | ||||||||||||||||||||||
| 89 | |||||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | =head1 METHODS | ||||||||||||||||||||||
| 92 | |||||||||||||||||||||||
| 93 | =over 4 | ||||||||||||||||||||||
| 94 | |||||||||||||||||||||||
| 95 | =item install_options | ||||||||||||||||||||||
| 96 | |||||||||||||||||||||||
| 97 | This method is required by L<App::Spec::Role::Plugin::GlobalOptions>. | ||||||||||||||||||||||
| 98 | |||||||||||||||||||||||
| 99 | See L<App::Spec::Role::Plugin::GlobalOptions#install_options>. | ||||||||||||||||||||||
| 100 | |||||||||||||||||||||||
| 101 | =item init_run | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | See L<App::Spec::Role::Plugin> | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | =item print_output | ||||||||||||||||||||||
| 106 | |||||||||||||||||||||||
| 107 | This method is called by L<App::Spec::Run> right before output. | ||||||||||||||||||||||
| 108 | |||||||||||||||||||||||
| 109 | =back | ||||||||||||||||||||||
| 110 | |||||||||||||||||||||||
| 111 | =cut | ||||||||||||||||||||||
| 112 | |||||||||||||||||||||||
| 113 | __DATA__ | ||||||||||||||||||||||