File Coverage

blib/lib/Mojolicious/Command/Author/generate/role.pm
Criterion Covered Total %
statement 20 20 100.0
branch 5 6 83.3
condition 12 13 92.3
subroutine 3 3 100.0
pod 1 1 100.0
total 41 43 95.3


line stmt bran cond sub pod time code
1             package Mojolicious::Command::Author::generate::role 0.002;
2              
3 1     1   558 use Mojo::Base 'Mojolicious::Command';
  1         2  
  1         9  
4              
5 1     1   224 use Mojo::Util qw(camelize class_to_path getopt);
  1         2  
  1         365  
6              
7             has description => 'Generate Mojolicious role directory structure';
8             has usage => sub { shift->extract_usage };
9              
10             sub run {
11 3     3 1 63862 my ( $self, @args ) = @_;
12              
13 3         19 getopt \@args, 'f|full' => \( my $full );
14              
15             # Class
16 3   100     12808 my $name = pop @args // 'MyRole';
17 3         10 my $consumer = pop @args;
18 3 100 100     26 my $class = $consumer && !$full ? "${consumer}::Role::${name}" : $name;
19 3 50 66     34 ( $consumer, $name ) = ( $1, $2 )
20             if ( !$consumer && $class =~ m/^(.*)::Role::(.*)/ );
21 3 100 100     38 my $sname = $consumer && $class =~ m/^${consumer}::Role::(.*)/ ? "+$1" : $class;
22 3   100     15 $consumer //= 'Mojo::Base';
23              
24 3         15 my $dir = join '-', split( '::', $class );
25 3         15 my $app = class_to_path $class;
26             my $vars = {
27             class => $class,
28 3         36 use_consumer => !exists $INC{class_to_path($consumer)},
29             consumer => $consumer,
30             name => $name,
31             sname => $sname,
32             path => $app
33             };
34 3         70 $self->render_to_rel_file( 'class', "$dir/lib/$app", $vars );
35              
36             # Test
37 3         17011 $self->render_to_rel_file( 'test', "$dir/t/basic.t", $vars );
38              
39             # Makefile
40 3         7803 $self->render_to_rel_file( 'makefile', "$dir/Makefile.PL", $vars );
41             }
42              
43             1;
44              
45             =encoding utf8
46              
47             =head1 NAME
48              
49             Mojolicious::Command::Author::generate::role - Role generator command
50              
51             =head1 VERSION
52              
53             version 0.002
54              
55             =head1 SYNOPSIS
56              
57             Usage: APPLICATION generate role [OPTIONS] [[CONSUMER_CLASS] NAME]
58              
59             mojo generate role
60             mojo generate role MyRole
61             mojo generate role Mojo::Whatever MyRole
62             mojo generate role Mojo::Whatever::Role::MyRole
63             mojo generate role -f Mojo::Whatever MyRole
64              
65             Options:
66             -f, --full NAME is the full role name (omit 'CONSUMER_CLASS::Role::')
67             -h, --help Show this summary of available options
68              
69             =head1 DESCRIPTION
70              
71             L generates directory structures
72             for fully functional roles to use with L objects/classes.
73              
74             If the intended consumer class is not specified it will be inferred from the
75             role name given (whatever precedes C<::Role::> in the name) or otherwise
76             default to L.
77              
78             =head1 ATTRIBUTES
79              
80             L inherits all attributes from
81             L and implements the following new ones:
82              
83             =head2 description
84              
85             my $description = $role_cmd->description;
86             $role_cmd = $role_cmd->description('Foo');
87              
88             Short description of this command, used for the command list.
89              
90             =head2 usage
91              
92             my $usage = $role_cmd->usage;
93             $role_cmd = $role_cmd->usage('Foo');
94              
95             Usage information for this command, used for the help screen.
96              
97             =head1 METHODS
98              
99             L inherits all methods from
100             L and implements the following new ones.
101              
102             =head2 run
103              
104             $role_cmd->run(@ARGV);
105              
106             Run this command.
107              
108             =head1 COPYRIGHT AND LICENSE
109              
110             Copyright (C) 2019, Roger Crew.
111              
112             This program is free software, you can redistribute it and/or
113             modify it under the terms of the Artistic License version 2.0.
114              
115             =head1 SEE ALSO
116              
117             L, L, L.
118              
119             =cut
120              
121             __DATA__