blib/lib/CXC/Gnuplot/V1/Util.pm

filter: Statementstmt 27.5% 33 / 120 Branchbran 0.0% 0 / 64 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 32.3% 11 / 34 Podpod 100% 11 / 11 totaltotal 24.0% 55 / 229 CC 33 SCAR 33.4
CC
33
Cov
18%
__ANON__
110.0
pvalidate
42.0
__ANON__
30.0
CRAP 28.3
« blib/lib/CXC/Gnuplot/V0/AxisLabel.pm ↑ Summary blib/lib/CXC/Gnuplot/V0/Timestamp.pm »
1 package CXC::Gnuplot::V1::Util;
2  
3 4 ▶use v5.38;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 our $VERSION = 'v0.29.3';
5  
6 4 ▶use experimental 'declared_refs', 'for_list', 'builtin', 'try';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 use Exporter::Shiny
9 4 'flatten_array',
10 'render_set',
11 'gnuplot_color',
12 'gnuplot_color_names',
13 'maybe_quote',
14 'pvalidate',
15 'pvalidate_xy_pair',
16 'quote',
17 'render_opts',
18 'to_hash_r',
19 'clone_object',
20 4 ▶ ;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # use namespace::autoclean;
23 4 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Ref::Util 'is_plain_arrayref', 'is_plain_hashref', 'is_plain_ref', 'is_ref', 'is_blessed_ref';
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use builtin::compat 'blessed', 'load_module', 'trim', 'is_bool', 'true', 'false';
Subroutine BEGIN: called CC=1 SCAR=0.0
26 4 ▶use CXC::Data::Visitor 'visit', 'VISIT_ALL', 'RESULT_CONTINUE';
Subroutine BEGIN: called CC=1 SCAR=0.0
27 4 ▶use B ();
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 4 ▶use constant BASE => __PACKAGE__ =~ s/[^:]+$//r;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 my sub croak {
32 0 ▶ require Carp;
Subroutine croak: not called CC=1 SCAR=6.9
33 0 goto \&Carp::croak;
34 }
35  
36 ▶my sub rewrite_prefix ( $module ) {
Subroutine rewrite_prefix: not called CC=3 SCAR=24.8
37 0 state $root = ( __PACKAGE__ =~ s/::Util//r ) . q{::};
38 return
39 0 ▶ defined $module
Branch: 2 branches
BranchTrueFalseTotal
defined $module ? : 0 0 0
substr($module, 0, 1) eq '+' ? : 0 0 0
40 ? substr( $module, 0, 1 ) eq q{+}
41 ? $module
42 : $root . $module
43 : undef;
44 }
45  
46 # not very efficient
47  
48  
49  
50  
51  
52 ▶sub flatten_array ( $array ) {
Subroutine flatten_array: not called CC=3 SCAR=24.8
Pod documented
53 return is_plain_arrayref( $array )
54 0 ▶ ? [ map { is_plain_arrayref( $_ ) ? __SUB__->( $_ )->@* : $_; } $array->@* ]
Branch: 2 branches
BranchTrueFalseTotal
is_plain_arrayref($array) ? : 0 0 0
is_plain_arrayref($_) ? : 0 0 0
55 : [$array];
56 }
57  
58  
59  
60  
61  
62 ▶sub render_set( $set, $format ) {
Subroutine render_set: not called CC=4 SCAR=30.0
Pod documented
63 ## no critic(NamingConventions::ProhibitAmbiguousNames)
64 0 my \@set = $set;
65 return
66 $format eq 'array' ? @set
67 0 ▶ : $format eq 'flatten_array' ? map { flatten_array( $_ ) } @set
Branch: 1 branch
BranchTrueFalseTotal
$format eq 'string' ? : 0 0 0
68 0 ▶ : $format eq 'string' ? map { join q{ }, flatten_array( $_ )->@* } @set
Branch: 2 branches
BranchTrueFalseTotal
$format eq 'array' ? : 0 0 0
$format eq 'flatten_array' ? : 0 0 0
69 : croak( 'unknown format: ' . $format );
70  
71 }
72  
73  
74  
75  
76  
77 ▶sub maybe_quote( $str ) {
Subroutine maybe_quote: not called CC=2 SCAR=17.9
Pod documented
78 0 ▶ return $str =~ /^(['"]).*(\1)$/ ? $str : B::cstring( $str );
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^(['"]).*(\1)$/u ? : 0 0 0
79 }
80  
81  
82  
83  
84  
85 ▶sub quote( $str ) {
Subroutine quote: not called CC=1 SCAR=6.9
Pod documented
86 0 return B::cstring( $str );
87 }
88  
89 ## no critic (NamingConventions::ProhibitAmbiguousNames )
90 ▶my sub merge ( $left, $right ) {
Subroutine merge: not called CC=1 SCAR=6.9
91 0 state $merger = do {
92 0 my $obj = Hash::Merge->new;
93 $obj->add_behavior_spec( {
94 'SCALAR' => {
95 0 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 ▶ 'ARRAY' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
97 0 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
98 },
99 'ARRAY' => {
100 0 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
101 0 ▶ 'ARRAY' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 ▶ 'HASH' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
103 },
104 'HASH' => {
105 0 ▶ 'SCALAR' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
106 0 ▶ 'ARRAY' => sub { $_[1] },
Subroutine __ANON__: not called CC=1 SCAR=6.9
107 ▶ 'HASH' => sub ( $left, $right ) {
Subroutine __ANON__: not called CC=10 SCAR=47.0
108 0 my \%left = $left;
109 0 my \%right = $right;
110  
111 # delete the specified entries from $left
112 0 ▶ if ( defined( my $delete = delete $right{-delete} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $delete = delete $right{"-delete"})) 0 0 0
113 # delete everything
114 0 ▶ if ( is_bool( $delete ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::is_bool($delete)) { } 0 0 0
115 0 ▶ %left = () if $delete;
Branch: 1 branch
BranchTrueFalseTotal
if $delete 0 0 0
116 }
117 else {
118 # delete specific keys
119 0 ▶ delete @left{ is_plain_arrayref( $delete ) ? $delete->@* : $delete };
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($delete) ? : 0 0 0
120 }
121 }
122  
123 0 ▶ if ( defined( my $overwrite = delete $right{-overwrite} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $overwrite = delete $right{"-overwrite"})) 0 0 0
124  
125 # overwrite keys in $left with keys in $right
126 # removing them from %right so they can't be merged
127 my @keys
128 0 ▶ = is_bool( $overwrite ) ? ( $overwrite ? keys %right : () )
Branch: 3 branches
BranchTrueFalseTotal
builtin::is_bool($overwrite) ? : 0 0 0
$overwrite ? : 0 0 0
is_plain_arrayref($overwrite) ? : 0 0 0
129 : is_plain_arrayref( $overwrite ) ? $overwrite->@*
130 : ( $overwrite );
131 0 @left{@keys} = delete @right{@keys};
132 }
133  
134  
135 0 ▶ return \%left unless keys %right;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %right 0 0 0
136  
137 # merge what's left
138 # _merge_hashes is part of the public API
139 ## no critic ( Subroutines::ProtectPrivateSubs )
140 0 Hash::Merge::_merge_hashes( \%left, \%right );
141 },
142 },
143 },
144 0 __PACKAGE__,
145 );
146 # this is on by default, but let's make it explicit
147 0 $obj->set_clone_behavior( true );
148 0 $obj;
149 };
150  
151 0 $merger->merge( $left, $right );
152 }
153  
154  
155  
156  
157  
158 ▶sub clone_object ( $object, $new_attr ) {
Subroutine clone_object: not called CC=1 SCAR=6.9
Pod documented
159  
160 0 my $class = blessed( $object );
161 0 my %new = $new_attr->%*;
162 0 my \%old = $object->to_hash;
163 0 my \%attr = merge( \%old, \%new );
164  
165 0 return $class->new( %attr );
166 }
167  
168  
169  
170  
171  
172  
173  
174 ▶sub pvalidate ( $name, $type, $value_ref ) {
Subroutine pvalidate: not called CC=6 SCAR=37.4
Pod documented
175  
176 0 ▶ return if !defined $value_ref->$*;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$value_ref 0 0 0
177  
178 0 try {
179 0 ▶ if ( $type isa Type::Tiny ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($type isa 'Type::Tiny') { } 0 0 0
elsif (not is_ref($type)) { } 0 0 0
180 0 ▶ if ( $type->has_coercion ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($type->has_coercion) { } 0 0 0
181 0 $value_ref->$* = $type->assert_coerce( $value_ref->$* );
182 }
183 else {
184 0 $type->assert_valid( $value_ref->$* );
185 }
186 }
187  
188 elsif ( !is_ref $type ) {
189 0 my $class = BASE . $type;
190 0 ▶ return if $value_ref->$* isa $class;
Branch: 1 branch
BranchTrueFalseTotal
if $$value_ref isa $class 0 0 0
191 0 load_module( $class );
192  
193 0 $value_ref->$* = $class->new( $value_ref->$* );
194 }
195 }
196 catch ( $e ) {
197 0 croak( qq{unable to validate or coerce "$name" parameter: $e} );
198 };
199 }
200  
201  
202  
203  
204  
205 ▶sub pvalidate_xy_pair ( $name, $type, $value_ref ) {
Subroutine pvalidate_xy_pair: not called CC=4 SCAR=30.0
Pod documented
206  
207 0 my $value = $value_ref->$*;
208  
209 0 ▶ return if !defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 0 0
210  
211 0 pvalidate( $name => $type, $value_ref );
212 0 ▶ $value_ref->$* = $value = { x => $value }
Branch: 1 branch
BranchTrueFalseTotal
unless is_plain_hashref($value) 0 0 0
213 unless is_plain_hashref( $value );
214  
215 defined $value->{x}
216 0 ▶ or croak( qq{must specify an "x" $name} );
Branch: 1 branch
BranchTrueFalseTotal
defined $value->{"x"} or croak qq[must specify an "x" $name] 0 0 0
217  
218 }
219  
220  
221 my sub _colors {
222  
223 1 ▶ state @colors = do {
Subroutine _colors: called CC=1 SCAR=2.0
224 1 require File::ShareDir::Tarball;
225 1 require Path::Tiny;
226 1 require CXC::Gnuplot::V1::Color;
227  
228 # the share/gnuplot_colors file is derived from the gnuplot 'show colors' command
229 0 my $file
230 = Path::Tiny::path( File::ShareDir::Tarball::dist_file( 'CXC-Gnuplot', 'gnuplot_colors' ) );
231  
232 ## no critic (BuiltinFunctions::ProhibitComplexMappings)
233 map {
234 0 my %color;
235 0 @color{ 'name', 'rgb', 'r', 'g', 'b' } = split /[\h=]+/;
236 0 CXC::Gnuplot::V1::Color->new( %color );
237 } grep length, map trim( $_ ), $file->lines;
238 };
239  
240 0 return @colors;
241  
242 }
243  
244  
245  
246  
247  
248 sub gnuplot_color_names {
249 1 ▶ state @names = map { $_->name } _colors;
Subroutine gnuplot_color_names: called CC=1 SCAR=2.6
Pod documented
250 0 return @names;
251 }
252  
253  
254  
255  
256  
257 ▶sub gnuplot_color ( $name ) {
Subroutine gnuplot_color: not called CC=1 SCAR=6.9
Pod documented
258 0 state %color = map { $_->name => $_ } _colors;
259 0 return $color{$name};
260 }
261  
262  
263  
264  
265  
266 ▶sub to_hash_r ( $hash ) {
Subroutine to_hash_r: not called CC=1 SCAR=6.9
Pod documented
267  
268 ▶ visit(
Subroutine __ANON__: not called CC=5 SCAR=34.0
269 $hash,
270 sub ( $kydx, $vref, $context, $metadata ) {
271  
272 # make copies of containers.
273 0 ▶ if ( is_plain_arrayref $vref->$* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_arrayref($$vref)) 0 0 0
274 0 $vref->$* = [ $vref->$*->@* ];
275 0 return RESULT_CONTINUE;
276 }
277  
278 0 ▶ if ( is_plain_hashref $vref->$* ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_plain_hashref($$vref)) 0 0 0
279 0 $vref->$* = { $vref->$*->%* };
280 0 return RESULT_CONTINUE;
281 }
282  
283 0 ▶ return RESULT_CONTINUE
Branch: 1 branch
BranchTrueFalseTotal
unless is_blessed_ref($$vref) 0 0 0
284 unless is_blessed_ref $vref->$*;
285 0 my $obj = $vref->$*;
286 0 my $mth = $obj->can( 'to_hash' );
287  
288 0 ▶ croak( 'no means of converting object to hash at: ' . join( q{.}, $metadata->{path}->@* ) )
Branch: 1 branch
BranchTrueFalseTotal
unless defined $mth 0 0 0
289 if !defined $mth;
290  
291 0 $vref->$* = $obj->$mth;
292 0 return RESULT_CONTINUE;
293 },
294 0 visit => VISIT_ALL,
295 );
296  
297 0 $hash;
298 }
299  
300  
301  
302  
303  
304 ▶sub render_opts ( $label, $value ) {
Subroutine render_opts: not called CC=4 SCAR=30.0
Pod documented
305  
306 0 ▶ !defined $value and return ();
Branch: 1 branch
BranchTrueFalseTotal
defined $value or return () 0 0 0
307  
308 0 ▶ !is_blessed_ref $value
Branch: 1 branch
BranchTrueFalseTotal
is_blessed_ref($value) or croak "value is not an object" 0 0 0
309 and croak( 'value is not an object' );
310  
311 0 ▶ my @opts = ( is_plain_arrayref( $label ) ? $label->@* : $label, $value->opts );
Branch: 1 branch
BranchTrueFalseTotal
is_plain_arrayref($label) ? : 0 0 0
312  
313 0 return \@opts;
314 }
315  
316 4 1;
317  
318 __END__
« blib/lib/CXC/Gnuplot/V0/AxisLabel.pm ↑ Summary blib/lib/CXC/Gnuplot/V0/Timestamp.pm »