Base/Configure.pm

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