| 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'} || {}
MC/DC100%1/1
$args{'attribute_evaluator'} || {}
$args{'attribute_evaluator'}
Truth table100%2/2
A
A= $args{'attribute_evaluator'}
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 40 | 52 | $line =~ s/^\s+//g; | |||||||||||||||||||||||||||
| 41 | 52 | ▶ | next if $line eq ""; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | 40 | ▶ | if($line =~ /^([^=]*)=(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 60 | 229 | my $quote = $args{quote}; | |||||||||||||||||||||||||||
| 61 | 229 | my $entries = $args{entries}; | |||||||||||||||||||||||||||
| 62 | 229 | ▶ | if(@$entries == 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 80 | 328 | ▶ | $self->{list}->set($key, $quote ? _wrap_value_with_quote($value) : $value); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 136 | 88 | return @$raw_value; | |||||||||||||||||||||||||||
| 137 | ▶ | }elsif($ref eq "CODE" && defined($evaluator)) { | |||||||||||||||||||||||||||
|
Condition66%2/3
$ref eq "CODE" and defined $evaluator
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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $pdata_with_key and not $pdata_with_key->has_own_entry($key)
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)
| |||||||||||||||||||||||||||||
| 148 | 1049 | $pdata_with_key = $pdata_with_key->get_parent; | |||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||
| 150 | 167 | ▶ | if(!defined($pdata_with_key)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | 12 | ▶ | return wantarray ? () : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $pdata_with_attr and not $pdata_with_attr->has_own_attribute($name)
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)
| |||||||||||||||||||||||||||||
| 176 | 484 | $pdata_with_attr = $pdata_with_attr->get_parent; | |||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | 585 | ▶ | return undef if not defined $pdata_with_attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
ref $raw_value eq "CODE" and defined $self->{"attribute_evaluator"}{$name}
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}
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||