File Coverage

blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm
Criterion Covered Total %
statement 44 44 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 11 11 100.0
pod 1 1 100.0
total 62 65 95.3


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::RenamePackageFromPath;
2             our $AUTHORITY = 'cpan:YANICK';
3             # ABSTRACT: Change the package name based on the files path
4             $PPIx::EditorTools::RenamePackageFromPath::VERSION = '0.20';
5 2     2   149616 use 5.008;
  2         13  
6 2     2   9 use strict;
  2         2  
  2         38  
7 2     2   8 use warnings;
  2         4  
  2         46  
8 2     2   8 use Carp;
  2         3  
  2         127  
9              
10 2         14 use Class::XSAccessor accessors => {
11             'replacement' => 'replacement',
12             'filename' => 'filename',
13 2     2   252 };
  2         1588  
14              
15 2     2   416 use base 'PPIx::EditorTools';
  2         4  
  2         398  
16 2     2   244 use PPIx::EditorTools::RenamePackage;
  2         4  
  2         42  
17 2     2   9 use Carp;
  2         4  
  2         71  
18 2     2   9 use File::Spec;
  2         4  
  2         53  
19 2     2   9 use File::Basename;
  2         3  
  2         474  
20              
21              
22             sub rename {
23 4     4 1 3107 my ( $self, %args ) = @_;
24 4         19 $self->process_doc(%args);
25 4   33     14 my $path = $args{filename} || croak "filename required";
26              
27 4         121 my $dir = dirname $path;
28 4         197 my $file = basename $path, qw/.pm .PM .Pm/;
29              
30             my @directories =
31 4 100       102 grep { $_ && !/^\.$/ } File::Spec->splitdir( File::Spec->rel2abs($dir) );
  33         119  
32 4         7 my $replacement;
33 4 100       6 if ( grep {/^lib$/} @directories ) {
  29         53  
34 3         18 while ( shift(@directories) !~ /^lib$/ ) { }
35             } else {
36 1 50       4 @directories = grep { $_ && !/^\.$/ } File::Spec->splitdir($dir);
  3         14  
37             }
38 4         10 $replacement = join( '::', @directories, $file );
39              
40 4         32 return PPIx::EditorTools::RenamePackage->new( ppi => $self->ppi )->rename( replacement => $replacement );
41              
42             }
43              
44             1;
45              
46             __END__
47              
48             =pod
49              
50             =encoding UTF-8
51              
52             =head1 NAME
53              
54             PPIx::EditorTools::RenamePackageFromPath - Change the package name based on the files path
55              
56             =head1 VERSION
57              
58             version 0.20
59              
60             =head1 SYNOPSIS
61              
62             my $munged = PPIx::EditorTools::RenamePackageFromPath->new->rename(
63             code => "package TestPackage;\nuse strict;\nBEGIN {
64             $^W = 1;
65             }\n1;\n",
66             filename => './lib/Test/Code/Path.pm',
67             );
68              
69             my $new_code_as_string = $munged->code;
70             my $package_ppi_element = $munged->element;
71              
72             =head1 DESCRIPTION
73              
74             This module uses PPI to change the package name of code.
75              
76             =head1 METHODS
77              
78             =over 4
79              
80             =item new()
81              
82             Constructor. Generally shouldn't be called with any arguments.
83              
84             =item rename( ppi => PPI::Document $ppi, filename => Str )
85              
86             =item rename( code => Str $code, filename => Str )
87              
88             Accepts either a C<PPI::Document> to process or a string containing
89             the code (which will be converted into a C<PPI::Document>) to process.
90             Replaces the package name with that supplied in the C<filename>
91             parameter and returns a C<PPIx::EditorTools::ReturnObject> with the
92             new code available via the C<ppi> or C<code> accessors, as a
93             C<PPI::Document> or C<string>, respectively.
94              
95             An attempt will be made to derive the package name from the filename passed
96             as a parameter. The filename's path will converted to an absolute path and
97             it will be searched for a C<lib> directory which will be assumed the start
98             of the package name. If no C<lib> directory can be found in the absolute
99             path, the relative path will be used.
100              
101             Croaks with a "package name not found" exception if unable to find the
102             package name.
103              
104             =back
105              
106             =head1 SEE ALSO
107              
108             This class inherits from C<PPIx::EditorTools>.
109             Also see L<App::EditorTools>, L<Padre>, and L<PPI>.
110              
111             =head1 AUTHORS
112              
113             =over 4
114              
115             =item *
116              
117             Steffen Mueller C<smueller@cpan.org>
118              
119             =item *
120              
121             Mark Grimes C<mgrimes@cpan.org>
122              
123             =item *
124              
125             Ahmad M. Zawawi <ahmad.zawawi@gmail.com>
126              
127             =item *
128              
129             Gabor Szabo <gabor@szabgab.com>
130              
131             =item *
132              
133             Yanick Champoux <yanick@cpan.org>
134              
135             =back
136              
137             =head1 COPYRIGHT AND LICENSE
138              
139             This software is copyright (c) 2017, 2014, 2012 by The Padre development team as listed in Padre.pm..
140              
141             This is free software; you can redistribute it and/or modify it under
142             the same terms as the Perl 5 programming language system itself.
143              
144             =cut