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/Response.pm »
1 package YATT::Lite::Object; sub MY () {__PACKAGE__}
2 64 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 64 ▶use warnings qw(FATAL all NONFATAL misc);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 64 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 64 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 64 ▶use fields;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 64 require YATT::Lite::XHF;
10 64 *read_file_xhf = *YATT::Lite::XHF::read_file_xhf;
11 64 *read_file_xhf = *YATT::Lite::XHF::read_file_xhf;
12  
13 64 require YATT::Lite::Util;
14  
15 24971 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
16 24971 my $self = fields::new(shift);
17 24971 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 24865 106 24971
18 24865 my @task = $self->configure(@_);
19 24865 $self->_before_after_new;
20 24865 $self->after_new;
21 24865 $$_[0]->($self, $$_[1]) for @task;
22 } else {
23 106 $self->_before_after_new;
24 106 $self->after_new;
25 }
26  
27 # To tolerate ``forgotten ->SUPER::after_new() bug'' in user class.
28 24971 $self->_after_after_new;
29  
30 24970 $self;
31 }
32  
33 189 ▶sub just_new {
Subroutine just_new: called CC=1 SCAR=0.0
Pod undocumented
34 189 my $self = fields::new(shift);
35 # To delay configure_zzz.
36 189 ($self, $self->configure(@_));
37 }
38  
39 # General initialization hook for each user class.
40 24838 ▶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 24758 ▶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 24758 ▶sub _after_after_new {}
Subroutine _after_after_new: called CC=1 SCAR=0.0
50  
51 64 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 64 };
72  
73 467 ▶sub cget {
Subroutine cget: called CC=4 SCAR=15.5
Pod documented
74 467 my ($self, $name, $default) = @_;
75 467 ▶ if ($name =~ m{^_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) 0 467 467
76 0 Carp::croak "Private option is prohibited for class ".ref($self).": $name";
77 }
78 467 my $fields = YATT::Lite::Util::fields_hash($self);
79 467 ▶ if (not exists $fields->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$name}) 0 467 467
80 0 confess $NO_SUCH_CONFIG_ITEM->($self, $name);
81 }
82 467 ▶ $self->{$name} // $default;
Condition66%2/3
$self->{$name} // $default
l!l&&r!l&&!r
456011
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 27874 ▶sub configure {
Subroutine configure: called CC=10 SCAR=25.4
Pod documented
86 27874 my $self = shift;
87 27874 my (@task);
88 27874 my $fields = YATT::Lite::Util::fields_hash($self);
89 27874 ▶ my @params = @_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 && ref $_[0] eq 'HASH' ? : 0 27874 27874
Condition33%1/3
@_ == 1 and ref $_[0] eq "HASH"
!ll&&!rl&&r
2787400
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 27874 ▶ while (my ($name, $value) = splice @params, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@params, 0, 2)) 85320 27874 113194
91 85320 ▶ unless (defined $name) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $name) 0 85320 85320
92 0 croak "Undefined name given for @{[ref($self)]}->configure(name=>value)!";
93 }
94 85320 $name =~ s/^-//;
95 85320 ▶ if ($name =~ m{^_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^_/) 0 85320 85320
96 0 Carp::croak "Private option is prohibited for class ".ref($self).": $name";
97 }
98 85320 ▶ if (my $sub = $self->can("configure_$name")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("configure_$name")) { } 3982 81338 85320
elsif (not exists $fields->{$name}) { } 0 81338 81338
99 3982 push @task, [$sub, $value];
100 } elsif (not exists $fields->{$name}) {
101 0 confess $NO_SUCH_CONFIG_ITEM->($self, $name);
102 } else {
103 81338 $self->{$name} = $value;
104 }
105 }
106 27874 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 25054 2820 27874
107 # To delay configure_zzz.
108 25054 @task;
109 } else {
110 2820 $$_[0]->($self, $$_[1]) for @task;
111 2820 $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 1548 ▶sub cf_delegate {
Subroutine cf_delegate: called CC=3 SCAR=11.2
Pod undocumented
136 1548 my MY $self = shift;
137 1548 my $fields = YATT::Lite::Util::fields_hash($self);
138 map {
139 1548 ▶ my ($from, $to) = ref $_ ? @$_ : ($_, $_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 1838 8253 10091
140 10091 ▶ unless (exists $fields->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$from}) 0 10091 10091
141 0 confess $NO_SUCH_CONFIG_ITEM->($self, $from);
142 }
143 10091 $to => $self->{$from}
144 } @_;
145 }
146  
147 614 ▶sub cf_delegate_defined {
Subroutine cf_delegate_defined: called CC=1 SCAR=0.0
Pod undocumented
148 614 my MY $self = shift;
149 614 my $fields = YATT::Lite::Util::fields_hash($self);
150 614 $self->cf_delegate_known(1, $fields, @_);
151 }
152  
153 789 ▶sub cf_delegate_known {
Subroutine cf_delegate_known: called CC=5 SCAR=16.2
Pod undocumented
154 789 (my MY $self, my ($raise_err, $fields)) = splice @_, 0, 3;
155 map {
156 789 ▶ my ($from, $to) = ref $_ ? @$_ : ($_, $_);
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 12179 12179
157 12179 ▶ if (not exists $fields->{$from}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $fields->{$from}) { } 388 11791 12179
158 388 ▶ $raise_err ? (confess $NO_SUCH_CONFIG_ITEM->($self, $from)) : ();
Branch: 1 branch
BranchTrueFalseTotal
$raise_err ? : 0 388 388
159 } else {
160 11791 ▶ defined $self->{$from} ? ($to => $self->{$from}) : ();
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{$from} ? : 3656 8135 11791
161 }
162 } @_;
163 }
164  
165 # Or, say, with_option.
166 # XXX: configure_ZZZ hook is not applied.
167 428 ▶sub cf_let {
Subroutine cf_let: called CC=2 SCAR=7.1
Pod undocumented
168 428 (my MY $self, my ($binding, $task)) = splice @_, 0, 3;
169 428 my ($keys, $values) = $self->cf_bindings(@$binding);
170 428 local @{$self}{@$keys} = @$values;
171 428 ▶ if (ref $task) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $task) { } 428 0 428
172 428 $task->($self, @_);
173 } else {
174 0 $self->$task(@_);
175 }
176 }
177  
178 428 ▶sub cf_bindings {
Subroutine cf_bindings: called CC=3 SCAR=11.0
Pod undocumented
179 428 my MY $self = shift;
180 428 ▶ carp "Odd number of key value bindings" if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 0 428 428
181 428 my (@keys, @values);
182 428 ▶ while (my ($key, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 32 428 460
183 # XXX: key check!
184 # XXX: task extraction!
185 32 push @keys, $key; push @values, $value;
186 }
187 428 (\@keys, \@values);
188 }
189  
190  
191 175 ▶sub cf_unknowns {
Subroutine cf_unknowns: called CC=5 SCAR=18.7
Pod undocumented
192 175 my $self = shift;
193 175 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
01750
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 175 my $fields = YATT::Lite::Util::fields_hash($class);
195 175 my @unknown;
196 175 ▶ while (my ($name, $value) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 6 175 181
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 175 @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 52 ▶sub cf_mkaccessors {
Subroutine cf_mkaccessors: called CC=3 SCAR=11.2
Pod undocumented
227 52 my ($class, @names) = @_;
228 52 my $fields = YATT::Lite::Util::fields_hash($class);
229 52 foreach my $name (@names) {
230 52 ▶ unless ($fields->{$name}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fields->{$name}) 0 52 52
231 0 croak "No such config: $name";
232 }
233 52 ▶ *{YATT::Lite::Util::globref($class, $name)} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
234 1 shift->{$name};
235 52 };
236 }
237 }
238 64 1;
« blib/lib/YATT/Lite/Entities.pm ↑ Summary blib/lib/YATT/Lite/Response.pm »