File Coverage

blib/lib/PPIx/EditorTools/RenamePackageFromPath.pm
Criterion Covered Total %
statement 45 45 100.0
branch 5 6 83.3
condition 1 3 33.3
subroutine 11 11 100.0
pod 1 1 100.0
total 63 66 95.4


line stmt bran cond sub pod time code
1             package PPIx::EditorTools::RenamePackageFromPath;
2              
3             # ABSTRACT: Change the package name based on the files path
4              
5 2     2   256600 use 5.008;
  2         7  
  2         90  
6 2     2   11 use strict;
  2         5  
  2         76  
7 2     2   14 use warnings;
  2         4  
  2         70  
8 2     2   16 use Carp;
  2         3  
  2         198  
9              
10 2         21 use Class::XSAccessor accessors => {
11             'replacement' => 'replacement',
12             'filename' => 'filename',
13 2     2   5265 };
  2         11601  
14              
15 2     2   527 use base 'PPIx::EditorTools';
  2         7  
  2         1038  
16 2     2   899 use PPIx::EditorTools::RenamePackage;
  2         5  
  2         58  
17 2     2   12 use Carp;
  2         5  
  2         117  
18 2     2   12 use File::Spec;
  2         3  
  2         49  
19 2     2   11 use File::Basename;
  2         5  
  2         928  
20              
21             our $VERSION = '0.18';
22              
23             =pod
24              
25             =head1 NAME
26              
27             PPIx::EditorTools::RenamePackageFromPath -Change the package name based on the files path
28              
29             =head1 SYNOPSIS
30              
31             my $munged = PPIx::EditorTools::RenamePackageFromPath->new->rename(
32             code => "package TestPackage;\nuse strict;\nBEGIN {
33             $^W = 1;
34             }\n1;\n",
35             filename => './lib/Test/Code/Path.pm',
36             );
37              
38             my $new_code_as_string = $munged->code;
39             my $package_ppi_element = $munged->element;
40              
41             =head1 DESCRIPTION
42              
43             This module uses PPI to change the package name of code.
44              
45             =head1 METHODS
46              
47             =over 4
48              
49             =item new()
50              
51             Constructor. Generally shouldn't be called with any arguments.
52              
53             =item rename( ppi => PPI::Document $ppi, filename => Str )
54             =item rename( code => Str $code, filename => Str )
55              
56             Accepts either a C to process or a string containing
57             the code (which will be converted into a C) to process.
58             Replaces the package name with that supplied in the C
59             parameter and returns a C with the
60             new code available via the C or C accessors, as a
61             C or C, respectively.
62              
63             An attempt will be made to derive the package name from the filename passed
64             as a parameter. The filename's path will converted to an absolute path and
65             it will be searched for a C directory which will be assumed the start
66             of the package name. If no C directory can be found in the absolute
67             path, the relative path will be used.
68              
69             Croaks with a "package name not found" exception if unable to find the
70             package name.
71              
72             =back
73              
74             =cut
75              
76             sub rename {
77 4     4 1 3575 my ( $self, %args ) = @_;
78 4         22 $self->process_doc(%args);
79 4   33     18 my $path = $args{filename} || croak "filename required";
80              
81 4         188 my $dir = dirname $path;
82 4         280 my $file = basename $path, qw/.pm .PM .Pm/;
83              
84 29 100       99 my @directories =
85 4         154 grep { $_ && !/^\.$/ } File::Spec->splitdir( File::Spec->rel2abs($dir) );
86 4         6 my $replacement;
87 4 100       8 if ( grep {/^lib$/} @directories ) {
  25         46  
88 3         22 while ( shift(@directories) !~ /^lib$/ ) { }
89             } else {
90 1 50       9 @directories = grep { $_ && !/^\.$/ } File::Spec->splitdir($dir);
  3         18  
91             }
92 4         13 $replacement = join( '::', @directories, $file );
93              
94 4         72 return PPIx::EditorTools::RenamePackage->new( ppi => $self->ppi )->rename( replacement => $replacement );
95              
96             }
97              
98             1;
99              
100             __END__