Spoon/Base.pm

filter: Statementstmt 56.9% 98 / 172 Branchbran 30.9% 13 / 42 Conditioncond 33.3% 5 / 15 MC/DCmcdc 11.1% 1 / 9 Subroutinesub 52.5% 21 / 40 Podpod 84.0% 21 / 25 totaltotal 52.4% 159 / 303 CC 26 SCAR 14.9
CC
26
Cov
51%
__ANON__
12.0
utf8_encode
12.0
hub
7.3
CRAP 4.4
« Spoon/ContentObject.pm ↑ Summary Spoon/Formatter.pm »
1 package Spoon::Base;
2 11 ▶use Spiffy 0.24 -Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 11 ▶use Spiffy qw(-yaml);
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use Spiffy qw(WWW XXX YYY ZZZ);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 # WWW - Creating a wrapper sub to require() IO::All caused spurious segfaults
6 11 ▶use IO::All 0.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 11 our @EXPORT = qw(io trace WWW XXX YYY ZZZ);
8 11 our @EXPORT_OK = qw(conf);
9  
10 11 field used_classes => [];
11 11 field 'encoding';
12 11 const plugin_base_directory => './plugin';
13 11 field using_debug => 0;
14 11 field config_class => 'Spoon::Config';
15  
16 43 ▶sub hub {
Subroutine hub: called CC=7 SCAR=19.9
Pod documented
17 43 ▶ return $Spoon::Base::HUB
Branch: 1 branch
BranchTrueFalseTotal
if defined $Spoon::Base::HUB and not @_ 37 6 43
Condition66%2/3
defined $Spoon::Base::HUB and not @_
!ll&&!rl&&r
6037
MC/DC50%1/2
defined $Spoon::Base::HUB and not @_
defined $Spoon::Base::HUB not @_
Truth table66%2/3
A and not(B)
A= defined $Spoon::Base::HUBB= not @_
ABresult
0X0
100
111
18 if defined($Spoon::Base::HUB) and not @_;
19 6 ▶ Carp::confess "Too late to create a new hub. One already exists"
Branch: 1 branch
BranchTrueFalseTotal
if defined $Spoon::Base::HUB 0 6 6
20 if defined $Spoon::Base::HUB;
21
22 6 my ($args, @config_files);
23 {
24 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶ local *paired_arguments = sub { qw(-config_class) };
Subroutine __ANON__: called CC=1 SCAR=0.0
26 6 ($args, @config_files) = $self->parse_arguments(@_);
27 }
28 my $config_class = $args->{-config_class} ||
29 6 ▶ $self->can('config_class')
Branch: 1 branch
BranchTrueFalseTotal
$args->{'-config_class'} || $self->can('config_class') ? : 6 0 6
Condition33%1/3
$args->{"-config_class"} or $self->can("config_class")
l!l&&r!l&&!r
060
MC/DC0%0/2
$args->{"-config_class"} or $self->can("config_class")
$args->{"-config_class"} $self->can("config_class")
Truth table33%1/3
A or B
A= $args->{"-config_class"}B= $self->can("config_class")
ABresult
1X1
011
000
30 ? $self->config_class
31 : 'Spoon::Config';
32 6 ▶ eval "require $config_class"; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 6 6
33 6 my $config = $config_class->new(@config_files);
34 6 my $hub_class = $config->hub_class;
35 6 eval "require $hub_class";
36 6 my $hub = $hub_class->new(
37 config => $config,
38 config_files => \@config_files,
39 );
40 }
41  
42 6 ▶sub destroy_hub {
Subroutine destroy_hub: called CC=1 SCAR=0.0
Pod undocumented
43 6 undef $Spoon::Base::HUB;
44 }
45  
46 24 ▶sub init { }
Subroutine init: called CC=1 SCAR=0.0
Pod documented
47  
48 0 ▶sub assert {
Subroutine assert: not called CC=2 SCAR=17.9
Pod documented
49 0 ▶ die "Assertion failed" unless shift;
Branch: 1 branch
BranchTrueFalseTotal
unless shift() 0 0 0
50 }
51  
52 0 ▶sub trace() {
Subroutine trace: not called CC=1 SCAR=1.2
Pod documented
53 0 require Spoon::Trace;
54 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
55 0 *trace = \ &Spoon::Trace::trace;
56 0 goto &trace;
57 }
58  
59 0 ▶sub t {
Subroutine t: not called CC=1 SCAR=6.9
Pod documented
60 0 trace->mark;
61 0 return $self;
62 }
63  
64 0 ▶sub conf() {
Subroutine conf: not called CC=1 SCAR=4.0
Pod documented
65 0 my ($name, $default) = @_;
66 0 my $package = caller;
67 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
68 0 ▶ *{$package . '::' . $name} = sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
69 0 my $self = shift;
70 return $self->{$name}
71 0 ▶ if exists $self->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{$name} 0 0 0
72 $self->{$name} = exists($self->hub->config->{$name})
73 0 ▶ ? $self->hub->config->{$name}
Branch: 1 branch
BranchTrueFalseTotal
exists $self->hub->config->{$name} ? : 0 0 0
74 : $default;
75 0 };
76 }
77  
78 0 ▶sub clone {
Subroutine clone: not called CC=1 SCAR=6.9
Pod documented
79 0 return bless {%$self}, ref $self;
80 }
81  
82 1 ▶sub is_in_cgi {
Subroutine is_in_cgi: called CC=1 SCAR=0.0
Pod documented
83 1 defined $ENV{GATEWAY_INTERFACE};
84 }
85  
86 0 ▶sub is_in_test {
Subroutine is_in_test: not called CC=1 SCAR=6.9
Pod documented
87 0 defined $ENV{SPOON_TEST};
88 }
89  
90 0 ▶sub have_plugin {
Subroutine have_plugin: not called CC=2 SCAR=17.9
Pod documented
91 0 ▶ my $hub = $self->class_id eq 'hub'
Branch: 1 branch
BranchTrueFalseTotal
$self->class_id eq 'hub' ? : 0 0 0
92 ? $self
93 : $self->hub;
94 0 local $@;
95 0 eval { $hub->load_class(shift) }
96 }
97
98 0 ▶sub plugin_directory {
Subroutine plugin_directory: not called CC=2 SCAR=17.9
Pod documented
99 0 my $dir = join '/',
100 $self->plugin_base_directory,
101 $self->class_id,
102 ;
103 0 ▶ mkdir $dir unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
104 0 return $dir;
105 }
106
107 1 ▶sub debug {
Subroutine debug: called CC=4 SCAR=16.1
Pod undocumented
108 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
109 1 ▶ if ($self->is_in_cgi) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->is_in_cgi) { } 0 1 1
110 0 ▶ eval 'use CGI::Carp qw(fatalsToBrowser)'; die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
111 0 ▶ $SIG{__DIE__} = sub { CGI::Carp::confess(@_) }
Subroutine __ANON__: not called CC=1 SCAR=6.9
112 0 }
113 else {
114 1 require Carp;
115 4 ▶ $SIG{__DIE__} = sub { Carp::confess(@_) }
Subroutine __ANON__: called CC=1 SCAR=0.0
116 1 }
117 1 ▶ $self->using_debug(1)
Branch: 1 branch
BranchTrueFalseTotal
if ref $self 0 1 1
118 if ref $self;
119 1 return $self;
120 }
121  
122 11 our ($UPPER, $LOWER, $ALPHA, $NUM, $ALPHANUM, $WORD, $WIKIWORD);
123 11 push @EXPORT_OK, qw($UPPER $LOWER $ALPHA $NUM $ALPHANUM $WORD $WIKIWORD);
124 11 our %EXPORT_TAGS = (char_classes => [@EXPORT_OK]);
125 11 ▶if ($] < 5.008) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.008) { } 0 11 11
126 0 $UPPER = 'A-Z\xc0-\xde';
127 0 $LOWER = 'a-z\xdf-\xff';
128 0 $ALPHA = $UPPER . $LOWER;
129 0 $NUM = '0-9';
130 0 $ALPHANUM = $ALPHA . $NUM;
131 0 $WORD = $ALPHANUM . '_';
132 0 $WIKIWORD = $WORD;
133 }
134 else {
135 11 $UPPER = '\p{UppercaseLetter}';
136 11 $LOWER = '\p{LowercaseLetter}';
137 11 $ALPHA = '\p{Letter}';
138 11 $NUM = '\p{Number}';
139 11 $ALPHANUM = '\p{Letter}\p{Number}\pM';
140 11 $WORD = '\p{Letter}\p{Number}\p{ConnectorPunctuation}\pM';
141 11 $WIKIWORD = "$UPPER$LOWER$NUM" . '\p{ConnectorPunctuation}\pM';
142 }
143  
144 0 ▶sub env_check {
Subroutine env_check: not called CC=2 SCAR=17.9
Pod documented
145 0 my $variable = shift;
146 die "Environment variable '$variable' not set"
147 0 ▶ unless defined $ENV{$variable};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ENV{$variable} 0 0 0
148 }
149  
150 0 ▶sub dumper_to_file {
Subroutine dumper_to_file: not called CC=2 SCAR=13.0
Pod documented
151 0 my $path = shift;
152 0 require Data::Dumper;
153 11 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
154 0 local $Data::Dumper::Indent = 1;
155 0 ▶ local $Data::Dumper::Terse = (@_ == 1) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
156 0 local $Data::Dumper::Sortkeys = 1;
157 0 io("$path")->assert->print(Data::Dumper::Dumper(@_));
158 }
159  
160 # Codecs and Escaping
161 11 my $has_utf8;
162 1 ▶sub has_utf8 {
Subroutine has_utf8: called CC=5 SCAR=17.5
Pod documented
163 1 ▶ $has_utf8 = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 1 1
164 1 ▶ return $has_utf8 if defined($has_utf8);
Branch: 1 branch
BranchTrueFalseTotal
if defined $has_utf8 0 1 1
165 1 ▶ $has_utf8 = $] < 5.008 ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.008 ? : 0 1 1
166 1 ▶ require Encode if $has_utf8;
Branch: 1 branch
BranchTrueFalseTotal
if $has_utf8 1 0 1
167 }
168  
169 1 ▶sub utf8_decode {
Subroutine utf8_decode: called CC=4 SCAR=17.1
Pod documented
170 1 ▶ $_[0] = Encode::decode('utf8', $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_utf8 and defined $_[0] and not &Encode::is_utf8($_[0]) 1 0 1
Condition33%2/6
$self->has_utf8 and defined $_[0] and not &Encode::is_utf8($_[0])
!ll&&!rl&&r
001
$self->has_utf8 and defined $_[0]
!ll&&!rl&&r
001
MC/DC0%0/3
$self->has_utf8 and defined $_[0] and not &Encode::is_utf8($_[0])
$self->has_utf8 defined $_[0] not &Encode::is_utf8($_[0])
Truth table25%1/4
A and B and not(C)
A= $self->has_utf8B= defined $_[0]C= not &Encode::is_utf8($_[0])
ABCresult
0XX0
10X0
1100
1111
171 if $self->has_utf8 and
172 defined $_[0] and
173 not Encode::is_utf8($_[0]);
174 1 return $_[0];
175 }
176  
177 0 ▶sub utf8_encode {
Subroutine utf8_encode: not called CC=3 SCAR=24.8
Pod documented
178 0 ▶ $_[0] = Encode::encode('utf8', $_[0])
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_utf8 and defined $_[0] 0 0 0
Condition0%0/3
$self->has_utf8 and defined $_[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$self->has_utf8 and defined $_[0]
$self->has_utf8 defined $_[0]
Truth table0%0/3
A and B
A= $self->has_utf8B= defined $_[0]
ABresult
0X0
100
111
179 if $self->has_utf8 and
180 defined $_[0];
181 0 return $_[0];
182 }
183  
184 0 ▶sub uri_escape {
Subroutine uri_escape: not called CC=1 SCAR=6.9
Pod documented
185 0 require CGI::Util;
186 0 my $data = shift;
187 0 $self->utf8_encode($data);
188 0 return CGI::Util::escape($data);
189 }
190  
191 0 ▶sub uri_unescape {
Subroutine uri_unescape: not called CC=1 SCAR=6.9
Pod documented
192 0 require CGI::Util;
193 0 my $data = shift;
194 0 $data = CGI::Util::unescape($data);
195 0 $self->utf8_decode($data);
196 0 return $data;
197 }
198  
199 # WWW - The CGI.pm version is broken in Chinese
200 10 ▶sub html_escape {
Subroutine html_escape: called CC=1 SCAR=0.0
Pod documented
201 10 my $val = shift;
202 10 $val =~ s/&/&#38;/g;
203 10 $val =~ s/</&lt;/g;
204 10 $val =~ s/>/&gt;/g;
205 10 $val =~ s/\(/&#40;/g;
206 10 $val =~ s/\)/&#41;/g;
207 10 $val =~ s/"/&#34;/g;
208 10 $val =~ s/'/&#39;/g;
209 10 return $val;
210 }
211  
212 0 ▶sub html_unescape {
Subroutine html_unescape: not called CC=1 SCAR=6.9
Pod documented
213 0 CGI::unescapeHTML(shift);
214 }
215  
216 0 ▶sub base64_encode {
Subroutine base64_encode: not called CC=1 SCAR=6.9
Pod documented
217 0 require MIME::Base64;
218 0 MIME::Base64::encode_base64(@_);
219 }
220  
221 0 ▶sub base64_decode {
Subroutine base64_decode: not called CC=1 SCAR=6.9
Pod undocumented
222 0 require MIME::Base64;
223 0 MIME::Base64::decode_base64(@_);
224 }
225  
226 # XXX Move to IO::All. Make more robust. Use Damian's prompting module.
227 package IO::All;
228  
229 0 ▶sub prompt {
Subroutine prompt: not called CC=1 SCAR=6.9
Pod undocumented
230 0 print shift;
231 0 io('-')->chomp->getline;
232 }
233  
234 11 __END__
« Spoon/ContentObject.pm ↑ Summary Spoon/Formatter.pm »