blib/lib/YATT/Lite/Entities.pm

filter: Statementstmt 59.6% 118 / 198 Branchbran 54.5% 24 / 44 Conditioncond 48.1% 13 / 27 MC/DCmcdc 20.0% 3 / 15 Subroutinesub 50.8% 30 / 59 Podpod 0.0% 0 / 51 totaltotal 47.7% 188 / 394 CC 41 SCAR 16.8
CC
41
Cov
58%
import
13.4
entity_query_string
12.3
entity_llength
12.0
CRAP 5.4
« blib/lib/YATT/Lite/CGen.pm ↑ Summary blib/lib/YATT/Lite/Object.pm »
1 package YATT::Lite::Entities;
2 54 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 54 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 54 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 54 ▶use constant DEBUG_IMPORT => $ENV{DEBUG_YATT_IMPORT} // 0;
Subroutine BEGIN: called CC=2 SCAR=7.1
Condition50%1/2
$ENV{"DEBUG_YATT_IMPORT"} // 0
l!l
054
MC/DC0%0/1
$ENV{"DEBUG_YATT_IMPORT"} // 0
$ENV{"DEBUG_YATT_IMPORT"}
Truth table50%1/2
A
A= $ENV{"DEBUG_YATT_IMPORT"}
Aresult
11
00
7  
8 54 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 # XXX: 残念ながら、要整理。
10  
11 54 require YATT::Lite::MFields;
12  
13 54 ▶use YATT::Lite::Util qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
14 globref terse_dump url_encode
15 raise_response
16 raise_download
17 raise_psgi_error
18 raise_psgi_dump
19 secure_text_plain
20 psgi_text
21 build_nested_query
22 54 /;
23  
24 61 ▶sub default_export { qw(*YATT) }
Subroutine default_export: called CC=1 SCAR=0.0
Pod undocumented
25  
26 #========================================
27 # Facade を template に見せるための, グローバル変数.
28 54 our $YATT;
29 158 ▶sub symbol_YATT { return *YATT }
Subroutine symbol_YATT: called CC=1 SCAR=0.0
Pod undocumented
30 8 ▶sub YATT { $YATT }
Subroutine YATT: called CC=1 SCAR=0.0
Pod undocumented
31 0 ▶sub DIR { $YATT }
Subroutine DIR: not called CC=1 SCAR=6.9
Pod undocumented
32  
33 # Factory/Dispatcher/Logger/... を template に見せる
34 54 our $SYS;
35 200 ▶sub symbol_SYS { return *SYS }
Subroutine symbol_SYS: called CC=1 SCAR=0.0
Pod undocumented
36 0 ▶sub SYS { $SYS }
Subroutine SYS: not called CC=1 SCAR=6.9
Pod undocumented
37 0 ▶sub SYSTEM { $SYS }
Subroutine SYSTEM: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶sub SITE { $SYS }
Subroutine SITE: not called CC=1 SCAR=6.9
Pod undocumented
39  
40 # Connection
41 54 our $CON;
42 203 ▶sub symbol_CON { return *CON }
Subroutine symbol_CON: called CC=1 SCAR=0.0
Pod undocumented
43 58 ▶sub CON { return $CON }
Subroutine CON: called CC=1 SCAR=0.0
Pod undocumented
44 #========================================
45  
46 256 ▶sub import {
Subroutine import: called CC=11 SCAR=25.9
47 256 my ($pack, @opts) = @_;
48 256 Carp::carp(scalar caller, " calls $_[0]->import()") if DEBUG_IMPORT;
49 256 ▶ @opts = $pack->default_export unless @opts;
Branch: 1 branch
BranchTrueFalseTotal
unless @opts 61 195 256
50 256 my $callpack = caller;
51 256 my (%opts, @task);
52 256 foreach my $exp (@opts) {
53 853 ▶ if (my $sub = $pack->can("define_$exp")) {
Branch: 4 branches
BranchTrueFalseTotal
if (my $sub = $pack->can("define_$exp")) { } 202 651 853
elsif ($exp =~ /^-(\w+)$/) { } 97 554 651
elsif ($exp =~ /^\*(\w+)$/) { } 554 0 554
elsif ($sub = $pack->can($exp)) { } 0 0 0
54 202 push @task, $sub;
55 } elsif ($exp =~ /^-(\w+)$/) {
56 97 ▶ $sub = $pack->can("declare_$1")
Branch: 1 branch
BranchTrueFalseTotal
$sub = $pack->can("declare_$1") or croak("Unknown declarator: $1") 97 0 97
57 or croak "Unknown declarator: $1";
58 97 $sub->($pack, \%opts, $callpack);
59 } elsif ($exp =~ /^\*(\w+)$/) {
60 554 ▶ $sub = $pack->can("symbol_$1")
Branch: 1 branch
BranchTrueFalseTotal
$sub = $pack->can("symbol_$1") or croak("Can't export symbol $1") 554 0 554
61 or croak "Can't export symbol $1";
62 554 my $val = $sub->();
63 554 ▶ unless (defined $val) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val) 0 554 554
64 0 croak "Undefined symbol in export spec: $exp";
65 }
66 554 *{globref($callpack, $1)} = $val;
67 } elsif ($sub = $pack->can($exp)) {
68 0 *{globref($callpack, $exp)} = $sub;
69 } else {
70 0 croak "Unknown export spec: $exp";
71 }
72 }
73 256 foreach my $sub (@task) {
74 202 $sub->($pack, \%opts, $callpack);
75 }
76 }
77  
78 # use 時に関数を生成したい場合、 define_ZZZ を定義すること。
79 # サブクラスで新たな symbol を export したい場合、 symbol_ZZZ を定義すること
80  
81 54 *declare_as_parent = *declare_as_base; *declare_as_parent = *declare_as_base;
82  
83 97 ▶sub declare_as_base {
Subroutine declare_as_base: called CC=1 SCAR=0.0
Pod undocumented
84 97 my ($myPack, $opts, $callpack) = @_;
85 # ckrequire($myPack); # Not needed because $myPack is just used!
86  
87 97 mro::set_mro($callpack, 'c3');
88  
89 # Fill $callpack's %FIELDS, by current ISA.
90 97 YATT::Lite::MFields->add_isa_to($callpack, $myPack)
91 ->define_fields($callpack);
92 }
93  
94 #########################################
95  
96 52 ▶sub define_import {
Subroutine define_import: called CC=1 SCAR=0.0
Pod undocumented
97 52 my ($myPack, $opts, $callpack) = @_;
98 52 *{globref($callpack, 'import')} = \&import;
99 }
100  
101 0 ▶sub define_MY {
Subroutine define_MY: not called CC=2 SCAR=17.9
Pod undocumented
102 0 my ($myPack, $opts, $callpack) = @_;
103 0 my $my = globref($callpack, 'MY');
104 0 ▶ unless (*{$my}{CODE}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (*{$my;}{"CODE"}) 0 0 0
105 0 YATT::Lite::Util::define_const($my, $callpack);
106 }
107 }
108  
109 #========================================
110 # 組み込み Entity
111 # Entity 呼び出し時の第一引数は, packageName (つまり文字列) になる。
112  
113 1 ▶sub entity_breakpoint {
Subroutine entity_breakpoint: called CC=1 SCAR=0.0
Pod undocumented
114 1 require YATT::Lite::Breakpoint;
115 1 &YATT::Lite::Breakpoint::breakpoint();
116 }
117  
118 #
119 # Identity. This helps learning about entity path expression.
120 #
121 8 ▶sub entity_val {
Subroutine entity_val: called CC=1 SCAR=0.0
Pod undocumented
122 8 my ($this, $val) = @_;
123 8 $val;
124 }
125  
126 #
127 # Hiding return value is another important idiom.
128 #
129 0 ▶sub entity_ignore {
Subroutine entity_ignore: not called CC=1 SCAR=6.9
Pod undocumented
130 0 ();
131 }
132  
133 2 ▶sub entity_param {
Subroutine entity_param: called CC=1 SCAR=0.0
Pod undocumented
134 2 shift;
135 2 $CON->param(@_);
136 }
137  
138 0 ▶sub entity_set_param {
Subroutine entity_set_param: not called CC=1 SCAR=6.9
Pod undocumented
139 0 my ($this, $name, $value) = @_;
140 0 $CON->param($name, $value);
141 0 '';
142 }
143  
144 0 ▶sub entity_delete_param {
Subroutine entity_delete_param: not called CC=1 SCAR=6.9
Pod undocumented
145 0 shift;
146 0 $CON->delete_param(@_);
147 }
148  
149 1 ▶sub entity_concat {
Subroutine entity_concat: called CC=1 SCAR=0.0
Pod undocumented
150 1 my $this = shift;
151 1 join '', @_;
152 }
153  
154 3 ▶sub entity_coalesce {
Subroutine entity_coalesce: called CC=3 SCAR=11.1
Pod undocumented
155 3 my $this = shift;
156 3 foreach my $str (@_) {
157 5 ▶ return $str if defined $str;
Branch: 1 branch
BranchTrueFalseTotal
if defined $str 3 2 5
158 }
159 0 '';
160 }
161  
162 5 ▶sub entity_default {
Subroutine entity_default: called CC=4 SCAR=13.9
Pod undocumented
163 5 my $this = shift;
164 5 foreach my $str (@_) {
165 8 ▶ return $str if defined $str and $str ne '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $str and $str ne "" 5 3 8
Condition100%3/3
defined $str and $str ne ""
!ll&&!rl&&r
215
MC/DC100%2/2
defined $str and $str ne ""
defined $str $str ne ""
Truth table100%3/3
A and B
A= defined $strB= $str ne ""
ABresult
0X0
100
111
166 }
167 0 '';
168 }
169  
170 54 *entity_lsize = *entity_llength; *entity_lsize = *entity_llength;
171 0 ▶sub entity_llength {
Subroutine entity_llength: not called CC=3 SCAR=24.8
Pod undocumented
172 0 my ($this, $list) = @_;
173 0 ▶ return undef unless defined $list and ref $list eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless defined $list and ref $list eq "ARRAY" 0 0 0
Condition0%0/3
defined $list and ref $list eq "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $list and ref $list eq "ARRAY"
defined $list ref $list eq "ARRAY"
Truth table0%0/3
A and B
A= defined $listB= ref $list eq "ARRAY"
ABresult
0X0
100
111
174 0 scalar @$list;
175 }
176  
177 3 ▶sub entity_join {
Subroutine entity_join: called CC=2 SCAR=7.0
Pod undocumented
178 3 my ($this, $sep) = splice @_, 0, 2;
179 3 ▶ join $sep, grep {defined $_ && $_ ne ''} @_;
Condition66%2/3
defined $_ && $_ ne ''
!ll&&!rl&&r
029
MC/DC50%1/2
defined $_ && $_ ne ''
defined $_ $_ ne ''
Truth table66%2/3
A && B
A= defined $_B= $_ ne ''
ABresult
0X0
100
111
180 }
181  
182 1 ▶sub entity_format {
Subroutine entity_format: called CC=1 SCAR=0.0
Pod undocumented
183 1 my ($this, $format) = (shift, shift);
184 1 sprintf $format, @_;
185 }
186  
187 6 ▶sub entity_HTML {
Subroutine entity_HTML: called CC=1 SCAR=0.0
Pod undocumented
188 6 my $this = shift;
189 6 \ join "", grep {defined $_} @_;
190 }
191  
192 1 ▶sub entity_url_encode {
Subroutine entity_url_encode: called CC=1 SCAR=0.0
Pod undocumented
193 1 my $this = shift;
194 1 join "", map {url_encode($this, $_)} @_;
195 }
196  
197 3 ▶sub entity_alternative {
Subroutine entity_alternative: called CC=2 SCAR=7.5
Pod undocumented
198 3 my ($this, $value, $list) = @_;
199 3 my @alt = do {
200 3 ▶ if (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $value) { } 3 0 3
201 3 grep {$value ne $_} @$list;
202 } else {
203 0 grep {defined $_} @$list;
204 }
205 };
206 3 $alt[0]
207 }
208  
209 # XXX: auto url_encode
210 3 ▶sub entity_append_params {
Subroutine entity_append_params: called CC=4 SCAR=13.9
Pod undocumented
211 3 my ($this, $url) = splice @_, 0, 2;
212 3 ▶ return $url unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 3 3
213 3 require URI;
214 3 require Hash::MultiValue;
215 3 my $uri = URI->new($url);
216 3 my $hmv = Hash::MultiValue->new($uri->query_form);
217 3 my %multi;
218 3 foreach my $item (@_) {
219 5 my ($key, @strs) = @$item;
220 5 ▶ $hmv->remove($key) unless $multi{$key}++;
Branch: 1 branch
BranchTrueFalseTotal
unless $multi{$key}++ 4 1 5
221 5 $hmv->add($key, join("", @strs));
222 }
223 3 $uri->query_form($hmv->flatten);
224 3 $uri->as_string;
225 }
226  
227 2 ▶sub entity_dump {
Subroutine entity_dump: called CC=1 SCAR=0.0
Pod undocumented
228 2 shift;
229 2 terse_dump(@_);
230 }
231  
232 0 ▶sub entity_can_render {
Subroutine entity_can_render: not called CC=2 SCAR=17.9
Pod undocumented
233 0 my ($this, $wspec) = @_;
234 0 my @nsegs = YATT::Lite::Util::lexpand($wspec);
235 0 ▶ my $wname = join _ => map {defined $_ ? $_ : ''} @nsegs;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
236 0 $this->can("render_$wname");
237 }
238  
239 0 ▶sub entity_uc { shift; uc($_[0]) }
Subroutine entity_uc: not called CC=1 SCAR=6.9
Pod undocumented
240 0 ▶sub entity_ucfirst { shift; ucfirst($_[0]) }
Subroutine entity_ucfirst: not called CC=1 SCAR=6.9
Pod undocumented
241 0 ▶sub entity_lc { shift; lc($_[0]) }
Subroutine entity_lc: not called CC=1 SCAR=6.9
Pod undocumented
242 0 ▶sub entity_lcfirst { shift; lcfirst($_[0]) }
Subroutine entity_lcfirst: not called CC=1 SCAR=6.9
Pod undocumented
243  
244 0 ▶sub entity_strftime {
Subroutine entity_strftime: not called CC=3 SCAR=24.8
Pod undocumented
245 0 my ($this, $fmt, $sec, $is_uts) = @_;
246 0 ▶ $sec //= time;
Condition0%0/3
$sec //= time
l!l&&r!l&&!r
000
MC/DC0%0/2
$sec //= time
$sec time
Truth table0%0/3
A //= B
A= $secB= time
ABresult
1X1
011
000
247 0 require POSIX;
248 0 ▶ POSIX::strftime($fmt, $is_uts ? gmtime($sec) : localtime($sec));
Branch: 1 branch
BranchTrueFalseTotal
$is_uts ? : 0 0 0
249 }
250  
251 0 ▶sub entity_mkhash {
Subroutine entity_mkhash: not called CC=2 SCAR=17.9
Pod undocumented
252 0 my ($this, @list) = @_;
253 0 my %hash;
254 0 $hash{$_} = 1 for @list;
255 0 \%hash;
256 }
257  
258 0 ▶sub entity_datetime {
Subroutine entity_datetime: not called CC=2 SCAR=17.9
Pod undocumented
259 0 my ($this, $method, @args) = @_;
260 0 ▶ $method //= 'now';
Condition0%0/2
$method //= "now"
l!l
00
MC/DC0%0/1
$method //= "now"
$method
Truth table0%0/2
A
A= $method
Aresult
11
00
261 0 require DateTime;
262 0 DateTime->$method(@args);
263 }
264  
265 0 ▶sub entity_localtime {
Subroutine entity_localtime: not called CC=1 SCAR=6.9
Pod undocumented
266 0 my ($this, $time) = @_;
267 0 require Time::Piece;
268 0 Time::Piece->localtime($time);
269 }
270  
271 0 ▶sub entity_gmtime {
Subroutine entity_gmtime: not called CC=1 SCAR=6.9
Pod undocumented
272 0 my ($this, $time) = @_;
273 0 require Time::Piece;
274 0 Time::Piece->gmtime($time);
275 }
276  
277 0 ▶sub entity_strptime {
Subroutine entity_strptime: not called CC=1 SCAR=6.9
Pod undocumented
278 0 my ($this, $string, $format) = @_;
279 0 require Time::Piece;
280 0 local $SIG{__DIE__}; # '@ytmpl/error.ytmpl' のハンドラーを抑制する
281 0 Time::Piece->strptime($string, $format);
282 }
283  
284 0 ▶sub entity_parse_date {
Subroutine entity_parse_date: not called CC=1 SCAR=6.9
Pod undocumented
285 0 my ($this, $string) = @_;
286 0 $this->entity_strptime($string, '%Y-%m-%d');
287 }
288 0 ▶sub entity_parse_datetime {
Subroutine entity_parse_datetime: not called CC=1 SCAR=6.9
Pod undocumented
289 0 my ($this, $string) = @_;
290 0 $this->entity_strptime($string, '%Y-%m-%d %H:%M:%S');
291 }
292  
293 0 ▶sub entity_redirect {
Subroutine entity_redirect: not called CC=1 SCAR=6.9
Pod undocumented
294 0 my ($this) = shift;
295 0 $CON->redirect(@_);
296 }
297  
298 # &yatt:code_of_entity(redirect);
299 #
300 0 ▶sub entity_code_of_entity {
Subroutine entity_code_of_entity: not called CC=1 SCAR=6.9
Pod undocumented
301 0 shift->entity_code_of(entity => @_);
302 }
303  
304 0 ▶sub entity_code_of {
Subroutine entity_code_of: not called CC=1 SCAR=6.9
Pod undocumented
305 0 my ($this, $prefix, $name) = @_;
306 0 $this->can(join("_", $prefix, $name));
307 }
308  
309 0 ▶sub entity_inspector {
Subroutine entity_inspector: not called CC=2 SCAR=17.9
Pod undocumented
310 0 require Sub::Inspector;
311 0 my ($this, $code) = @_;
312 0 ▶ croak "Not a code ref" unless ref $code;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $code 0 0 0
313 0 Sub::Inspector->new($code);
314 }
315  
316 0 ▶sub raise_dump {
Subroutine raise_dump: not called CC=1 SCAR=6.9
Pod undocumented
317 0 my ($this, @args) = @_;
318 0 $this->raise_response($this->psgi_text(500, terse_dump(@args)));
319 }
320  
321 0 ▶sub entity_raise_dump {shift->raise_dump(@_)}
Subroutine entity_raise_dump: not called CC=1 SCAR=6.9
Pod undocumented
322  
323 5 ▶sub entity_query_string {
Subroutine entity_query_string: called CC=11 SCAR=25.1
Pod undocumented
324 5 my $this = shift;
325 5 ▶ my $args = (@_ == 1 ? $_[0] : +{@_});
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 5 5
326 # XXX: check unknown options... statically?! ← entmacro?
327 5 ▶ $args->{sep} //= $args->{separator} // ';';
Condition60%3/5
$args->{"sep"} //= $args->{"separator"} // ";"
l!l&&r!l&&!r
050
$args->{"separator"} // ";"
l!l
23
MC/DC0%0/2
$args->{"sep"} //= $args->{"separator"} // ";"
$args->{"sep"} $args->{"separator"}
Truth table33%1/3
A //= B
A= $args->{"sep"}B= $args->{"separator"}
ABresult
1X1
011
000
328 5 my $hash = do {
329 5 ▶ my $h = $args->{of} // $args->{in} // $CON->as_hash;
Condition66%4/6
($args->{"of"} // $args->{"in"}) // $YATT::Lite::Entities::CON->as_hash
l!l&&r!l&&!r
140
$args->{"of"} // $args->{"in"}
l!l&&r!l&&!r
104
MC/DC0%0/3
($args->{"of"} // $args->{"in"}) // $YATT::Lite::Entities::CON->as_hash
$args->{"of"} $args->{"in"} $YATT::Lite::Entities::CON->as_hash
Truth table50%2/4
A // B // C
A= $args->{"of"}B= $args->{"in"}C= $YATT::Lite::Entities::CON->as_hash
ABCresult
1XX1
01X1
0011
0000
330 5 ▶ if (my $sub = UNIVERSAL::can($h, 'clone')) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $sub = UNIVERSAL::can($h, 'clone')) { } 0 5 5
331 0 $sub->($h);
332 } else {
333 5 YATT::Lite::Util::ixhash(%$h);
334 }
335 };
336 5 ▶ if (my $merge = $args->{merge}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $merge = $args->{"merge"}) 1 4 5
337 1 $hash->{$_} = $merge->{$_} for keys %$merge;
338 }
339 5 ▶ if (my $delete = $args->{delete}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $delete = $args->{"delete"}) 1 4 5
340 1 delete $hash->{$_} for YATT::Lite::Util::lexpand($delete);
341 }
342 5 $this->build_nested_query($hash, $args);
343 }
344  
345 0 ▶sub entity_build_nested_query {
Subroutine entity_build_nested_query: not called CC=1 SCAR=6.9
Pod undocumented
346 0 my ($this, $hash, $args) = @_;
347 0 $this->build_nested_query($hash, $args);
348 }
349  
350 54 ▶use YATT::Lite::Breakpoint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
351 54 YATT::Lite::Breakpoint::break_load_entns();
352  
353 54 1;
« blib/lib/YATT/Lite/CGen.pm ↑ Summary blib/lib/YATT/Lite/Object.pm »