Neaf/Util/Base.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 75.0% 6 / 8 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 4 / 4 totaltotal 88.7% 55 / 62 CC 6 SCAR 9.9
CC
6
Cov
89%
__ANON__
5.3
my_croak
2.0
dir
1.0
CRAP 2.7
« Neaf/X/Form/Data.pm ↑ Summary Neaf/X/Session.pm »
1 package MVC::Neaf::Util::Base;
2  
3 110 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 110 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 110 our $VERSION = '0.2901';
6  
7 =head1 NAME
8  
9 MVC::Neaf::Util::Base - base class for other Not Even A Framework classes.
10  
11 =head1 DESCRIPTION
12  
13 This is an internal package providing some utility methods for Neaf itself.
14  
15 See L<MVC::Neaf::X> for public interface.
16  
17 =head1 METHODS
18  
19 =cut
20  
21 110 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 110 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 =head2 new( %options )
25  
26 Will happily accept any args and pack them into self.
27  
28 =cut
29  
30 192 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
31 192 my ($class, %opt) = @_;
32  
33 192 return bless \%opt, $class;
34 };
35  
36 # NOTE My bad! The first method in this package was prefixed with my_
37 # Please prefix new methods with neaf_ instead, if possible.
38  
39 =head2 my_croak( $message )
40  
41 Like croak() from Carp, but the message is prefixed
42 with self's package and the name of method
43 in which error occurred.
44  
45 =cut
46  
47 30 ▶sub my_croak {
Subroutine my_croak: called CC=2 SCAR=7.0
Pod documented
48 30 my ($self, $msg) = @_;
49  
50 30 my $sub = [caller(1)]->[3];
51 30 $sub =~ s/.*:://;
52  
53 30 ▶ croak join "", (ref $self || $self),"->",$sub,": ",$msg;
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
2280
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
54 };
55  
56 =head2 dir ($path || [$path, ...])
57  
58 For every given path, return $path if it starts with a '/',
59 or canonized concatenation of $self->neaf_base_dir and $path
60 otherwise.
61  
62 Dies if C<neaf_base_dir> is not set.
63  
64 B<NOTE> Please use this method whenever your Neaf extension/plugin
65 is given a path, do not rely on '.' to be set correctly!
66  
67 =cut
68  
69 24 ▶sub dir {
Subroutine dir: called CC=1 SCAR=0.1
Pod documented
70 24 my $self = shift;
71  
72 # Cannot use Carp as it will likely point to the wrong location
73 # TODO Only calculate this when needed
74 24 my @stack = caller(1);
75  
76 # cache root so we only calculate it once
77 24 my $root;
78  
79 # recursive handler sub that maps arrayrefs through itself
80 my $handler;
81 26 ▶ $handler = sub {
Subroutine __ANON__: called CC=5 SCAR=16.7
82 26 ▶ return [map { $handler->() } @$_] if ref $_ eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_ eq "ARRAY" 15 11 26
83 11 ▶ return File::Spec->canonpath($_)
Branch: 1 branch
BranchTrueFalseTotal
if "File::Spec"->file_name_is_absolute($_) 5 6 11
84 if File::Spec->file_name_is_absolute($_);
85 6 ▶ if (!defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root) 6 0 6
86 6 $root = $self->neaf_base_dir;
87 6 ▶ unless (defined $root) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $root) 0 6 6
88 0 warn ((ref $self)."->path(...) was called, but neaf_base_dir was never set at $stack[1] line $stack[2].\n");
89 0 $root = '.';
90 };
91 };
92 6 return File::Spec->canonpath("$root/$_");
93 24 };
94  
95 24 local $_ = shift;
96 24 return $handler->();
97 };
98  
99 =head2 neaf_base_dir()
100  
101 Dumb accessor that returns C<$self-E<gt>{neaf_base_dir}>.
102  
103 Used by C<dir> (see above).
104  
105 =cut
106  
107 # Dumb accessor
108 4 ▶sub neaf_base_dir {
Subroutine neaf_base_dir: called CC=1 SCAR=0.0
Pod documented
109 4 return $_[0]->{neaf_base_dir};
110 }
111  
112 =head1 LICENSE AND COPYRIGHT
113  
114 This module is part of L<MVC::Neaf> suite.
115  
116 Copyright 2016-2023 Konstantin S. Uvarin C<khedin@cpan.org>.
117  
118 This program is free software; you can redistribute it and/or modify it
119 under the terms of either: the GNU General Public License as published
120 by the Free Software Foundation; or the Artistic License.
121  
122 See L<http://dev.perl.org/licenses/> for more information.
123  
124 =cut
125  
126 110 1;
« Neaf/X/Form/Data.pm ↑ Summary Neaf/X/Session.pm »