Base/Configure.pm

filter: Statementstmt 56.5% 52 / 92 Branchbran 40.0% 12 / 30 Conditioncond 26.6% 4 / 15 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 73.6% 14 / 19 Podpod 54.5% 6 / 11 totaltotal 51.1% 90 / 176 CC 23 SCAR 27.6
CC
23
Cov
50%
cf_configs
56.0
configure
16.7
cget
12.0
CRAP 15.8
« blib/lib/MOP4Import/Base/JSON.pm ↑ Summary blib/lib/MOP4Import/Base/CLI.pm »
1 package MOP4Import::Base::Configure;
2 20 ▶use MOP4Import::Declare -as_base, -fatal;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 20 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 20 ▶use MOP4Import::Opts;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 use MOP4Import::Types::Extend
9 20 ▶ FieldSpec => [[fields => qw/weakref/]];
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 20 ▶use constant DEBUG_WEAK => $ENV{DEBUG_MOP4IMPORT_WEAKREF};
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 20 our %FIELDS;
14  
15 #---------
16  
17 sub cf_make_object {
18 47 ▶ fields::new(shift);
Subroutine cf_make_object: called CC=1 SCAR=0.0
Pod undocumented
19 }
20  
21 sub new :method {
22 47 ▶ my MY $self = shift->cf_make_object;
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 47 $self->configure(@_);
24 45 $self->before_configure_default;
25 45 $self->after_new;
26 45 $self->configure_default;
27 45 $self->after_configure_default;
28 45 $self;
29 }
30  
31 ▶sub before_configure_default :method {}
Subroutine before_configure_default: called CC=1 SCAR=0.0
Pod documented
32 ▶sub after_new {}; # Should be deprecated near future.
Subroutine after_new: called CC=1 SCAR=0.0
Pod documented
33 ▶sub after_configure_default :method {}
Subroutine after_configure_default: called CC=1 SCAR=0.0
Pod documented
34  
35 sub configure_default :method {
36 45 ▶ (my MY $self, my $target) = @_;
Subroutine configure_default: called CC=5 SCAR=16.2
Pod documented
37  
38 45 ▶ $target //= $self;
Condition33%1/3
$target //= $self
l!l&&r!l&&!r
0450
MC/DC0%0/2
$target //= $self
$target $self
Truth table33%1/3
A //= B
A= $targetB= $self
ABresult
1X1
011
000
39  
40 45 my $fields = MOP4Import::Declare::fields_hash($self);
41  
42 45 ▶ while ((my $name, my FieldSpec $spec) = each %$fields) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$fields) 330 45 375
43 330 ▶ if (not defined $target->{$name} and defined $spec->{default}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $target->{$name} and defined $spec->{"default"}) 99 231 330
Condition100%3/3
not defined $target->{$name} and defined $spec->{"default"}
!ll&&!rl&&r
5317899
MC/DC100%2/2
not defined $target->{$name} and defined $spec->{"default"}
not defined $target->{$name} defined $spec->{"default"}
Truth table100%3/3
not(A) and B
A= not defined $target->{$name}B= defined $spec->{"default"}
ABresult
0X0
100
111
44 99 $target->{$name} = $self->can("default_$name")->($self);
45 }
46 }
47  
48 45 $target;
49 }
50  
51 sub configure :method {
52 142 ▶ (my MY $self) = shift;
Subroutine configure: called CC=10 SCAR=28.1
Pod documented
53  
54 142 my @args = do {
55 142 ▶ if (@_ != 1) {
Branch: 3 branches
BranchTrueFalseTotal
if (@_ != 1) { } 142 0 142
elsif (ref $_[0] eq 'HASH') { } 0 0 0
elsif (my $sub = UNIVERSAL::can($_[0], 'cf_configs')) { } 0 0 0
56 142 @_;
57 } elsif (ref $_[0] eq 'HASH') {
58 0 %{$_[0]}
59 } elsif (my $sub = UNIVERSAL::can($_[0], "cf_configs")) {
60 # Shallow copy via cf_configs()
61 0 $sub->($_[0]);
62 } else {
63 0 Carp::croak "Unknown argument! ".MOP4Import::Util::terse_dump($_[0]);
64 }
65 };
66  
67 142 my $fields = MOP4Import::Declare::fields_hash($self);
68  
69 142 my @setter;
70 142 ▶ while (my ($key, $value) = splice @args, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@args, 0, 2)) 176 142 318
71 176 ▶ unless (defined $key) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $key) 0 176 176
72 0 croak "Undefined option name for class ".ref($self);
73 }
74  
75 176 ▶ if ($key =~ m{^_}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($key =~ /^_/) 0 176 176
76 0 croak "Private option is prohibited for class ".ref($self).": $key";
77 }
78  
79 176 ▶ if (my $sub = $self->can("onconfigure_$key")) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $sub = $self->can("onconfigure_$key")) { } 2 174 176
elsif (exists $fields->{$key}) { } 174 0 174
80 2 push @setter, [$sub, $value];
81 } elsif (exists $fields->{$key}) {
82 174 $self->{$key} = $value;
83 } else {
84 0 croak "Unknown option for class ".ref($self).": ".$key;
85 }
86 }
87  
88 142 $_->[0]->($self, $_->[-1]) for @setter;
89  
90 140 $self;
91 }
92  
93 sub cget :method {
94 0 ▶ my ($self, $key, $default) = @_;
Subroutine cget: not called CC=3 SCAR=24.8
Pod undocumented
95 0 $key =~ s/^--//;
96 0 my $fields = MOP4Import::Declare::fields_hash($self);
97 0 ▶ if (not exists $fields->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $fields->{$key}) 0 0 0
98 0 confess "No such option: $key"
99 }
100 0 ▶ $self->{$key} // $default;
Condition0%0/3
$self->{$key} // $default
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{$key} // $default
$self->{$key} $default
Truth table0%0/3
A // B
A= $self->{$key}B= $default
ABresult
1X1
011
000
101 }
102  
103 sub declare___field_with_weakref :MetaOnly {
104 0 ▶ (my $myPack, my Opts $opts, my FieldSpec $fs, my ($k, $v)) = m4i_args(@_);
Subroutine declare___field_with_weakref: not called CC=2 SCAR=17.9
Pod undocumented
105  
106 0 $fs->{$k} = $v;
107  
108 0 ▶ if ($v) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v) 0 0 0
109 0 my $name = $fs->{name};
110 0 my $setter = "onconfigure_$name";
111 0 print STDERR "# Declaring weakref $setter for $opts->{objpkg}.$name\n"
112 if DEBUG_WEAK;
113 0 *{MOP4Import::Util::globref($opts->{objpkg}, $setter)} = sub {
114 0 ▶ print STDERR "# weaken $opts->{objpkg}.$name\n" if DEBUG_WEAK;
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 weaken($_[0]->{$name} = $_[1]);
116 0 };
117 }
118 20 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 sub cf_configs :method {
121 0 ▶ (my MY $self, my (%opts)) = @_;
Subroutine cf_configs: not called CC=7 SCAR=40.3
Pod undocumented
122 0 my $all = delete $opts{all};
123 0 ▶ if (keys %opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %opts) 0 0 0
124 0 Carp::croak "Unknown option for cf_configs: ".join(", ", sort keys %opts);
125 }
126 0 my $fields = MOP4Import::Util::fields_hash($self);
127 0 my @result;
128 0 foreach my $key ($self->cf_public_fields) {
129 0 ▶ defined (my $val = $self->{$key})
Branch: 1 branch
BranchTrueFalseTotal
defined(my $val = $self->{$key}) or next 0 0 0
130 or next;
131 0 my FieldSpec $spec = $fields->{$key};
132 0 ▶ if (not $all
Branch: 1 branch
BranchTrueFalseTotal
if (not $all and defined $spec->{"default"} and $val eq $spec->{"default"}) 0 0 0
Condition0%0/6
not $all and defined $spec->{"default"} and $val eq $spec->{"default"}
!ll&&!rl&&r
000
not $all and defined $spec->{"default"}
!ll&&!rl&&r
000
MC/DC0%0/3
not $all and defined $spec->{"default"} and $val eq $spec->{"default"}
not $all defined $spec->{"default"} $val eq $spec->{"default"}
Truth table0%0/4
not(A) and B and C
A= not $allB= defined $spec->{"default"}C= $val eq $spec->{"default"}
ABCresult
0XX0
10X0
1100
1111
133 and defined $spec->{default} and $val eq $spec->{default}) {
134 0 next;
135 }
136 0 push @result, $key, MOP4Import::Util::shallow_copy($val);
137 }
138 0 @result;
139 }
140  
141 sub cf_public_fields :method {
142 0 ▶ my $obj_or_class = shift;
Subroutine cf_public_fields: not called CC=1 SCAR=6.9
Pod undocumented
143 0 my $fields = MOP4Import::Util::fields_hash($obj_or_class);
144 0 sort grep {/^[a-z]/i} keys %$fields;
145 }
146  
147 20 1;
148  
149 __END__
« blib/lib/MOP4Import/Base/JSON.pm ↑ Summary blib/lib/MOP4Import/Base/CLI.pm »