blib/lib/File/Corresponding/File/Profile.pm untested

filter: Statementstmt 0.0% 0 / 43 Branchbran n/a Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 63 CC 1 SCAR 6.9
CC
1
Cov
0%
matching_file_fragment
2.0
new_found_if_file_exists
2.0
regex_from_qr
2.0
CRAP 2.0
1  
2 =head1 NAME
3  
4 File::Corresponding::File::Profile - The definition of what matches
5 and translates to corresponding files
6  
7 =cut
8  
9 use strict;
10 package File::Corresponding::File::Profile;
11 use Moose;
12  
13 use Moose::Util::TypeConstraints;
14 use Data::Dumper;
15 use Path::Class;
16  
17 use File::Corresponding::File::Found;
18  
19  
20  
21 =head1 PROPERTIES
22  
23 =head2 name
24  
25 Name/description of this file profile.
26  
27 =cut
28 has 'name' => (is => 'ro', isa => 'Str', default => "");
29  
30  
31  
32  
33 =head2 sprintf
34  
35 sprintf string to construct a file name. It should contain at least
36 one % command to insert a relative file name.
37  
38 Only used if defined.
39  
40 =cut
41 has 'sprintf' => (is => 'ro', isa => 'Maybe[Str]');
42  
43  
44  
45 =head2 regex : RegexRef
46  
47 Regex matching a file. The first capture parens are used to extract
48 the local file name.
49  
50 If coerced from a string, define as qr$regex, i.e. specify the
51 delimiters and any needed flags.
52  
53 =cut
54  
55 subtype RegexRef
56 => as RegexpRef
57 => where { ref($_) eq "Regexp" }; #print "JPL: where: ($_) (" . ref($_) . ")\n";
58 coerce RegexRef
59 => from 'Str'
60 => via { regex_from_qr($_) };
61  
62 has 'regex' => (
63 is => 'rw',
64 isa => 'RegexRef',
65 coerce => 1,
66 required => 1,
67 );
68  
69  
70  
71 =head1 METHODS
72  
73 =head2 matching_file_fragment($file) : ($file_base, $file_fragment) | ()
74  
75 Return two item list with (the base filename, the captured file name
76 fragment) from matching $file against regex, or () if nothing matched.
77  
78 The $file_base is the $file, but with the whole matching regex
79 removed, forming the basis for looking up corresponding files.
80  
81 =cut
82  
83 sub matching_file_fragment {
84 my $self = shift;
85 my ($file) = @_;
86 my $regex = $self->regex;
87
88 my $file_base = $file;
89 $file_base =~ s/$regex// and return ($file_base, $1);
90
91 return ();
92 }
93  
94  
95  
96 =head2 new_found_if_file_exists($matching_profile, $file_base, $fragment) : File::Found | ()
97  
98 Return a new File::Corresponding::File::Found object if a file made up
99 of $file_base, this profile, and $fragment exists in the filesystem.
100  
101 If not, return ().
102  
103 =cut
104  
105 sub new_found_if_file_exists {
106 my $self = shift;
107 my ($matching_profile, $file_base, $fragment) = @_;
108 my $sprintf = $self->sprintf or return ();
109  
110 my $file = file($file_base, sprintf($sprintf, $fragment));
111  
112 -e $file or return ();
113
114 return File::Corresponding::File::Found->new({
115 file => $file,
116 matching_profile => $matching_profile,
117 found_profile => $self,
118 });
119 }
120  
121  
122  
123 =head1 SUBROUTINES
124  
125 =head2 rex_from_qr($rex_string) : RegexRef
126  
127 Convert $rex_string to a proper Regex ref, or die with a useful error
128 message.
129  
130 =cut
131 sub regex_from_qr {
132 my ($rex_string) = @_;
133 my $rex = eval "qr $rex_string";
134 $@ and die("Could not parse regexp ($rex_string):
135 $@
136 Correct regex syntax is e.g. '/ prove [.] bat /x'
137 ");
138 return $rex;
139 }
140  
141  
142  
143 1;
144  
145  
146  
147 __END__