blib/lib/Gnuplot/Builder/PrototypedData.pm

filter: Statementstmt 100% 124 / 124 Branchbran 100% 48 / 48 Conditioncond 92.8% 13 / 14 MC/DCmcdc 88.8% 8 / 9 Subroutinesub 100% 27 / 27 Podpod 100% 13 / 13 totaltotal 99.1% 233 / 235 SCAR 35.0
CC
33
Cov
99%
_parse_pairs
7.0
get_resolved_entry
6.0
get_resolved_attribute
6.0
CRAP 33.0
« blib/lib/Gnuplot/Builder/Script.pm ↑ Summary blib/lib/Gnuplot/Builder/PartiallyKeyedList.pm »
1 package Gnuplot::Builder::PrototypedData;
2 57 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 57 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 57 ▶use Gnuplot::Builder::PartiallyKeyedList;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 57 ▶use Gnuplot::Builder::Util qw(quote_gnuplot_str);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 57 ▶use List::Util 1.28 qw(pairs);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 sub new {
9 1422 ▶ my ($class, %args) = @_;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
10 my $self = bless {
11 list => Gnuplot::Builder::PartiallyKeyedList->new,
12 attributes => {},
13 parent => undef,
14 entry_evaluator => $args{entry_evaluator},
15 attribute_evaluator => $args{attribute_evaluator} || {},
16 1422 ▶ }, $class;
Condition100%2/2
$args{'attribute_evaluator'} || {}
l!l
1241298
MC/DC100%1/1
$args{'attribute_evaluator'} || {}
$args{'attribute_evaluator'}
Truth table100%2/2
A
A= $args{'attribute_evaluator'}
Aresult
11
00
17 1422 return $self;
18 }
19  
20 sub _trim_whitespaces {
21 67 ▶ my ($val) = @_;
Subroutine _trim_whitespaces: called CC=1 SCAR=0.0
22 67 $val =~ s/^\s+//g;
23 67 $val =~ s/\s+$//g;
24 67 return $val;
25 }
26  
27 sub _parse_pairs {
28 15 ▶ my ($pairs_str) = @_;
Subroutine _parse_pairs: called CC=7 SCAR=19.5
29 15 my @pairs = ();
30 15 my $carried = "";
31 15 foreach my $line (split /^/, $pairs_str) {
32 62 $line =~ s/[\r\n]+$//g;
33 62 ▶ if($line =~ /\\$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /\\$/) 6 56 62
34 6 $carried .= substr($line, 0, -1);
35 6 next;
36 }
37 56 $line = $carried . $line;
38 56 $carried = "";
39 56 ▶ next if $line =~ /^#/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^#/ 4 52 56
40 52 $line =~ s/^\s+//g;
41 52 ▶ next if $line eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $line eq "" 12 40 52
42 40 ▶ if($line =~ /^([^=]*)=(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /^([^=]*)=(.*)$/) { } 27 13 40
43 27 my ($name, $value) = ($1, $2);
44 27 push(@pairs, _trim_whitespaces($name), _trim_whitespaces($value));
45 }else {
46 13 my $name = _trim_whitespaces($line);
47 13 ▶ if($name =~ /^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^-/) { } 6 7 13
48 6 push(@pairs, substr($name, 1), undef);
49 }else {
50 7 push(@pairs, $name, "");
51 }
52 }
53 }
54 15 return \@pairs;
55 }
56  
57 sub set_entry {
58 229 ▶ my ($self, %args) = @_;
Subroutine set_entry: called CC=5 SCAR=16.1
Pod documented
59 229 ▶ my $prefix = defined($args{key_prefix}) ? $args{key_prefix} : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'key_prefix'} ? : 111 118 229
60 229 my $quote = $args{quote};
61 229 my $entries = $args{entries};
62 229 ▶ if(@$entries == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$entries == 1) 15 214 229
63 15 $entries = _parse_pairs($entries->[0]);
64 }
65
66 ## Multiple occurrences of the same key are combined into an array-ref value.
67 229 my $temp_list = Gnuplot::Builder::PartiallyKeyedList->new;
68 229 foreach my $entry_pair (pairs @$entries) {
69 330 my ($given_key, $value) = @$entry_pair;
70 330 my $key = $prefix . $given_key;
71 330 ▶ if($temp_list->exists($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($temp_list->exists($key)) { } 2 328 330
72 2 push(@{$temp_list->get($key)}, $value);
73 }else {
74 328 $temp_list->set($key, [$value]);
75 }
76 }
77 $temp_list->each(sub {
78 328 ▶ my ($key, $value_arrayref) = @_;
Subroutine __ANON__: called CC=3 SCAR=11.0
79 328 ▶ my $value = (@$value_arrayref == 1) ? $value_arrayref->[0] : $value_arrayref;
Branch: 1 branch
BranchTrueFalseTotal
@$value_arrayref == 1 ? : 326 2 328
80 328 ▶ $self->{list}->set($key, $quote ? _wrap_value_with_quote($value) : $value);
Branch: 1 branch
BranchTrueFalseTotal
$quote ? : 54 274 328
81 229 });
82 }
83  
84 sub _wrap_value_with_quote {
85 90 ▶ my ($value) = @_;
Subroutine _wrap_value_with_quote: called CC=3 SCAR=11.0
86 90 my $ref = ref($value);
87 90 ▶ if($ref eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 10 80 90
elsif ($ref eq 'CODE') { } 17 63 80
88 10 return [map { quote_gnuplot_str($_) } @$value];
89 }elsif($ref eq "CODE") {
90 return sub {
91 34 ▶ return map { quote_gnuplot_str($_) } $value->(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
92 17 };
93 }else {
94 63 return quote_gnuplot_str($value);
95 }
96 }
97  
98 sub add_entry {
99 20 ▶ my ($self, @entries) = @_;
Subroutine add_entry: called CC=2 SCAR=6.9
Pod documented
100 20 $self->{list}->add($_) foreach @entries;
101 }
102  
103 13 ▶sub delete_entry { $_[0]->{list}->delete($_[1]) }
Subroutine delete_entry: called CC=1 SCAR=0.0
Pod documented
104  
105 1204 ▶sub has_own_entry { return $_[0]->{list}->exists($_[1]) }
Subroutine has_own_entry: called CC=1 SCAR=0.0
Pod documented
106  
107 1037 ▶sub set_parent { $_[0]->{parent} = $_[1] }
Subroutine set_parent: called CC=1 SCAR=0.0
Pod documented
108  
109 2912 ▶sub get_parent { return $_[0]->{parent} }
Subroutine get_parent: called CC=1 SCAR=0.0
Pod documented
110  
111 sub _create_inheritance_stack {
112 350 ▶ my ($self) = @_;
Subroutine _create_inheritance_stack: called CC=2 SCAR=6.9
113 350 my @pdata_stack = ($self);
114 350 my $current = $self;
115 350 ▶ while(defined(my $parent = $current->get_parent)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $parent = $current->get_parent)) 1029 350 1379
116 1029 push(@pdata_stack, $parent);
117 1029 $current = $parent;
118 }
119 350 return \@pdata_stack;
120 }
121  
122 sub _create_merged_pkl {
123 350 ▶ my ($self) = @_;
Subroutine _create_merged_pkl: called CC=2 SCAR=6.9
124 350 my $result = Gnuplot::Builder::PartiallyKeyedList->new;
125 350 my $pdata_stack = $self->_create_inheritance_stack();
126 350 ▶ while(defined(my $cur_pdata = pop(@$pdata_stack))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cur_pdata = pop @$pdata_stack)) 1379 350 1729
127 1379 $result->merge($cur_pdata->{list});
128 }
129 350 return $result;
130 }
131  
132 sub _normalize_value {
133 599 ▶ my ($raw_value, $evaluator, $key) = @_;
Subroutine _normalize_value: called CC=4 SCAR=13.9
134 599 my $ref = ref($raw_value);
135 599 ▶ if($ref eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref eq 'ARRAY') { } 88 511 599
elsif ($ref eq 'CODE' and defined $evaluator) { } 90 421 511
136 88 return @$raw_value;
137 ▶ }elsif($ref eq "CODE" && defined($evaluator)) {
Condition66%2/3
$ref eq "CODE" and defined $evaluator
!ll&&!rl&&r
421090
MC/DC50%1/2
$ref eq "CODE" and defined $evaluator
$ref eq "CODE" defined $evaluator
Truth table66%2/3
A and B
A= $ref eq "CODE"B= defined $evaluator
ABresult
0X0
100
111
138 90 return $evaluator->($key, $raw_value);
139 }else {
140 421 return ($raw_value);
141 }
142 }
143  
144 sub get_resolved_entry {
145 167 ▶ my ($self, $key) = @_;
Subroutine get_resolved_entry: called CC=6 SCAR=17.9
Pod documented
146 167 my $pdata_with_key = $self;
147 167 ▶ while(defined($pdata_with_key) && !$pdata_with_key->has_own_entry($key)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdata_with_key and not $pdata_with_key->has_own_entry($key)) 1049 167 1216
Condition100%3/3
defined $pdata_with_key and not $pdata_with_key->has_own_entry($key)
!ll&&!rl&&r
121551049
MC/DC100%2/2
defined $pdata_with_key and not $pdata_with_key->has_own_entry($key)
defined $pdata_with_key not $pdata_with_key->has_own_entry($key)
Truth table100%3/3
A and not(B)
A= defined $pdata_with_keyB= not $pdata_with_key->has_own_entry($key)
ABresult
0X0
100
111
148 1049 $pdata_with_key = $pdata_with_key->get_parent;
149 }
150 167 ▶ if(!defined($pdata_with_key)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pdata_with_key) 12 155 167
151 12 ▶ return wantarray ? () : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 5 12
152 }
153 155 my $raw_value = $pdata_with_key->{list}->get($key);
154 155 my @normalized_values = _normalize_value($raw_value, $self->{entry_evaluator}, $key);
155 155 ▶ return wantarray ? @normalized_values : $normalized_values[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 77 78 155
156 }
157  
158 sub each_resolved_entry {
159 350 ▶ my ($self, $code) = @_;
Subroutine each_resolved_entry: called CC=1 SCAR=0.0
Pod documented
160 350 my $merged = $self->_create_merged_pkl();
161 $merged->each(sub {
162 444 ▶ my ($key, $raw_value) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
163 444 $code->($key, [_normalize_value($raw_value, $self->{entry_evaluator}, $key)]);
164 350 });
165 }
166  
167 sub set_attribute {
168 145 ▶ my ($self, %args) = @_;
Subroutine set_attribute: called CC=2 SCAR=6.9
Pod documented
169 145 ▶ $self->{attributes}{$args{key}} = $args{quote} ? _wrap_value_with_quote($args{value}) : $args{value};
Branch: 1 branch
BranchTrueFalseTotal
$args{'quote'} ? : 36 109 145
170 }
171  
172 sub get_resolved_attribute {
173 585 ▶ my ($self, $name) = @_;
Subroutine get_resolved_attribute: called CC=6 SCAR=17.9
Pod documented
174 585 my $pdata_with_attr = $self;
175 585 ▶ while(defined($pdata_with_attr) && !$pdata_with_attr->has_own_attribute($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $pdata_with_attr and not $pdata_with_attr->has_own_attribute($name)) 484 585 1069
Condition100%3/3
defined $pdata_with_attr and not $pdata_with_attr->has_own_attribute($name)
!ll&&!rl&&r
413172484
MC/DC100%2/2
defined $pdata_with_attr and not $pdata_with_attr->has_own_attribute($name)
defined $pdata_with_attr not $pdata_with_attr->has_own_attribute($name)
Truth table100%3/3
A and not(B)
A= defined $pdata_with_attrB= not $pdata_with_attr->has_own_attribute($name)
ABresult
0X0
100
111
176 484 $pdata_with_attr = $pdata_with_attr->get_parent;
177 }
178 585 ▶ return undef if not defined $pdata_with_attr;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pdata_with_attr 413 172 585
179 172 my $raw_value = $pdata_with_attr->{attributes}{$name};
180 172 ▶ if(ref($raw_value) eq "CODE" && defined($self->{attribute_evaluator}{$name})) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $raw_value eq 'CODE' and defined $self->{'attribute_evaluator'}{$name}) { } 11 161 172
Condition100%3/3
ref $raw_value eq "CODE" and defined $self->{"attribute_evaluator"}{$name}
!ll&&!rl&&r
1461511
MC/DC100%2/2
ref $raw_value eq "CODE" and defined $self->{"attribute_evaluator"}{$name}
ref $raw_value eq "CODE" defined $self->{"attribute_evaluator"}{$name}
Truth table100%3/3
A and B
A= ref $raw_value eq "CODE"B= defined $self->{"attribute_evaluator"}{$name}
ABresult
0X0
100
111
181 11 my ($result) = $self->{attribute_evaluator}{$name}->($name, $raw_value);
182 11 return $result;
183 }else {
184 161 return $raw_value;
185 }
186 }
187  
188 656 ▶sub has_own_attribute { exists $_[0]->{attributes}{$_[1]} }
Subroutine has_own_attribute: called CC=1 SCAR=0.0
Pod documented
189  
190 20 ▶sub delete_attribute { delete $_[0]->{attributes}{$_[1]} }
Subroutine delete_attribute: called CC=1 SCAR=0.0
Pod documented
191  
192  
193 57 1;
194  
195 __END__
« blib/lib/Gnuplot/Builder/Script.pm ↑ Summary blib/lib/Gnuplot/Builder/PartiallyKeyedList.pm »