blib/lib/YATT/Lite/Object.pm

filter: Statementstmt 76.0% 105 / 138 Branchbran 61.5% 32 / 52 Conditioncond 58.8% 10 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 75.8% 22 / 29 Podpod 23.5% 4 / 17 totaltotal 67.3% 177 / 263 CC 36 SCAR 16.3
CC
36
Cov
71%
configure
12.6
cf_by_filetype
12.0
cf_unknowns
6.5
CRAP 5.1
« blib/lib/YATT/Lite/Entities.pm ↑ Summary blib/lib/YATT/Lite/CGen.pm »
1 package YATT::Lite::Object; sub MY () {__PACKAGE__}
2 98 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 98 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 98 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 98 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 98 ▶use fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 98 require YATT::Lite::XHF;
10 98 *read_file_xhf = *YATT::Lite::XHF::read_file_xhf;
11 98 *read_file_xhf = *YATT::Lite::XHF::read_file_xhf;
12  
13 98 require YATT::Lite::Util;
14  
15 39417 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
16 39417 my $self = fields::new(shift);
17 39417 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 39296 121 39417
18 39296 my @task = $self->configure(@_);
19 39296 $self->_before_after_new;
20 39296 $self->after_new;
21 39296 $$_[0]->($self, $$_[1]) for @task;
22 } else {
23 121 $self->_before_after_new;
24 121 $self->after_new;
25 }
26  
27 # To tolerate ``forgotten ->SUPER::after_new() bug'' in user class.
28 39410 $self->_after_after_new;
29  
30 39409 $self;
31 }
32  
33 257 ▶sub just_new {
Subroutine just_new: called CC=1 SCAR=0.0
Pod undocumented
34 257 my $self = fields::new(shift);
35 # To delay configure_zzz.
36 257 ($self, $self->configure(@_));
37 }
38  
39 # General initialization hook for each user class.
40 39280 ▶sub after_new {};
Subroutine after_new: called CC=1 SCAR=0.0
Pod documented
41  
42 # Two more initialization hooks for framework writer.
43  
44 # Called just after parameter initialization.
45 # Good for private member initialization.
46 39096 ▶sub _before_after_new {}
Subroutine _before_after_new: called CC=1 SCAR=0.0
47  
48 # Called after all configure_ZZZ hook is called.
49 39089 ▶sub _after_after_new {}
Subroutine _after_after_new: called CC=1 SCAR=0.0
50  
51 98 our $loading_file;
52 0 ▶sub _loading_file {
Subroutine _loading_file: not called CC=2 SCAR=17.9
53 0 ▶ return "\n loaded from (unknown file)" unless defined $loading_file;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $YATT::Lite::Object::loading_file 0 0 0
54 0 sprintf qq|\n loaded from file '%s'|, $loading_file;
55 }
56 24 ▶sub _with_loading_file {
Subroutine _with_loading_file: called CC=2 SCAR=7.4
57 24 my ($self, $fn, $method) = @_[0 .. 2];
58 24 local $loading_file = $fn;
59 24 ▶ if (ref $method eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $method eq 'CODE') { } 24 0 24
60 24 $method->(@_[3 .. $#_]);
61 } else {
62 0 $self->$method(@_[3 .. $#_]);
63 }
64 }
65  
66 # XXX: To hide from subclass. (Might harm localization)
67 0 ▶my $NO_SUCH_CONFIG_ITEM = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
68 0 my ($self, $name) = @_;
69 0 "No such config item $name in class " . ref($self)
70 . $self->_loading_file;
71 98 };
72  
73 878 ▶sub cget {
Subroutine cget: called CC=4 SCAR=15.5
Pod documented
74 878 my ($self, $name, $default) = @_;
75 878 ▶ if ($name =~ m{^_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) 0 878 878
76 0 Carp::croak "Private option is prohibited for class ".ref($self).": $name";
77 }
78 878 my $fields = YATT::Lite::Util::fields_hash($self);
79 878 ▶ if (not exists $fields->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$name}) 0 878 878
80 0 confess $NO_SUCH_CONFIG_ITEM->($self, $name);
81 }
82 878 ▶ $self->{$name} // $default;
Condition66%2/3
$self->{$name} // $default
l!l&&r!l&&!r
822056
MC/DC50%1/2
$self->{$name} // $default
$self->{$name} $default
Truth table66%2/3
A // B
A= $self->{$name}B= $default
ABresult
1X1
011
000
83 }
84  
85 43612 ▶sub configure {
Subroutine configure: called CC=10 SCAR=25.4
Pod documented
86 43612 my $self = shift;
87 43612 my (@task);
88 43612 my $fields = YATT::Lite::Util::fields_hash($self);
89 0 ▶ my @params = @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 43612 43612
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
4361200
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "HASH"
@_ == 1 ref $_[0] eq "HASH"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "HASH"
ABresult
0X0
100
111
90 43612 ▶ while (my ($name, $value) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 133071 43612 176683
91 133071 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 133071 133071
92 0 croak "Undefined name given for @{[ref($self)]}->configure(name=>value)!";
93 }
94 133071 $name =~ s/^-//;
95 133071 ▶ if ($name =~ m{^_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) 0 133071 133071
96 0 Carp::croak "Private option is prohibited for class ".ref($self).": $name";
97 }
98 133071 ▶ if (my $sub = $self->can("configure_$name")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("configure_$name")) { } 5878 127193 133071
elsif (not exists $fields->{$name}) { } 0 127193 127193
99 5878 push @task, [$sub, $value];
100 } elsif (not exists $fields->{$name}) {
101 0 confess $NO_SUCH_CONFIG_ITEM->($self, $name);
102 } else {
103 127193 $self->{$name} = $value;
104 }
105 }
106 43612 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 39553 4059 43612
107 # To delay configure_zzz.
108 39553 @task;
109 } else {
110 4059 $$_[0]->($self, $$_[1]) for @task;
111 4059 $self;
112 }
113 }
114  
115 2 ▶sub cf_list {
Subroutine cf_list: called CC=3 SCAR=11.0
Pod undocumented
116 2 my $obj_or_class = shift;
117 2 my $pat = shift;
118 2 my $fields = YATT::Lite::Util::fields_hash($obj_or_class);
119 sort grep {
120 2 ▶ !/^_/ && (!defined $pat || $_ =~ $pat)
Condition100%6/6
!/^_/ && (!defined($pat) || $_ =~ /$pat/)
!ll&&!rl&&r
213
not defined $pat or $_ =~ /$pat/
l!l&&r!l&&!r
211
MC/DC100%3/3
!/^_/ && (!defined($pat) || $_ =~ /$pat/)
!/^_/ not defined $pat $_ =~ /$pat/
Truth table100%4/4
not(A) && not(B) or C
A= !/^_/B= not defined $patC= $_ =~ /$pat/
ABCresult
0XX0
1000
11X1
1011
121 } keys %$fields;
122 }
123  
124 0 ▶sub cf_pairs {
Subroutine cf_pairs: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my ($obj) = shift;
126 0 my $fields = YATT::Lite::Util::fields_hash($obj);
127 map {
128 0 [$_ => $obj->{$_}]
129 0 } grep {!/^_/} keys %$fields;
130 }
131  
132 #
133 # util for delegate
134 #
135 2103 ▶sub cf_delegate {
Subroutine cf_delegate: called CC=3 SCAR=11.2
Pod undocumented
136 2103 my MY $self = shift;
137 2103 my $fields = YATT::Lite::Util::fields_hash($self);
138 map {
139 2103 ▶ my ($from, $to) = ref $_ ? @$_ : ($_, $_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 2526 11802 14328
140 14328 ▶ unless (exists $fields->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$from}) 0 14328 14328
141 0 confess $NO_SUCH_CONFIG_ITEM->($self, $from);
142 }
143 14328 $to => $self->{$from}
144 } @_;
145 }
146  
147 926 ▶sub cf_delegate_defined {
Subroutine cf_delegate_defined: called CC=1 SCAR=0.0
Pod undocumented
148 926 my MY $self = shift;
149 926 my $fields = YATT::Lite::Util::fields_hash($self);
150 926 $self->cf_delegate_known(1, $fields, @_);
151 }
152  
153 1208 ▶sub cf_delegate_known {
Subroutine cf_delegate_known: called CC=5 SCAR=16.2
Pod undocumented
154 1208 (my MY $self, my ($raise_err, $fields)) = splice @_, 0, 3;
155 map {
156 1208 ▶ my ($from, $to) = ref $_ ? @$_ : ($_, $_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 19648 19648
157 19648 ▶ if (not exists $fields->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $fields->{$from}) { } 602 19046 19648
158 602 ▶ $raise_err ? (confess $NO_SUCH_CONFIG_ITEM->($self, $from)) : ();
Branch: 1 branch
BranchTrueFalseTotal
$raise_err ? : 0 602 602
159 } else {
160 19046 ▶ defined $self->{$from} ? ($to => $self->{$from}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$from} ? : 5806 13240 19046
161 }
162 } @_;
163 }
164  
165 # Or, say, with_option.
166 # XXX: configure_ZZZ hook is not applied.
167 471 ▶sub cf_let {
Subroutine cf_let: called CC=2 SCAR=7.1
Pod undocumented
168 471 (my MY $self, my ($binding, $task)) = splice @_, 0, 3;
169 471 my ($keys, $values) = $self->cf_bindings(@$binding);
170 471 local @{$self}{@$keys} = @$values;
171 471 ▶ if (ref $task) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $task) { } 471 0 471
172 471 $task->($self, @_);
173 } else {
174 0 $self->$task(@_);
175 }
176 }
177  
178 471 ▶sub cf_bindings {
Subroutine cf_bindings: called CC=3 SCAR=11.0
Pod undocumented
179 471 my MY $self = shift;
180 471 ▶ carp "Odd number of key value bindings" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 471 471
181 471 my (@keys, @values);
182 471 ▶ while (my ($key, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 36 471 507
183 # XXX: key check!
184 # XXX: task extraction!
185 36 push @keys, $key; push @values, $value;
186 }
187 471 (\@keys, \@values);
188 }
189  
190  
191 282 ▶sub cf_unknowns {
Subroutine cf_unknowns: called CC=5 SCAR=18.7
Pod undocumented
192 282 my $self = shift;
193 282 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
02820
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
194 282 my $fields = YATT::Lite::Util::fields_hash($class);
195 282 my @unknown;
196 282 ▶ while (my ($name, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 6 282 288
197 6 ▶ next if $fields->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if $fields->{$name} 6 0 6
198 0 ▶ next if $self->can("configure_$name");
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("configure_$name") 0 0 0
199 0 push @unknown, $name;
200 }
201 282 @unknown;
202 }
203  
204 0 ▶sub cf_by_file {
Subroutine cf_by_file: not called CC=1 SCAR=6.9
Pod undocumented
205 0 (my MY $self, my $fn) = @_[0..1];
206 0 my ($ext) = $fn =~ m{\.(\w+)$};
207 0 $self->cf_by_filetype($ext, $fn, @_[3..$#_]);
208 }
209  
210 0 ▶sub cf_by_filetype {
Subroutine cf_by_filetype: not called CC=3 SCAR=24.8
Pod undocumented
211 0 (my MY $self, my ($ext, $fn)) = @_[0..2];
212 0 ▶ $ext //= 'xhf';
Condition0%0/2
$ext //= "xhf"
l!l
00
MC/DC0%0/1
$ext //= "xhf"
$ext
Truth table0%0/2
A
A= $ext
Aresult
11
00
213 0 ▶ my $sub = $self->can("read_file_$ext")
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $self->can("read_file_$ext") or croak("Unknown config file type: $fn") 0 0 0
214 or croak "Unknown config file type: $fn";
215 $self->_with_loading_file
216 0 ▶ ($fn, sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 $self->configure($sub->($self, $fn));
218 0 });
219 }
220  
221 0 ▶sub define {
Subroutine define: not called CC=1 SCAR=6.9
Pod undocumented
222 0 my ($class, $name, $sub) = @_;
223 0 *{YATT::Lite::Util::globref($class, $name)} = $sub;
224 }
225  
226 80 ▶sub cf_mkaccessors {
Subroutine cf_mkaccessors: called CC=3 SCAR=11.2
Pod undocumented
227 80 my ($class, @names) = @_;
228 80 my $fields = YATT::Lite::Util::fields_hash($class);
229 80 foreach my $name (@names) {
230 80 ▶ unless ($fields->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields->{$name}) 0 80 80
231 0 croak "No such config: $name";
232 }
233 80 ▶ *{YATT::Lite::Util::globref($class, $name)} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 1 shift->{$name};
235 80 };
236 }
237 }
238 98 1;
« blib/lib/YATT/Lite/Entities.pm ↑ Summary blib/lib/YATT/Lite/CGen.pm »