Registry/Instance.pm

filter: Statementstmt 12.8% 33 / 256 Branchbran 0.0% 0 / 144 Conditioncond 0.0% 0 / 73 MC/DCmcdc 0.0% 0 / 42 Subroutinesub 28.1% 9 / 32 Podpod 100% 9 / 9 totaltotal 9.1% 51 / 556 CC 105 SCAR 57.9
CC
105
Cov
7%
set
930.0
__ANON__
506.0
as_where_clause
182.0
CRAP 327.2
↑ Summary Registry/Template.pm »
1 package Params::Registry::Instance;
2  
3 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use Params::Registry::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use URI::Escape ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 #use constant INF => 100**100**100;
17 #use constant NEG_INF => 1 - INF;
18  
19 =head1 NAME
20  
21 Params::Registry::Instance - An instance of registered parameters
22  
23 =head1 VERSION
24  
25 Version 0.04_03
26  
27 =cut
28  
29 1 our $VERSION = '0.04_03';
30  
31 1 has registry => (
32 is => 'ro',
33 isa => 'Params::Registry',
34 required => 1,
35 weak_ref => 1,
36 #init_arg => 'registry',
37 );
38  
39 0 ▶sub _registry {
Subroutine _registry: not called CC=1 SCAR=6.9
40 0 $_[0]->registry;
41 }
42  
43 has _content => (
44 is => 'ro',
45 isa => 'HashRef',
46 traits => [qw(Hash)],
47 lazy => 1,
48 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
49 1 handles => {
50 exists => 'exists',
51 # get => 'get',
52 keys => 'keys',
53 },
54 );
55  
56 has _other => (
57 is => 'ro',
58 isa => 'HashRef',
59 traits => [qw(Hash)],
60 lazy => 1,
61 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
62 1 handles => {
63 },
64 );
65  
66  
67 =head1 SYNOPSIS
68  
69 use Params::Registry;
70 use URI;
71 use URI::QueryParam;
72  
73 my $registry = Params::Registry->new(%enormous_arg_list);
74  
75 my $uri = URI->new($str);
76  
77 # The instance is created through Params::Registry, which will
78 # raise different exceptions for different types of conflict in
79 # the parameters.
80 my $instance = eval { $registry->process($uri->query_form_hash) };
81  
82 # Contents have already been coerced
83 my $thingy = $instance->get($key);
84  
85 # This will perform type validation and coercion, so if you aren't
86 # certain the input is clean, you'll want to wrap this call in an
87 # eval.
88 eval { $instance->set($key, $val) };
89  
90 # Take a subset of parameters peculiar to a certain application.
91 my $group = $instance->group($name);
92  
93 # This string is guaranteed to be consistent for a given set of
94 # parameters and values.
95 $uri->query($instance->as_string);
96  
97 =head1 METHODS
98  
99 =head2 get $KEY
100  
101 Retrieve an element of the parameter instance.
102  
103 =cut
104  
105 0 ▶sub get {
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
106 0 my ($self, $key) = @_;
107 0 my $content = $self->_content;
108 0 ▶ return $content->{$key} if exists $content->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $content->{$key} 0 0 0
109  
110 # otherwise...
111  
112 0 my $t = $self->registry->template($key);
113 0 my %c = map { $_ => 1 } $self->keys;
114 0 my $c = scalar grep { $c{$_} } $t->conflicts;
115  
116 0 ▶ if (!$c and my $d = $t->default) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $c and my $d = $t->default) 0 0 0
Condition0%0/3
not $c and my $d = $t->default
!ll&&!rl&&r
000
MC/DC0%0/2
not $c and my $d = $t->default
not $c my $d = $t->default
Truth table0%0/3
not(A) and B
A= not $cB= my $d = $t->default
ABresult
0X0
100
111
117 # call the default with both the template *and* the instance
118 0 my $val = $d->($t, $self);
119  
120 # this is me being clever
121 0 my $c = $t->composite;
122 0 ▶ if ($c = $c->coercion) {
Branch: 1 branch
BranchTrueFalseTotal
if ($c = $c->coercion) 0 0 0
123 0 return $c->coerce($val);
124 }
125  
126 0 return $val;
127 }
128  
129 0 return;
130 }
131  
132 =head2 set \%PARAMS | $KEY, $VAL [, $KEY2, \@VALS2 ...]
133  
134 Modifies one or more of the parameters in the instance. Attempts to
135 coerce the input according to the template. Accepts, as values, either
136 a literal, an C<ARRAY> reference of literals, or the target datatype.
137 If a <Params::Registry::Template/composite> is specified for a given
138 key, C<ARRAY> references will be coerced into the appropriate
139 composite datatype.
140  
141 Syntax, semantics, cardinality, dependencies and conflicts are all
142 observed, but cascading is I<not>. This method will throw an exception
143 if the input can't be reconciled with the L<Params::Registry> that
144 generated the instance.
145  
146 =cut
147  
148 # it isn't clear why '_process' should not admit already-parsed
149 # values, and why 'set' should not do cascading. they are essentially
150 # identical. in fact, we may be able to just get rid of '_process'
151 # altogether in favour of 'set'.
152  
153 # the difference between 'set' and '_process' is that '_process' runs
154 # defaults while 'set' does not, and 'set' compares depends/conflicts
155 # with existing content while '_process' has nothing to compare it to.
156  
157 # * parameters handed to 'set' may already be parsed, or partially
158 # parsed (as in an arrayref of 'type' but not 'composite')
159  
160 # * dependencies, conflicts, and precursor 'consumes' parameters may
161 # be present in the existing data structure
162  
163 # * dependencies/conflicts can be cleared by passing in 'undef'; to
164 # deal with 'empty' parameters, pass in an empty arrayref or
165 # arrayref containing only undefs.
166  
167 # although if the parameters are ranked and inserted ,
168  
169 0 ▶sub set {
Subroutine set: not called CC=30 SCAR=68.4
Pod documented
170 0 my $self = shift;
171  
172 # deal with parameters and metaparameters
173 0 my (%p, %meta);
174 0 ▶ if (ref $_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0]) { } 0 0 0
175 0 ▶ Params::Registry::Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[0] eq "HASH" 0 0 0
176 ('If the first argument is a ref, it has to be a HASH ref')
177 unless ref $_[0] eq 'HASH';
178 # params are their own hashref
179 0 %p = %{$_[0]};
180  
181 0 ▶ if (ref $_[1]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1]) { } 0 0 0
182 0 ▶ Params::Registry::Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless ref $_[1] eq "HASH" 0 0 0
183 ('If the first and second arguments are refs, ' .
184 'they both have to be HASH refs')
185 unless ref $_[1] eq 'HASH';
186  
187 # metaparams are their own hashref
188 0 %meta = %{$_[1]};
189 }
190 else {
191 0 ▶ Params::Registry::Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 1 0 0 0
192 ('Expected even number of args for metaparameters')
193 unless @_ % 2 == 1; # note: even is actually odd here
194  
195 # metaparams are everything after the hashref
196 0 %meta = @_[1..$#_];
197 }
198 }
199 else {
200 0 ▶ Params::Registry::Error->throw
Branch: 1 branch
BranchTrueFalseTotal
unless @_ % 2 == 0 0 0 0
201 ('Expected even number of args for metaparameters')
202 unless @_ % 2 == 0; # note: even is actually even here
203  
204 # arguments = params
205 0 %p = @_;
206  
207 # pull metaparams out of ordinary params
208 0 %meta = map { $_ => delete $p{$_} } qw(-defaults -force);
209 }
210  
211 # grab the parent object that stores all the configuration data
212 0 my $r = $self->registry;
213  
214 # create a map of params to complement/negate
215 0 my %neg;
216 0 ▶ if (my $c = delete $p{$r->complement}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = delete $p{$r->complement}) 0 0 0
217 0 my $x = ref $c;
218 0 ▶ Params::Registry::Error->throw
Branch: 1 branch
BranchTrueFalseTotal
if $x and $x ne "ARRAY" 0 0 0
Condition0%0/3
$x and $x ne "ARRAY"
!ll&&!rl&&r
000
MC/DC0%0/2
$x and $x ne "ARRAY"
$x $x ne "ARRAY"
Truth table0%0/3
A and B
A= $xB= $x ne "ARRAY"
ABresult
0X0
100
111
219 ('If complement is a reference, it must be an ARRAY reference')
220 if $x and $x ne 'ARRAY';
221 0 ▶ map { $neg{$_} = 1 } @{$x ? $c : [$c]};
Branch: 1 branch
BranchTrueFalseTotal
$x ? : 0 0 0
222 }
223  
224 # and now for the product
225 0 my %out = %{$self->_content};
226 0 my (%del, %err);
227 # the registry has already ranked groups of parameters by order of
228 # depends/consumes
229 0 for my $list (@{$r->_ranked}) {
230 # each rank has a list of parameters which are roughly in the
231 # original sequence provided to the registry
232 0 for my $p (@$list) {
233  
234 # normalize input value(s) if present
235 0 my @v;
236 0 ▶ if (exists $p{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $p{$p}) 0 0 0
237 0 my $v = $p{$p};
238  
239 # XXX i wonder how long it has been the case that you
240 # can't delete a param in `set`
241 0 ▶ unless (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 0 0 0
242 0 $del{$p} = 1;
243 0 next;
244 }
245  
246 0 my $rv = ref $v;
247 0 ▶ $v = [$v] if !$rv || $rv ne 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if not $rv or $rv ne "ARRAY" 0 0 0
Condition0%0/3
not $rv or $rv ne "ARRAY"
l!l&&r!l&&!r
000
MC/DC0%0/2
not $rv or $rv ne "ARRAY"
not $rv $rv ne "ARRAY"
Truth table0%0/3
not(A) or B
A= not $rvB= $rv ne "ARRAY"
ABresult
1X1
011
000
248 0 @v = @$v;
249 }
250  
251 # skip if there's nothing to set
252 0 ▶ next if @v == 0 and !$meta{-force};
Branch: 1 branch
BranchTrueFalseTotal
if @v == 0 and not $meta{"-force"} 0 0 0
Condition0%0/3
@v == 0 and not $meta{"-force"}
!ll&&!rl&&r
000
MC/DC0%0/2
@v == 0 and not $meta{"-force"}
@v == 0 not $meta{"-force"}
Truth table0%0/3
A and not(B)
A= @v == 0B= not $meta{"-force"}
ABresult
0X0
100
111
253  
254 # retrieve the appropriate template object
255 0 my $t = $r->template($p);
256  
257 # run the preprocessor
258 0 my @deps = $t->_consdep;
259 0 ▶ if (my $pp = $t->preproc
Branch: 1 branch
BranchTrueFalseTotal
if (my $pp = $t->preproc and @deps == grep({exists $out{$_};} @deps)) 0 0 0
Condition0%0/3
my $pp = $t->preproc and @deps == grep({exists $out{$_};} @deps)
!ll&&!rl&&r
000
MC/DC0%0/2
my $pp = $t->preproc and @deps == grep({exists $out{$_};} @deps)
my $pp = $t->preproc @deps == grep({exists $out{$_};} @deps)
Truth table0%0/3
A and B
A= my $pp = $t->preprocB= @deps == grep({exists $out{$_};} @deps)
ABresult
0X0
100
111
260 0 and @deps == grep { exists $out{$_} } @deps) {
261 0 ▶ try {
Subroutine __ANON__: not called CC=1 SCAR=6.9
262 # apply the preprocessor
263 0 @v = $pp->($t, \@v, @out{@deps});
264  
265 # get rid of consumed parameters
266 0 map { $del{$_} = 1 } $t->consumes;
267 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
268 0 $err{$p} = $_;
269 0 };
270 }
271  
272 # now we run the main parameter template processor
273 0 ▶ if (!$err{$p} and @v > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $err{$p} and @v > 0) 0 0 0
Condition0%0/3
not $err{$p} and @v > 0
!ll&&!rl&&r
000
MC/DC0%0/2
not $err{$p} and @v > 0
not $err{$p} @v > 0
Truth table0%0/3
not(A) and B
A= not $err{$p}B= @v > 0
ABresult
0X0
100
111
274 0 ▶ try {
Subroutine __ANON__: not called CC=3 SCAR=24.8
275 0 my $tmp = $t->process(@v);
276 0 ▶ $out{$p} = $tmp if defined $tmp or $t->empty;
Branch: 1 branch
BranchTrueFalseTotal
if defined $tmp or $t->empty 0 0 0
Condition0%0/3
defined $tmp or $t->empty
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $tmp or $t->empty
defined $tmp $t->empty
Truth table0%0/3
A or B
A= defined $tmpB= $t->empty
ABresult
1X1
011
000
277 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
278 0 $err{$p} = $_;
279 0 };
280 }
281  
282 # now we test for conflicts
283 0 ▶ unless ($err{$p}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($err{$p}) 0 0 0
284 0 ▶ my @x = grep { $out{$_} && !$del{$_} } $t->conflicts;
Condition0%0/3
$out{$_} && !$del{$_}
!ll&&!rl&&r
000
MC/DC0%0/2
$out{$_} && !$del{$_}
$out{$_} !$del{$_}
Truth table0%0/3
A && not(B)
A= $out{$_}B= !$del{$_}
ABresult
0X0
100
111
285 0 ▶ $err{$p} = Params::Registry::Error->new
Branch: 1 branch
BranchTrueFalseTotal
if @x 0 0 0
286 (sprintf '%s conflicts with %s', $p, join ', ', @x) if @x;
287 }
288  
289  
290 # XXX what was the problem with this? 2016-05-30
291  
292 # elsif ($meta{-defaults} and my $d = $t->default) {
293 # # add a default value unless there are conflicts
294 # my @x = grep { $out{$_} && !$del{$_} } $t->conflicts;
295 # $out{$p} = $d->($t) unless @x;
296 # }
297  
298 # now handle the complement
299 0 ▶ if (!$err{$p} and $neg{$p} and $t->has_complement) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $err{$p} and $neg{$p} and $t->has_complement) 0 0 0
Condition0%0/6
not $err{$p} and $neg{$p} and $t->has_complement
!ll&&!rl&&r
000
not $err{$p} and $neg{$p}
!ll&&!rl&&r
000
MC/DC0%0/3
not $err{$p} and $neg{$p} and $t->has_complement
not $err{$p} $neg{$p} $t->has_complement
Truth table0%0/4
not(A) and B and C
A= not $err{$p}B= $neg{$p}C= $t->has_complement
ABCresult
0XX0
10X0
1100
1111
300 0 $out{$p} = $t->complement($out{$p});
301 }
302 }
303 }
304  
305 0 ▶ Params::Registry::Error::Processing->throw(parameters => \%err)
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %err 0 0 0
306 if keys %err;
307  
308 # we waited to delete the contents all at once in case there were
309 # dependencies
310 0 map { delete $out{$_} } keys %del;
311  
312 # now we replace the content all in one shot
313 0 %{$self->_content} = %out;
314  
315 # not sure what else to return
316 0 return $self;
317 }
318  
319 =head2 group $KEY
320  
321 Selects a subset of the instance according to the groups laid out in
322 the L<Params::Registry> specification, clones them, and returns them
323 in a C<HASH> reference, suitable for passing into another method.
324  
325 =cut
326  
327 0 ▶sub group {
Subroutine group: not called CC=4 SCAR=30.0
Pod documented
328 0 my ($self, $key) = @_;
329  
330 0 my %out;
331 0 ▶ my @list = @{$self->registry->_groups->{$key} || []};
Condition0%0/2
$self->registry->_groups->{$key} || []
l!l
00
MC/DC0%0/1
$self->registry->_groups->{$key} || []
$self->registry->_groups->{$key}
Truth table0%0/2
A
A= $self->registry->_groups->{$key}
Aresult
11
00
332 0 my $c = $self->_content;
333 0 for my $k (@list) {
334 # XXX ACTUALLY CLONE THESE (MAYBE)
335  
336 # use exists, not defined
337 0 ▶ $out{$k} = $c->{$k} if exists $c->{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $c->{$k} 0 0 0
338 }
339  
340 0 \%out;
341 }
342  
343 =head2 template $KEY
344  
345 Retrieves the template for C<$KEY>.
346  
347 =cut
348  
349 0 ▶sub template {
Subroutine template: not called CC=1 SCAR=6.9
Pod documented
350 0 my ($self, $key) = @_;
351 0 $self->registry->template($key);
352 }
353  
354 =head2 clone $KEY => $VAL [...] | \%PAIRS
355  
356 Produces a clone of the instance object, with the supplied parameters
357 overwritten. Internally, this uses L</set>, so the input must already
358 be clean, or wrapped in an C<eval>.
359  
360 =cut
361  
362 0 ▶sub clone {
Subroutine clone: not called CC=7 SCAR=40.3
Pod documented
363 0 my $self = shift;
364 0 ▶ my %p = ref $_[0] eq 'HASH' ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'HASH' ? : 0 0 0
365  
366  
367 # XXX deep copy?
368 0 my %orig = %{$self->_content};
369  
370 # sometimes we only want to clone certain params
371 0 ▶ if (defined $p{-only}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p{"-only"}) 0 0 0
372 0 my $o = delete $p{-only};
373 0 ▶ my %only = map { $_ => 1 } (ref $o ? @$o : $o);
Branch: 1 branch
BranchTrueFalseTotal
ref $o ? : 0 0 0
374 0 ▶ map { delete $orig{$_} unless $only{$_} } keys %orig if %only;
Branch: 2 branches
BranchTrueFalseTotal
if %only 0 0 0
unless $only{$_} 0 0 0
375 }
376  
377 0 my $out = Params::Registry::Instance->new(
378 registry => $self->registry,
379 _content => \%orig,
380 );
381  
382  
383 0 ▶ $out->set(\%p) if keys %p;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %p 0 0 0
384  
385 # warn 'waaaat ' . $out->as_string;
386  
387 0 $out;
388 }
389  
390 =head2 as_hash
391  
392 Dump out a hash(ref) of the parameter instance, depending on the
393 calling context.
394  
395 =cut
396  
397 0 ▶sub as_hash {
Subroutine as_hash: not called CC=2 SCAR=17.9
Pod documented
398 0 my %out = %{$_[0]->_content};
399 0 ▶ wantarray ? %out : \%out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
400 }
401  
402 =head2 as_where_clause
403  
404 Generates a data structure suitable to pass into L<SQL::Abstract>
405 (e.g., via L<DBIx::Class>).
406  
407 =cut
408  
409 0 ▶sub _do_span {
Subroutine _do_span: not called CC=12 SCAR=50.5
410 0 my ($span, $universe) = @_;
411  
412 0 my ($s, $e) = ($span->start, $span->end);
413  
414 # deal with possibly-empty universe
415 0 my ($us, $ue);
416 0 ▶ if ($universe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($universe) 0 0 0
417 0 ▶ my $u = $universe->isa('DateTime::SpanSet')
Branch: 1 branch
BranchTrueFalseTotal
$universe->isa('DateTime::SpanSet') ? : 0 0 0
418 ? $universe->span : $universe;
419 0 ($us, $ue) = ($u->start, $u->end);
420 }
421  
422 # adjust for open sets
423 0 ▶ my $sop = $span->start_is_open ? '>' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$span->start_is_open ? : 0 0 0
424 0 ▶ my $eop = $span->end_is_open ? '<' : '<=';
Branch: 1 branch
BranchTrueFalseTotal
$span->end_is_open ? : 0 0 0
425  
426 # XXX this does not adjust for BETWEEN or when start and end are
427 # the same
428  
429 0 my %out;
430 0 ▶ if ($s->is_finite and (!$us or $s > $us)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($s->is_finite and !$us || $s > $us) 0 0 0
Condition0%0/6
$s->is_finite and !$us || $s > $us
!ll&&!rl&&r
000
not $us or $s > $us
l!l&&r!l&&!r
000
MC/DC0%0/3
$s->is_finite and !$us || $s > $us
$s->is_finite not $us $s > $us
Truth table0%0/4
A and not(B) or C
A= $s->is_finiteB= not $usC= $s > $us
ABCresult
0XX0
1000
11X1
1011
431 0 $out{$sop} = $s;
432 }
433 0 ▶ if ($e->is_finite and (!$ue or $e < $ue)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($e->is_finite and !$ue || $e < $ue) 0 0 0
Condition0%0/6
$e->is_finite and !$ue || $e < $ue
!ll&&!rl&&r
000
not $ue or $e < $ue
l!l&&r!l&&!r
000
MC/DC0%0/3
$e->is_finite and !$ue || $e < $ue
$e->is_finite not $ue $e < $ue
Truth table0%0/4
A and not(B) or C
A= $e->is_finiteB= not $ueC= $e < $ue
ABCresult
0XX0
1000
11X1
1011
434 0 $out{$eop} = $e;
435 }
436  
437 # this can be empty and that screws up sql generation
438 0 ▶ return \%out if keys %out;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %out 0 0 0
439 }
440  
441 # XXX these should really be embedded in the types, no?
442  
443 # NOTE: we have these functions return the key along with the clause
444 # to signal that there actually *is* a a clause, because just
445 # returning undef could be interpreted by SQL::Abstract as IS NULL,
446 # and we don't want that. Unless we actually *do* want that.
447  
448 my %TYPES = (
449 0 ▶ 'Set::Scalar' => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
450 # any set coming into this sub will already have been complemented
451 0 my ($key, $val, $template) = @_;
452  
453 # there is nothing to select
454 0 my $vs = $val->size;
455 0 ▶ return if $vs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $vs == 0 0 0 0
456  
457 # there is everything to select
458 0 my $comp = $template->complement($val);
459 0 my $cs = $comp->size;
460 0 ▶ return if $cs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cs == 0 0 0 0
461  
462 0 ▶ if ($vs > $cs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vs > $cs) { } 0 0 0
463 0 my @e = $comp->elements;
464 0 ▶ return ($key => $cs == 1 ? { '!=' => $e[0] } : { -not_in => \@e });
Branch: 1 branch
BranchTrueFalseTotal
$cs == 1 ? : 0 0 0
465 }
466 else {
467 0 my @e = $val->elements;
468 0 ▶ return ($key => $vs == 1 ? $e[0] : { -in => \@e });
Branch: 1 branch
BranchTrueFalseTotal
$vs == 1 ? : 0 0 0
469 }
470 },
471 0 ▶ 'Set::Infinite' => sub {
Subroutine __ANON__: not called CC=22 SCAR=62.3
472 0 my ($key, $val, $template) = @_;
473 0 ▶ return if $val->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $val->is_empty 0 0 0
474  
475 # bail out if the span is wider than the universe
476 0 my $universe = $template->universe;
477 0 ▶ return if $universe and $val->is_span
Branch: 1 branch
BranchTrueFalseTotal
if $universe and $val->is_span and $val->min <= $universe->min and $val->max >= $universe->max 0 0 0
Condition0%0/9
$universe and $val->is_span and $val->min <= $universe->min and $val->max >= $universe->max
!ll&&!rl&&r
000
$universe and $val->is_span and $val->min <= $universe->min
!ll&&!rl&&r
000
$universe and $val->is_span
!ll&&!rl&&r
000
MC/DC0%0/4
$universe and $val->is_span and $val->min <= $universe->min and $val->max >= $universe->max
$universe $val->is_span $val->min <= $universe->min $val->max >= $universe->max
Truth table0%0/5
A and B and C and D
A= $universeB= $val->is_spanC= $val->min <= $universe->minD= $val->max >= $universe->max
ABCDresult
0XXX0
10XX0
110X0
11100
11111
478 and $val->min <= $universe->min and $val->max >= $universe->max;
479  
480 0 my $inf = Set::Infinite->inf;
481 0 my $ninf = Set::Infinite->minus_inf;
482  
483 0 my @ranges;
484 0 my ($span, $tail) = $val->first;
485 0 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$span and do { do { () = $span->min_a; () = $span->max_a; my $closed = !($mop || $xop); $mop = $mop ? ">" : ">="; $xop = $xop ? "<" : "<="; my %rec; if ($min == $ninf and $max == $inf) { next; } elsif ($closed and $min > $ninf and $max < $inf) { if ($min == $max) { push @ranges, $min; } else { $rec{"-between"} = [$min, $max]; } ; } else { $rec{$mop} = $min + 0 unless $min == $ninf; $rec{$xop} = $max + 0 unless $max == $inf; } ; push @ranges, \%rec if CORE::keys %rec; () = $tail ? $tail->first : () } } 0 0 0
486 0 my ($min, $mop) = $span->min_a;
487 0 my ($max, $xop) = $span->max_a;
488  
489 0 ▶ my $closed = !($mop || $xop);
Condition0%0/3
$mop || $xop
l!l&&r!l&&!r
000
MC/DC0%0/2
$mop || $xop
$mop $xop
Truth table0%0/3
A || B
A= $mopB= $xop
ABresult
1X1
011
000
490 0 ▶ $mop = $mop ? '>' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$mop ? : 0 0 0
491 0 ▶ $xop = $xop ? '<' : '<=';
Branch: 1 branch
BranchTrueFalseTotal
$xop ? : 0 0 0
492  
493 0 my %rec;
494 0 ▶ if ($min == $ninf and $max == $inf) {
Branch: 2 branches
BranchTrueFalseTotal
if ($min == $ninf and $max == $inf) { } 0 0 0
elsif ($closed and $min > $ninf and $max < $inf) { } 0 0 0
Condition0%0/3
$min == $ninf and $max == $inf
!ll&&!rl&&r
000
MC/DC0%0/2
$min == $ninf and $max == $inf
$min == $ninf $max == $inf
Truth table0%0/3
A and B
A= $min == $ninfB= $max == $inf
ABresult
0X0
100
111
495 0 next;
496 }
497 ▶ elsif ($closed and $min > $ninf and $max < $inf) {
Condition0%0/6
$closed and $min > $ninf and $max < $inf
!ll&&!rl&&r
000
$closed and $min > $ninf
!ll&&!rl&&r
000
MC/DC0%0/3
$closed and $min > $ninf and $max < $inf
$closed $min > $ninf $max < $inf
Truth table0%0/4
A and B and C
A= $closedB= $min > $ninfC= $max < $inf
ABCresult
0XX0
10X0
1100
1111
498 0 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 0 0
499 0 push @ranges, $min;
500 }
501 else {
502 0 $rec{-between} = [$min, $max];
503 }
504 }
505 else {
506 0 ▶ $rec{$mop} = $min + 0 unless $min == $ninf;
Branch: 1 branch
BranchTrueFalseTotal
unless $min == $ninf 0 0 0
507 0 ▶ $rec{$xop} = $max + 0 unless $max == $inf;
Branch: 1 branch
BranchTrueFalseTotal
unless $max == $inf 0 0 0
508 }
509  
510 0 ▶ push @ranges, \%rec if keys %rec;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %rec 0 0 0
511  
512 0 ▶ ($span, $tail) = $tail ? $tail->first : ();
Branch: 1 branch
BranchTrueFalseTotal
$tail ? : 0 0 0
513 } while ($span);
514  
515 0 ▶ return ($key, $ranges[0]) if @ranges == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ranges == 1 0 0 0
516 0 ▶ return ($key, \@ranges) if @ranges;
Branch: 1 branch
BranchTrueFalseTotal
if @ranges 0 0 0
517 },
518 0 ▶ 'DateTime::Span' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
519 0 my ($key, $val, $template) = @_;
520  
521 0 my $out = _do_span($val, $template->universe);
522 0 ▶ return ($key, $out) if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 0 0
523 },
524 0 ▶ 'DateTime::SpanSet' => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
525 0 my ($key, $val, $template) = @_;
526 0 my $u = $template->universe;
527  
528 0 my @spans;
529 0 for my $span ($val->as_list) {
530 0 my $rule = _do_span($span, $u);
531 0 ▶ push @spans, $rule if $rule;
Branch: 1 branch
BranchTrueFalseTotal
if $rule 0 0 0
532 }
533 0 ▶ return ($key, $spans[0]) if @spans == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @spans == 1 0 0 0
534 0 ▶ return ($key, \@spans) if @spans;
Branch: 1 branch
BranchTrueFalseTotal
if @spans 0 0 0
535 },
536 # i don't think we have any of these at the moment
537 0 ▶ 'DateTime::Set' => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
538 },
539 0 ▶ 'ARRAY' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
540 0 my ($key, $val) = @_;
541 0 return ($key, { -in => [ @$val ] });
542 },
543 1 );
544  
545 0 ▶sub as_where_clause {
Subroutine as_where_clause: not called CC=13 SCAR=52.0
Pod documented
546 0 my $self = shift;
547 0 my %p = @_;
548  
549 0 ▶ my %only = map { $_ => 1 } @{$p{only} || []};
Condition0%0/2
$p{'only'} || []
l!l
00
MC/DC0%0/1
$p{'only'} || []
$p{'only'}
Truth table0%0/2
A
A= $p{'only'}
Aresult
11
00
550  
551 0 my %out;
552  
553 0 my $r = $self->registry;
554  
555 0 for my $kin ($self->keys) {
556 # skip skeep skorrp
557 0 ▶ next if %only && !$only{$kin};
Branch: 1 branch
BranchTrueFalseTotal
if %only and not $only{$kin} 0 0 0
Condition0%0/3
%only and not $only{$kin}
!ll&&!rl&&r
000
MC/DC0%0/2
%only and not $only{$kin}
%only not $only{$kin}
Truth table0%0/3
A and not(B)
A= %onlyB= not $only{$kin}
ABresult
0X0
100
111
558  
559 0 my $vin = $self->get($kin);
560  
561 0 my $dispatch;
562 0 ▶ if (my $ref = ref $vin) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $vin) 0 0 0
563 0 ▶ unless ($dispatch = $TYPES{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dispatch = $TYPES{$ref}) 0 0 0
564 0 ▶ if (Scalar::Util::blessed($vin)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($vin)) 0 0 0
565 0 for my $t (keys %TYPES) {
566 0 ▶ if ($vin->isa($t)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vin->isa($t)) 0 0 0
567 0 $dispatch = $TYPES{$t};
568 0 last;
569 }
570 }
571 }
572 }
573 }
574  
575 0 my ($kout, $vout);
576 0 ▶ if ($dispatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch) { } 0 0 0
577 0 my $t = $r->template($kin);
578 0 ($kout, $vout) = $dispatch->($kin, $vin, $t);
579 }
580 else {
581 0 ($kout, $vout) = ($kin, $vin);
582 }
583  
584 0 ▶ $out{$kout} = $vout if $kout;
Branch: 1 branch
BranchTrueFalseTotal
if $kout 0 0 0
585 }
586  
587 0 ▶ wantarray ? %out : \%out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
588 }
589  
590 =head2 as_string
591  
592 Generates the canonical URI query string according to the template.
593  
594 =cut
595  
596 0 ▶sub as_string {
Subroutine as_string: not called CC=4 SCAR=30.0
Pod documented
597 0 my $self = shift;
598 0 my $r = $self->registry;
599  
600 # this just creates [key => \@values], ...
601 0 my (@out, %comp);
602 0 for my $k ($r->sequence) {
603 # skip unless the parameter is present. this gets around
604 # 'empty'-marked params that we don't actually have.
605 0 ▶ next unless $self->exists($k);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($k) 0 0 0
606  
607 0 my $t = $r->template($k);
608 0 my $v = $self->get($k);
609  
610 # get dependencies
611 0 my @dep = map { $self->get($_) } $t->depends;
612  
613 # retrieve un-processed ARRAY ref
614 0 (my $obj, $comp{$k}) = $t->unprocess($v, @dep);
615  
616 # skip empties
617 0 ▶ next unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
618  
619 # in general we want to be conservative about what we escape:
620 # control characters, space
621  
622 # accumulate
623 0 push @out, [$k, $obj];
624 }
625  
626 # XXX we have to handle complements here
627  
628 # for sets/composites, check if displaying '&complement=key' is
629 # shorter than just displaying the contents of the set
630 # (e.g. &key=val&key=val&key=val... it almost certainly will be).
631  
632 # XXX we *also* need to handle escaping
633  
634 return join '&', map {
635 0 my $x = $_->[0];
636 map {
637 0 sprintf '%s=%s', $x, URI::Escape::uri_escape_utf8
638 0 ($_, q{^-._~0-9A-Za-z\Q:/[]@!\$'()*+,;\E}) } @{$_->[1]} } @out;
639 }
640  
641 =head2 make_uri $URI
642  
643 Accepts a L<URI> object and returns a clone of that object with its
644 query string overwritten with the contents of the instance. This is a
645 convenience method for idioms like:
646  
647 my $new_uri = $instance->clone(foo => undef)->make_uri($old_uri);
648  
649 As expected, this will produce a new instance with the C<foo>
650 parameter removed, which is then used to generate a URI, suitable for
651 a link.
652  
653 =cut
654  
655 0 ▶sub make_uri {
Subroutine make_uri: not called CC=3 SCAR=24.8
Pod documented
656 0 my ($self, $uri) = @_;
657 # looks like URI.pm has made canonical no longer always clone
658 0 $uri = $uri->clone->canonical;
659 0 my $q = $self->as_string;
660 0 ▶ undef $q if defined $q and $q eq '';
Branch: 1 branch
BranchTrueFalseTotal
if defined $q and $q eq "" 0 0 0
Condition0%0/3
defined $q and $q eq ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $q and $q eq ""
defined $q $q eq ""
Truth table0%0/3
A and B
A= defined $qB= $q eq ""
ABresult
0X0
100
111
661 0 $uri->query($q);
662 0 $uri;
663 }
664  
665 =head1 AUTHOR
666  
667 Dorian Taylor, C<< <dorian at cpan.org> >>
668  
669 =head1 SEE ALSO
670  
671 =over 4
672  
673 =item
674  
675 L<Params::Registry>
676  
677 =item
678  
679 L<Params::Registry::Template>
680  
681 =back
682  
683 =head1 LICENSE AND COPYRIGHT
684  
685 Copyright 2013 Dorian Taylor.
686  
687 Licensed under the Apache License, Version 2.0 (the "License"); you
688 may not use this file except in compliance with the License. You may
689 obtain a copy of the License at
690 L<http://www.apache.org/licenses/LICENSE-2.0> .
691  
692 Unless required by applicable law or agreed to in writing, software
693 distributed under the License is distributed on an "AS IS" BASIS,
694 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
695 implied. See the License for the specific language governing
696 permissions and limitations under the License.
697  
698 =cut
699  
700 1 __PACKAGE__->meta->make_immutable;
701  
702 1 1; # End of Params::Registry::Instance
↑ Summary Registry/Template.pm »