blib/lib/CXC/Number/Sequence/Utils.pm

filter: Statementstmt 95.7% 67 / 70 Branchbran 78.5% 11 / 14 Conditioncond 66.6% 2 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 15 / 15 Podpod 100% 2 / 2 totaltotal 91.5% 97 / 106 CC 13 SCAR 15.4
CC
13
Cov
92%
__ANON__
9.8
load_class
5.0
buildargs_factory
1.0
CRAP 4.7
« blib/lib/CXC/Number/Grid/Tree.pm ↑ Summary blib/lib/CXC/Number/Grid.pm »
1 package CXC::Number::Sequence::Utils;
2  
3 # ABSTRACT: Utilities for CXC::Number::Sequence generators
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use v5.28;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 12 our $VERSION = '0.12';
11  
12 # ABSTRACT: sequence utilities
13  
14 12 ▶use Exporter::Shiny qw( buildargs_factory load_class );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 12 ▶use Type::Params qw( compile_named compile_named_oo );
Subroutine BEGIN: called CC=1 SCAR=0.0
16 12 ▶use Types::Standard -types;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 12 ▶use Types::Common::Numeric qw( PositiveInt );
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 12 ▶use CXC::Number::Sequence::Failure -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 use Hash::Wrap 0.11 {
22 12 -as => 'wrap_attrs_ro',
23 -immutable => 1,
24 -exists => 'has',
25 12 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
26 12 ▶use Hash::Wrap { -as => 'wrap_attrs_rw' };
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 12 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117 sub buildargs_factory {
118  
119 6 ▶ state $check = compile_named_oo(
Subroutine buildargs_factory: called CC=1 SCAR=0.1
Pod documented
120 map => HashRef [
121 Dict [
122 flag => PositiveInt,
123 type => InstanceOf ['Type::Tiny'],
124 ],
125 ],
126 build => Map [ PositiveInt, CodeRef ],
127 xvalidate => Optional [ ArrayRef [ Tuple [ PositiveInt, CodeRef ] ] ],
128 adjust => Optional [CodeRef],
129 );
130  
131 6 my $arg = $check->( @_ );
132  
133 return sub {
134  
135 ## no critic( Variables::ProhibitReusedNames )
136  
137 state $check = compile_named(
138 62 ▶ map { $_ => $arg->map->{$_}{type} }
Subroutine __ANON__: called CC=9 SCAR=22.8
139 keys $arg->map->%*,
140 );
141  
142 62 my ( $class, undef ) = ( shift, shift );
143  
144 62 ▶ my %attrs = @_ == 1 ? $_[0]->%* : @_;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 62 62
145  
146 # don't touch attributes we don't know about
147 62 my %build_attrs;
148 62 $build_attrs{$_} = delete $attrs{$_} foreach grep { exists $arg->map->{$_} } keys %attrs;
149  
150 62 ▶ if ( $arg->adjust ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->adjust) 0 62 62
151 0 local $_ = \%build_attrs;
152 0 $arg->adjust->();
153 }
154  
155 62 my $attrs = $check->( %build_attrs );
156  
157 61 my $attrs_set = 0;
158 61 $attrs_set |= $arg->map->{$_}{flag} for keys %build_attrs;
159  
160 61 ▶ my $build = $arg->build->{$attrs_set} // parameter_IllegalCombination->throw(
Condition66%2/3
$arg->build->{$attrs_set} // parameter_IllegalCombination()->throw('illegal combination of parameters: ' . join(", ", sort(keys %build_attrs)))
l!l&&r!l&&!r
5704
MC/DC0%0/2
$arg->build->{$attrs_set} // parameter_IllegalCombination()->throw('illegal combination of parameters: ' . join(", ", sort(keys %build_attrs)))
$arg->build->{$attrs_set} parameter_IllegalCombination()->throw('illegal combination of parameters: ' . join(", ", sort(keys %build_attrs)))
Truth table33%1/3
A // B
A= $arg->build->{$attrs_set}B= parameter_IllegalCombination()->throw('illegal combination of parameters: ' . join(", ", sort(keys %build_attrs)))
ABresult
1X1
011
000
161 'illegal combination of parameters: ' . join( ', ', sort keys %build_attrs ) );
162  
163 57 ▶ if ( $arg->has_xvalidate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arg->has_xvalidate) 57 0 57
164 57 local $_ = wrap_attrs_rw( $attrs );
165 57 foreach my $pair ( $arg->xvalidate->@* ) {
166 355 my ( $key, $validate ) = $pair->@*;
167 355 ▶ next unless $key == ( $attrs_set & $key );
Branch: 1 branch
BranchTrueFalseTotal
unless $key == ($attrs_set & $key) 310 45 355
168 45 $validate->();
169 }
170 }
171  
172 55 local $_ = wrap_attrs_ro( $attrs );
173 0 return { %attrs, $build->()->%* };
174 6 };
175 }
176  
177  
178  
179  
180  
181  
182  
183  
184  
185 # based on Mojo::Plugin::load_plugin, Mojo::Loader::load_class, Mojo::Util::camelize
186 ▶sub load_class ( $name ) {
Subroutine load_class: called CC=5 SCAR=16.1
Pod documented
187  
188 $name = join q{::}, map {
189 5 ▶ join( q{}, map { ucfirst lc } split /_/ )
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^[[:upper:]]/u 3 2 5
190 } split( /-/, $name )
191 unless $name =~ /^[[:upper:]]/;
192  
193 5 for my $class ( "CXC::Number::Sequence::${name}", $name ) {
194 ## no critic (BuiltinFunctions::ProhibitStringyEval)
195 8 ▶ eval "require $class; 1"
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class; 1" and return $class 3 5 8
196 && return $class;
197  
198 ## no critic( RegularExpressions::ProhibitUnusualDelimiters )
199 5 ▶ loadclass_CompileError->throw( "$class had a compile error: $@" )
Branch: 1 branch
BranchTrueFalseTotal
unless $@ =~ m[Can't locate \Q@{[$class =~ s[::][/]gru . '.pm'];}\E]u 1 4 5
200 5 unless $@ =~ m|Can't locate \Q@{[ $class =~ s{::}{/}gr . '.pm' ]}|;
201 }
202  
203 1 loadclass_NoClass->throw( "unable to find Sequence class matching $name" );
204 }
205  
206 #
207 # This file is part of CXC-Number
208 #
209 # This software is Copyright (c) 2019 by Smithsonian Astrophysical Observatory.
210 #
211 # This is free software, licensed under:
212 #
213 # The GNU General Public License, Version 3, June 2007
214 #
215  
216 12 1;
217  
218 __END__
219  
220 =pod
221  
222 =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory CamelCased bitmask
223 xvalidate
224  
225 =head1 NAME
226  
227 CXC::Number::Sequence::Utils - Utilities for CXC::Number::Sequence generators
228  
229 =head1 VERSION
230  
231 version 0.12
232  
233 =head1 SYNOPSIS
234  
235 =head1 SUBROUTINES
236  
237 =head2 buildargs_factory
238  
239 $sub = buildargs_factory( %args );
240  
241 Generate a subroutine wrapper for L<Moo/BUILDARGS> for use with L<Moo/around>, e.g.
242  
243 around BUILDARGS => buildargs_factory(
244 map => \%ArgMap,
245 build => \%ArgBuild,
246 xvalidate => \@ArgsCrossValidate
247 );
248  
249 It takes the following arguments:
250  
251 =over
252  
253 =item map => \%hash
254  
255 This hash maps a parameter name to a L<Type::Tiny> type and a bitmask
256 flag which uniquely identifies the parameter. The hash keys are the
257 parameter names, and the values are hashes with elements keys C<type>
258 (the C<Type::Tiny> type) and C<flag> (an integer bitmask flag). For
259 example,
260  
261 use enum qw( BITMASK: MIN MAX SOFT_MIN SOFT_MAX NBINS BINW RATIO GROW );
262  
263 my %ArgMap = (
264 binw => { type => BinWidth, flag => BINW },
265 max => { type => Optional [BigNum], flag => MAX },
266 min => { type => Optional [BigNum], flag => MIN },
267 nbins => { type => Optional [PositiveInt], flag => NBINS },
268 ratio => { type => Ratio, flag => RATIO },
269 soft_max => { type => Optional [BigNum], flag => SOFT_MAX },
270 soft_min => { type => Optional [BigNum], flag => SOFT_MIN },
271 );
272  
273 =item build => \%hash
274  
275 This hash maps I<combinations> of parameters with subroutines which
276 return parameters to be returned by L<Moo/BUILDARGS>. The keys are
277 masks which specify the parameters, and the values are subroutines
278 which operate on C<$_> (an object with methods named for the
279 parameters). For example,
280  
281 ( MIN | NBINS | BINW | RATIO ),
282 sub {
283 my $nbins = $_->nbins;
284 if ( $_->binw > 0 ) {
285 ...
286 }
287 ...
288 { elements => { } };
289 },
290  
291 =item xvalidate => \@array
292  
293 This optional argument provides subroutines to cross-validate
294 parameters. The array elements are themselves arrays with two
295 elements; the first is a mask which represents the combination of
296 parameters to test, the second is a subroutine which operates on C<$_>
297 (an object with methods named for the parameters). It should
298 throw if the validation fails. Validation subroutines are called
299 in the order presented in the array.
300  
301 For example, the following entry ensures that the specified minimum
302 values are less than maximum values:
303  
304 [
305 MIN | MAX,
306 sub {
307 parameter_constraint->throw( "min < max\n" ) unless $_->min < $_->max;
308 },
309 ],
310  
311 =item adjust => \&sub
312  
313 This is an optional parameter providing a subroutine which is passed
314 (via C<$_>) a hash containing the passed build parameters. It can
315 adjust them in place as required.
316  
317 =back
318  
319 =head2 load_class
320  
321 $class_name = load_seq_class( $class_or_submodule );
322  
323 C<$class_or_submodule> is CamelCased.
324  
325 =head1 INTERNALS
326  
327 =for Pod::Coverage BUILDARGS
328  
329 =head1 SUPPORT
330  
331 =head2 Bugs
332  
333 Please report any bugs or feature requests to bug-cxc-number@rt.cpan.org or through the web interface at: L<https://rt.cpan.org/Public/Dist/Display.html?Name=CXC-Number>
334  
335 =head2 Source
336  
337 Source is available at
338  
339 https://gitlab.com/djerius/cxc-number
340  
341 and may be cloned from
342  
343 https://gitlab.com/djerius/cxc-number.git
344  
345 =head1 SEE ALSO
346  
347 Please see those modules/websites for more information related to this module.
348  
349 =over 4
350  
351 =item *
352  
353 L<CXC::Number|CXC::Number>
354  
355 =back
356  
357 =head1 AUTHOR
358  
359 Diab Jerius <djerius@cpan.org>
360  
361 =head1 COPYRIGHT AND LICENSE
362  
363 This software is Copyright (c) 2019 by Smithsonian Astrophysical Observatory.
364  
365 This is free software, licensed under:
366  
367 The GNU General Public License, Version 3, June 2007
368  
369 =cut
« blib/lib/CXC/Number/Grid/Tree.pm ↑ Summary blib/lib/CXC/Number/Grid.pm »