Format/Module.pm

filter: Statementstmt 92.8% 104 / 112 Branchbran 65.0% 13 / 20 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 28 / 28 Podpod 100% 7 / 7 totaltotal 91.1% 155 / 170 CC 14 SCAR 7.3
CC
14
Cov
89%
from_guess
5.0
_calc_components_from_string
3.3
_all
3.0
CRAP 2.1
« Format/PerlMF_App.pm ↑ Summary Format/ModuleList.pm »
1 package Module::Format::Module;
2 7 $Module::Format::Module::VERSION = '0.4.0';
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6  
7 sub _new
8 22 ▶{
Subroutine _new: called CC=1 SCAR=0.0
9 22 my $class = shift;
10 22 my $self = bless {}, $class;
11 22 $self->_init(@_);
12 22 return $self;
13 }
14  
15 sub _components
16 84 ▶{
Subroutine _components: called CC=2 SCAR=6.9
17 84 my $self = shift;
18  
19 84 ▶ if (@_)
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 22 62 84
20 {
21 22 $self->{_components} = shift;
22 }
23 84 return $self->{_components};
24 }
25  
26 sub _init
27 22 ▶{
Subroutine _init: called CC=1 SCAR=0.0
28 22 my ( $self, $args ) = @_;
29  
30 22 $self->_components( [ @{ $args->{_components} } ] );
31  
32 22 return;
33 }
34  
35  
36 sub from_components
37 4 ▶{
Subroutine from_components: called CC=1 SCAR=0.0
Pod documented
38 4 my ( $class, $args ) = @_;
39  
40 4 return $class->_new( { _components => [ @{ $args->{components} } ] } );
41 }
42  
43  
44 7 my $dash_re = qr{(?:\w+-)*\w+};
45 7 my $colon_re = qr{(?:\w+::)*\w+};
46 7 my $METACPAN_REL = 'https://metacpan.org/release/';
47  
48 sub _gen_dash_format
49 21 ▶{
Subroutine _gen_dash_format: called CC=1 SCAR=0.0
50 21 my ($args) = @_;
51  
52 21 my $prefix = $args->{prefix};
53 21 my $name = $args->{name};
54  
55 return +{
56 name => $name,
57 regex => qr{\A\Q$prefix\E$dash_re\z},
58 3 ▶ input => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
59 3 my ( $class, $value ) = @_;
60  
61 3 ▶ if ( $value !~ s{\A\Q$prefix\E}{} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ s/\A\Q$prefix\E//) 0 3 3
62 {
63 0 die "$name value does not start with the '$prefix' prefix.";
64 }
65  
66 3 return $class->_calc_components_from_string(
67 { format => 'dash', value => $value } );
68 },
69 9 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
70 9 my ($self) = @_;
71  
72 9 return $prefix . $self->format_as('dash');
73 },
74 21 };
75 }
76  
77 sub _gen_colon_format
78 14 ▶{
Subroutine _gen_colon_format: called CC=1 SCAR=0.0
79 14 my ($args) = @_;
80  
81 14 my $prefix = $args->{prefix};
82 14 my $suffix = $args->{suffix};
83 14 my $name = $args->{name};
84  
85 return +{
86 name => $name,
87 regex => qr{\A\Q$prefix\E$colon_re\Q$suffix\E\z},
88 4 ▶ input => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
89 4 my ( $class, $value ) = @_;
90  
91 4 ▶ if ( $value !~ m{\A\Q$prefix\E((?:\w+::)*\w+)\Q$suffix\E\z} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /\A\Q$prefix\E((?:\w+::)*\w+)\Q$suffix\E\z/) 0 4 4
92 {
93 0 die "Improper value for $name";
94 }
95  
96 4 return $class->_calc_components_from_string(
97 { format => 'colon', value => $1 } );
98 },
99 5 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
100 5 my ($self) = @_;
101  
102 5 return $prefix . $self->format_as('colon') . $suffix;
103 },
104 14 };
105 }
106  
107 my @formats_by_priority = (
108 _gen_dash_format(
109 {
110 name => 'rpm_dash',
111 prefix => 'perl-',
112 }
113 ),
114 _gen_colon_format(
115 {
116 name => 'rpm_colon',
117 prefix => 'perl(',
118 suffix => ')',
119 }
120 ),
121 {
122 name => 'colon',
123 regex => qr{\A$colon_re\z},
124 9 ▶ input => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
125 9 my ( $class, $value ) = @_;
126 9 return [ split( /::/, $value, -1 ) ];
127 },
128 14 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
129 14 my ($self) = @_;
130  
131 14 return join( '::', @{ $self->_components() } );
132 },
133 },
134 {
135 name => 'dash',
136 regex => qr{\A$dash_re\z},
137 7 ▶ input => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
138 7 my ( $class, $value ) = @_;
139 7 return [ split( /-/, $value, -1 ) ];
140 },
141 21 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
142 21 my ($self) = @_;
143  
144 21 return join( '-', @{ $self->_components() } );
145 },
146 },
147 {
148 name => 'unix',
149 regex => qr{\A(?:\w+/)*\w+\.pm\z},
150 2 ▶ input => sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
151 2 my ( $class, $value ) = @_;
152  
153 2 ▶ if ( $value !~ s{\.pm\z}{} )
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ s/\.pm\z//) 0 2 2
154 {
155 0 die "Cannot find a .pm suffix in the 'unix' format.";
156 }
157  
158 2 return [ split( m{/}, $value, -1 ) ];
159 },
160 3 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
161 3 my ($self) = @_;
162  
163 3 return join( '/', @{ $self->_components() } ) . '.pm';
164 },
165 },
166 _gen_dash_format(
167 {
168 name => 'metacpan_rel',
169 prefix => $METACPAN_REL,
170 }
171 ),
172 _gen_colon_format(
173 {
174 name => 'metacpan_pod',
175 prefix => 'https://metacpan.org/pod/',
176 suffix => '',
177 }
178 ),
179 {
180 name => 'debian',
181 1 ▶ format => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
182 1 my ($self) = @_;
183  
184 1 return 'lib' . lc( $self->format_as('dash') ) . '-perl';
185 },
186 },
187 7 _gen_dash_format(
188 {
189 name => 'freebsd_dash',
190 prefix => 'p5-',
191 }
192 ),
193 );
194  
195 7 my %formats = ( map { $_->{name} => $_ } @formats_by_priority );
196  
197 sub _calc_components_from_string
198 25 ▶{
Subroutine _calc_components_from_string: called CC=3 SCAR=12.0
199 25 my ( $class, $args ) = @_;
200  
201 25 my $format = $args->{format};
202 25 my $value = $args->{value};
203  
204 25 ▶ if ( exists( $formats{$format} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $formats{$format}) { } 25 0 25
205 {
206 25 ▶ if ( my $handler = $formats{$format}->{'input'} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $handler = $formats{$format}{'input'}) { } 25 0 25
207 {
208 25 return $class->$handler($value);
209 }
210 else
211 {
212 0 die "Format $format is output-only!";
213 }
214 }
215 else
216 {
217 0 die "Unknown format '$format'!";
218 }
219 }
220  
221 sub from
222 18 ▶{
Subroutine from: called CC=1 SCAR=0.0
Pod documented
223 18 my ( $class, $args ) = @_;
224  
225 18 my $format = $args->{format};
226 18 my $value = $args->{value};
227  
228 18 return $class->_new(
229 {
230 _components => $class->_calc_components_from_string($args)
231 }
232 );
233 }
234  
235  
236 sub get_components_list
237 20 ▶{
Subroutine get_components_list: called CC=1 SCAR=0.0
Pod documented
238 20 my $self = shift;
239  
240 20 return [ @{ $self->_components() } ];
241 }
242  
243  
244 sub format_as
245 53 ▶{
Subroutine format_as: called CC=2 SCAR=7.9
Pod documented
246 53 my ( $self, $format ) = @_;
247  
248 53 ▶ if ( exists( $formats{$format} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $formats{$format}) { } 53 0 53
249 {
250 53 my $handler = $formats{$format}->{'format'};
251 53 return $self->$handler();
252 }
253 else
254 {
255 0 die "Unknown format '$format';";
256 }
257  
258 0 return;
259 }
260  
261  
262 sub clone
263 1 ▶{
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
264 1 my $self = shift;
265  
266 return
267 1 ref($self)
268 ->from_components( { components => $self->get_components_list() } );
269 }
270  
271  
272 sub _all
273 4 ▶{
Subroutine _all: called CC=3 SCAR=11.0
274 4 my ( $cb, $l ) = @_;
275  
276 4 foreach (@$l)
277 {
278 9 ▶ if ( not $cb->($_) )
Branch: 1 branch
BranchTrueFalseTotal
unless (&$cb($_)) 1 8 9
279 {
280 1 return;
281 }
282 }
283  
284 3 return 1;
285 }
286  
287 sub is_sane
288 4 ▶{
Subroutine is_sane: called CC=1 SCAR=0.0
Pod documented
289 4 my $self = shift;
290  
291 4 ▶ return _all( sub { m{\A\w+\z}; }, $self->_components() );
Subroutine __ANON__: called CC=1 SCAR=0.0
292 }
293  
294  
295 sub from_guess
296 12 ▶{
Subroutine from_guess: called CC=5 SCAR=16.2
Pod documented
297 12 my ( $class, $args ) = @_;
298  
299 12 my $dummy_format_string;
300  
301 12 my $string = $args->{value};
302 12 ▶ my $out_format_ref = ( $args->{format_ref} || ( \$dummy_format_string ) );
Condition100%2/2
$args->{'format_ref'} || \$dummy_format_string
l!l
75
MC/DC100%1/1
$args->{'format_ref'} || \$dummy_format_string
$args->{'format_ref'}
Truth table100%2/2
A
A= $args->{'format_ref'}
Aresult
11
00
303  
304 # TODO : After the previous line the indentation in vim is reset to the
305 # first column.
306  
307 12 foreach my $format_record (@formats_by_priority)
308 {
309 44 ▶ if ( my $regex = $format_record->{regex} )
Branch: 1 branch
BranchTrueFalseTotal
if (my $regex = $format_record->{"regex"}) 44 0 44
310 {
311 44 ▶ if ( $string =~ $regex )
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ /$regex/) 12 32 44
312 {
313 12 my $format_id = $format_record->{name};
314  
315 12 ${$out_format_ref} = $format_id;
316  
317 12 return $class->from(
318 { value => $string, format => $format_id, } );
319 }
320 }
321 }
322  
323 0 die "Could not guess the format of the value '$string'!";
324 }
325  
326  
327 7 1; # End of Module::Format::Module
328  
329 __END__
« Format/PerlMF_App.pm ↑ Summary Format/ModuleList.pm »