blib/lib/Config/Model/IdElementReference.pm

filter: Statementstmt 87.5% 77 / 88 Branchbran 77.2% 17 / 22 Conditioncond 100% 7 / 7 MC/DCmcdc 100% 4 / 4 Subroutinesub 77.7% 7 / 9 Podpod 40.0% 2 / 5 totaltotal 84.4% 114 / 135 CC 18 SCAR 19.1
CC
18
Cov
86%
get_choice_from_referred_to
11.0
BUILD
6.2
reference_info
3.0
CRAP 6.8
« blib/lib/Config/Model/Lister.pm ↑ Summary blib/lib/Config/Model/WarpedNode.pm »
1 #
2 # This file is part of Config-Model
3 #
4 # This software is Copyright (c) 2005-2022 by Dominique Dumont.
5 #
6 # This is free software, licensed under:
7 #
8 # The GNU Lesser General Public License, Version 2.1, February 1999
9 #
10 package Config::Model::IdElementReference 2.169;
11  
12 59 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 59 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 59 ▶use Config::Model::ValueComputer;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 59 ▶use Log::Log4perl qw(get_logger :levels);
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 59 my $logger = get_logger("Tree::Element::IdElementReference");
19  
20 # config_elt is a reference to the object that called new
21 59 has config_elt => ( is => 'ro', isa => 'Config::Model::AnyThing', required => 1, weak_ref => 1 );
22 59 has refer_to => ( is => 'ro', isa => 'Maybe[Str]' );
23 59 has computed_refer_to => ( is => 'ro', isa => 'Maybe[HashRef]' );
24  
25 66 ▶sub BUILD {
Subroutine BUILD: called CC=6 SCAR=18.2
Pod documented
26 66 my $self = shift;
27  
28 66 my $found = scalar grep { defined $self->$_; } qw/refer_to computed_refer_to/;
29  
30 66 ▶ if ( not $found ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $found) { } 0 66 66
elsif ($found > 1) { } 0 66 66
31 0 Config::Model::Exception::Model->throw(
32 object => $self->config_elt,
33 message => "missing " . "refer_to or computed_refer_to parameter"
34 );
35 }
36 elsif ( $found > 1 ) {
37 0 Config::Model::Exception::Model->throw(
38 object => $self->config_elt,
39 message => "cannot specify both " . "refer_to and computed_refer_to parameters"
40 );
41 }
42  
43 66 my $rft = $self->{refer_to};
44 66 ▶ my $crft = $self->{computed_refer_to} || {};
Condition100%2/2
$self->{'computed_refer_to'} || {}
l!l
561
MC/DC100%1/1
$self->{'computed_refer_to'} || {}
$self->{'computed_refer_to'}
Truth table100%2/2
A
A= $self->{'computed_refer_to'}
Aresult
11
00
45 66 my %c_args = %$crft;
46  
47 my $refer_path =
48 defined $rft
49 ? $rft
50 66 ▶ : delete $c_args{formula};
Branch: 1 branch
BranchTrueFalseTotal
defined $rft ? : 61 5 66
51  
52 # split refer_path on + then create as many ValueComputer as
53 # required
54 66 my @references = split /\s+\+\s+/, $refer_path;
55  
56 66 foreach my $single_path (@references) {
57 114 push @{ $self->{compute} }, Config::Model::ValueComputer->new(
58 formula => $single_path,
59 variables => {},
60 %c_args,
61 value_object => $self->{config_elt},
62 114 value_type => 'string' # a reference is always a string
63 );
64 }
65  
66 66 return $self;
67 }
68  
69 # internal
70  
71 # FIXME: do not call back value object -> may recurse
72 247 ▶sub get_choice_from_referred_to {
Subroutine get_choice_from_referred_to: called CC=11 SCAR=24.0
Pod undocumented
73 247 my $self = shift;
74  
75 247 my $config_elt = $self->{config_elt};
76 247 my @enum_choice = $config_elt->get_default_choice;
77  
78 247 foreach my $compute_obj ( @{ $self->{compute} } ) {
79 408 my $user_spec = $compute_obj->compute;
80  
81 408 ▶ next unless defined $user_spec;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $user_spec 3 405 408
82  
83 405 my @path = split( /\s+/, $user_spec );
84  
85 405 $logger->trace("path: @path");
86  
87 405 my $referred_to = eval { $config_elt->grab("@path"); };
88  
89 405 ▶ if (ref $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $@) 2 403 405
90 2 my $e = $@;
91 # don't use $e->full_description as it will recurse badly
92 2 Config::Model::Exception::Model->throw(
93 object => $config_elt,
94 error => "'refer_to' parameter with path '@path': " .$e->description
95 );
96 }
97  
98 403 my $element = pop @path;
99 403 my $obj = $referred_to->parent;
100 403 my $type = $obj->element_type($element);
101  
102 403 my @choice;
103 403 ▶ if ( $type eq 'check_list' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'check_list') { } 9 394 403
elsif ($type eq 'hash') { } 390 4 394
elsif ($type eq 'list') { } 4 0 4
104 9 @choice = $obj->fetch_element($element)->get_checked_list();
105 }
106 elsif ( $type eq 'hash' ) {
107 390 @choice = $obj->fetch_element($element)->fetch_all_indexes();
108 }
109 elsif ( $type eq 'list' ) {
110 4 my $list_obj = $obj->fetch_element($element);
111 4 my $ct = $list_obj->get_cargo_type;
112 4 ▶ if ( $ct eq 'leaf' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct eq 'leaf') { } 4 0 4
113 4 @choice = $list_obj->fetch_all_values( mode => 'user' );
114 }
115 else {
116 0 Config::Model::Exception::Model->throw(
117 object => $obj,
118 message => "element '$element' cargo_type is $ct. " . "Expected 'leaf'"
119 );
120 }
121 }
122 else {
123 0 Config::Model::Exception::Model->throw(
124 object => $obj,
125 message => "element '$element' type is $type. "
126 . "Expected hash or list or check_list"
127 );
128 }
129  
130 # use a hash so choices are unique
131 403 push @enum_choice, @choice;
132 }
133  
134 # prune out repeated items
135 245 my %h;
136 my @unique =
137 245 ▶ grep { my $found = $h{$_} || 0; $h{$_} = 1; not $found; } @enum_choice;
Condition100%2/2
$h{$_} || 0
l!l
12661
MC/DC100%1/1
$h{$_} || 0
$h{$_}
Truth table100%2/2
A
A= $h{$_}
Aresult
11
00
138  
139 245 my @res;
140 245 ▶ if ( $config_elt->value_type eq 'check_list' and $config_elt->ordered ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($config_elt->value_type eq 'check_list' and $config_elt->ordered) { } 3 242 245
Condition100%3/3
$config_elt->value_type eq "check_list" and $config_elt->ordered
!ll&&!rl&&r
1341083
MC/DC100%2/2
$config_elt->value_type eq "check_list" and $config_elt->ordered
$config_elt->value_type eq "check_list" $config_elt->ordered
Truth table100%3/3
A and B
A= $config_elt->value_type eq "check_list"B= $config_elt->ordered
ABresult
0X0
100
111
141 3 @res = @unique;
142 }
143 else {
144 242 @res = sort @unique;
145 }
146  
147 245 $logger->debug( "Setting choice to '", join( "','", @res ), "'" );
148  
149 245 return $config_elt->setup_reference_choice(@res);
150 }
151  
152 1 ▶sub reference_info {
Subroutine reference_info: called CC=3 SCAR=11.0
Pod documented
153 1 my $self = shift;
154 1 my $str = "choice was retrieved with: ";
155  
156 1 foreach my $compute_obj ( @{ $self->{compute} } ) {
157 2 my $path = $compute_obj->formula;
158 2 ▶ $path = defined $path ? "'$path'" : 'undef';
Branch: 1 branch
BranchTrueFalseTotal
defined $path ? : 2 0 2
159 2 $str .= "\n\tpath $path";
160 2 $str .= "\n\t" . $compute_obj->compute_info;
161 }
162 1 return $str;
163 }
164  
165 0 ▶sub compute_obj {
Subroutine compute_obj: not called CC=1 SCAR=6.9
Pod undocumented
166 0 my $self = shift;
167 0 return @{ $self->{compute} };
168 }
169  
170 0 ▶sub reference_path {
Subroutine reference_path: not called CC=1 SCAR=6.9
Pod undocumented
171 0 my $self = shift;
172 0 return map { $_->formula } @{ $self->{compute} };
173 }
174  
175 59 __PACKAGE__->meta->make_immutable;
176  
177 59 1;
178  
179 # ABSTRACT: Refer to id element(s) and extract keys
180  
181 __END__
« blib/lib/Config/Model/Lister.pm ↑ Summary blib/lib/Config/Model/WarpedNode.pm »