File Coverage

blib/lib/App/Gimei/Runner.pm
Criterion Covered Total %
statement 52 52 100.0
branch 10 10 100.0
condition 3 3 100.0
subroutine 11 11 100.0
pod 0 3 0.0
total 76 79 96.2


line stmt bran cond sub pod time code
1             package App::Gimei::Runner;
2              
3 8     8   15883 use warnings;
  8         52  
  8         252  
4 8     8   82 use v5.22;
  8         26  
5             binmode STDOUT, ":utf8";
6              
7 8     8   6147 use Getopt::Long;
  8         110373  
  8         45  
8 8     8   5319 use Pod::Usage;
  8         695887  
  8         1068  
9 8     8   85 use Pod::Find qw( pod_where );
  8         19  
  8         505  
10              
11 8     8   3234 use App::Gimei;
  8         185  
  8         225  
12              
13 8     8   649 use Class::Tiny;
  8         1824  
  8         31  
14              
15             #
16             # global vars
17             #
18              
19             my %conf = ( POD_FILE => pod_where( { -inc => 1 }, 'App::Gimei' ) );
20              
21             #
22             # methods
23             #
24              
25             sub parse_option {
26 34     34 0 98 my ( $self, $args_ref, $opts_ref ) = @_;
27              
28 34         85 $opts_ref->{n} = 1;
29 34         75 $opts_ref->{sep} = ', ';
30              
31 34         259 my $p = Getopt::Long::Parser->new( config => ["no_ignore_case"], );
32              
33 34     1   3152 local $SIG{__WARN__} = sub { die "Error: $_[0]" };
  1         715  
34 34         174 my $ok = $p->getoptionsfromarray( $args_ref, $opts_ref, "help|h", "version|v", "n=i",
35             "sep=s", );
36              
37 33 100       14986 if ( $opts_ref->{n} < 1 ) {
38 1         23 die
39             "Error: value $opts_ref->{n} invalid for option n (must be positive number)\n";
40             }
41             }
42              
43             sub execute {
44 34     34 0 134623 my ( $self, @args ) = @_;
45              
46 34         75 my %opts;
47 34         141 $self->parse_option( \@args, \%opts );
48              
49 32 100       158 if ( $opts{version} ) {
50 2         132 say "$App::Gimei::VERSION";
51 2         20 return 0;
52             }
53              
54 30 100       93 if ( $opts{help} ) {
55 2         16 pod2usage( -input => $conf{POD_FILE}, -exitval => 'noexit' );
56 2         30422 return 0;
57             }
58              
59 28 100       72 if ( !@args ) {
60 3         10 push @args, 'name:kanji';
61             }
62              
63 28         111 my @generators = App::Gimei::Parser::parse_args( @args );
64            
65 25         99 semantic_analysis( @generators );
66              
67 23         433 foreach ( 1 .. $opts{n} ) {
68 24         80 my ( @words, %cache );
69 24         46 foreach my $g (@generators) {
70 25         87 push @words, $g->execute( \%cache );
71             }
72 24         1288 say join $opts{sep}, @words;
73             }
74              
75 23         955 return 0;
76             }
77              
78             sub semantic_analysis {
79 25     25 0 58 my ( @generators ) = @_;
80            
81 25         50 foreach my $gen (@generators) {
82 26 100 100     699 if ($gen->word_class eq 'Data::Gimei::Address' && $gen->render eq 'romaji') {
83 2         86 die "Error: rendering romaji is not supported for address\n";
84             }
85             }
86             }
87              
88             1;