Registry/Instance.pm

filter: Statementstmt 13.0% 33 / 253 Branchbran 0.0% 0 / 148 Conditioncond 0.0% 0 / 66 MC/DCmcdc 0.0% 0 / 38 Subroutinesub 29.0% 9 / 31 Podpod 100% 8 / 8 totaltotal 9.1% 50 / 544 SCAR 90.8
CC
104
Cov
7%
set
930.0
__ANON__
506.0
as_where_clause
182.0
CRAP 8785.3
↑ 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 sub _registry {
40 0 ▶ $_[0]->registry;
Subroutine _registry: not called CC=1 SCAR=6.9
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 sub get {
106 0 ▶ my ($self, $key) = @_;
Subroutine get: not called CC=5 SCAR=34.0
Pod documented
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 sub set {
170 0 ▶ my $self = shift;
Subroutine set: not called CC=30 SCAR=68.4
Pod documented
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 try {
262 # apply the preprocessor
263 0 ▶ @v = $pp->($t, \@v, @out{@deps});
Subroutine __ANON__: not called CC=1 SCAR=6.9
264  
265 # get rid of consumed parameters
266 0 map { $del{$_} = 1 } $t->consumes;
267 } catch {
268 0 ▶ $err{$p} = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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 try {
275 0 ▶ my $tmp = $t->process(@v);
Subroutine __ANON__: not called CC=3 SCAR=24.8
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 } catch {
278 0 ▶ $err{$p} = $_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
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;
Branch: 1 branch
BranchTrueFalseTotal
$out{$_} and not $del{$_} 0 0 0
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 sub group {
328 0 ▶ my ($self, $key) = @_;
Subroutine group: not called CC=4 SCAR=30.0
Pod documented
329  
330 0 my %out;
331 0 ▶ my @list = @{$self->registry->_groups->{$key} || []};
Branch: 1 branch
BranchTrueFalseTotal
$self->registry->_groups->{$key} or [] 0 0 0
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 sub template {
350 0 ▶ my ($self, $key) = @_;
Subroutine template: not called CC=1 SCAR=6.9
Pod documented
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 sub clone {
363 0 ▶ my $self = shift;
Subroutine clone: not called CC=7 SCAR=40.3
Pod documented
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 0 ▶ $out->set(\%p) if keys %p;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %p 0 0 0
383  
384 0 $out;
385 }
386  
387 =head2 as_where_clause
388  
389 Generates a data structure suitable to pass into L<SQL::Abstract>
390 (e.g., via L<DBIx::Class>).
391  
392 =cut
393  
394 sub _do_span {
395 0 ▶ my ($span, $universe) = @_;
Subroutine _do_span: not called CC=12 SCAR=50.5
396  
397 0 my ($s, $e) = ($span->start, $span->end);
398  
399 # deal with possibly-empty universe
400 0 my ($us, $ue);
401 0 ▶ if ($universe) {
Branch: 1 branch
BranchTrueFalseTotal
if ($universe) 0 0 0
402 0 ▶ my $u = $universe->isa('DateTime::SpanSet')
Branch: 1 branch
BranchTrueFalseTotal
$universe->isa('DateTime::SpanSet') ? : 0 0 0
403 ? $universe->span : $universe;
404 0 ($us, $ue) = ($u->start, $u->end);
405 }
406  
407 # adjust for open sets
408 0 ▶ my $sop = $span->start_is_open ? '>' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$span->start_is_open ? : 0 0 0
409 0 ▶ my $eop = $span->end_is_open ? '<' : '<=';
Branch: 1 branch
BranchTrueFalseTotal
$span->end_is_open ? : 0 0 0
410  
411 # XXX this does not adjust for BETWEEN or when start and end are
412 # the same
413  
414 0 my %out;
415 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
416 0 $out{$sop} = $s;
417 }
418 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
419 0 $out{$eop} = $e;
420 }
421  
422 # this can be empty and that screws up sql generation
423 0 ▶ return \%out if keys %out;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %out 0 0 0
424 }
425  
426 # XXX these should really be embedded in the types, no?
427  
428 # NOTE: we have these functions return the key along with the clause
429 # to signal that there actually *is* a a clause, because just
430 # returning undef could be interpreted by SQL::Abstract as IS NULL,
431 # and we don't want that. Unless we actually *do* want that.
432  
433 my %TYPES = (
434 'Set::Scalar' => sub {
435 # any set coming into this sub will already have been complemented
436 0 ▶ my ($key, $val, $template) = @_;
Subroutine __ANON__: not called CC=6 SCAR=37.4
437  
438 # there is nothing to select
439 0 my $vs = $val->size;
440 0 ▶ return if $vs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $vs == 0 0 0 0
441  
442 # there is everything to select
443 0 my $comp = $template->complement($val);
444 0 my $cs = $comp->size;
445 0 ▶ return if $cs == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $cs == 0 0 0 0
446  
447 0 ▶ if ($vs > $cs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vs > $cs) { } 0 0 0
448 0 my @e = $comp->elements;
449 0 ▶ return ($key => $cs == 1 ? { '!=' => $e[0] } : { -not_in => \@e });
Branch: 1 branch
BranchTrueFalseTotal
$cs == 1 ? : 0 0 0
450 }
451 else {
452 0 my @e = $val->elements;
453 0 ▶ return ($key => $vs == 1 ? $e[0] : { -in => \@e });
Branch: 1 branch
BranchTrueFalseTotal
$vs == 1 ? : 0 0 0
454 }
455 },
456 'Set::Infinite' => sub {
457 0 ▶ my ($key, $val, $template) = @_;
Subroutine __ANON__: not called CC=22 SCAR=62.3
458 0 ▶ return if $val->is_empty;
Branch: 1 branch
BranchTrueFalseTotal
if $val->is_empty 0 0 0
459  
460 # bail out if the span is wider than the universe
461 0 my $universe = $template->universe;
462 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
463 and $val->min <= $universe->min and $val->max >= $universe->max;
464  
465 0 my $inf = Set::Infinite->inf;
466 0 my $ninf = Set::Infinite->minus_inf;
467  
468 0 my @ranges;
469 0 my ($span, $tail) = $val->first;
470 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
471 0 my ($min, $mop) = $span->min_a;
472 0 my ($max, $xop) = $span->max_a;
473  
474 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
475 0 ▶ $mop = $mop ? '>' : '>=';
Branch: 1 branch
BranchTrueFalseTotal
$mop ? : 0 0 0
476 0 ▶ $xop = $xop ? '<' : '<=';
Branch: 1 branch
BranchTrueFalseTotal
$xop ? : 0 0 0
477  
478 0 my %rec;
479 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
480 0 next;
481 }
482 ▶ 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
483 0 ▶ if ($min == $max) {
Branch: 1 branch
BranchTrueFalseTotal
if ($min == $max) { } 0 0 0
484 0 push @ranges, $min;
485 }
486 else {
487 0 $rec{-between} = [$min, $max];
488 }
489 }
490 else {
491 0 ▶ $rec{$mop} = $min + 0 unless $min == $ninf;
Branch: 1 branch
BranchTrueFalseTotal
unless $min == $ninf 0 0 0
492 0 ▶ $rec{$xop} = $max + 0 unless $max == $inf;
Branch: 1 branch
BranchTrueFalseTotal
unless $max == $inf 0 0 0
493 }
494  
495 0 ▶ push @ranges, \%rec if keys %rec;
Branch: 1 branch
BranchTrueFalseTotal
if CORE::keys %rec 0 0 0
496  
497 0 ▶ ($span, $tail) = $tail ? $tail->first : ();
Branch: 1 branch
BranchTrueFalseTotal
$tail ? : 0 0 0
498 } while ($span);
499  
500 0 ▶ return ($key, $ranges[0]) if @ranges == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @ranges == 1 0 0 0
501 0 ▶ return ($key, \@ranges) if @ranges;
Branch: 1 branch
BranchTrueFalseTotal
if @ranges 0 0 0
502 },
503 'DateTime::Span' => sub {
504 0 ▶ my ($key, $val, $template) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
505  
506 0 my $out = _do_span($val, $template->universe);
507 0 ▶ return ($key, $out) if $out;
Branch: 1 branch
BranchTrueFalseTotal
if $out 0 0 0
508 },
509 'DateTime::SpanSet' => sub {
510 0 ▶ my ($key, $val, $template) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
511 0 my $u = $template->universe;
512  
513 0 my @spans;
514 0 for my $span ($val->as_list) {
515 0 my $rule = _do_span($span, $u);
516 0 ▶ push @spans, $rule if $rule;
Branch: 1 branch
BranchTrueFalseTotal
if $rule 0 0 0
517 }
518 0 ▶ return ($key, $spans[0]) if @spans == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @spans == 1 0 0 0
519 0 ▶ return ($key, \@spans) if @spans;
Branch: 1 branch
BranchTrueFalseTotal
if @spans 0 0 0
520 },
521 # i don't think we have any of these at the moment
522 ▶ 'DateTime::Set' => sub {
Subroutine __ANON__: not called CC=1 SCAR=0.0
523 },
524 'ARRAY' => sub {
525 0 ▶ my ($key, $val) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
526 0 return ($key, { -in => [ @$val ] });
527 },
528 1 );
529  
530 sub as_where_clause {
531 0 ▶ my $self = shift;
Subroutine as_where_clause: not called CC=13 SCAR=52.0
Pod documented
532 0 my %p = @_;
533  
534 0 ▶ my %only = map { $_ => 1 } @{$p{only} || []};
Branch: 1 branch
BranchTrueFalseTotal
$p{"only"} or [] 0 0 0
535  
536 0 my %out;
537  
538 0 my $r = $self->registry;
539  
540 0 for my $kin ($self->keys) {
541 # skip skeep skorrp
542 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
543  
544 0 my $vin = $self->get($kin);
545  
546 0 my $dispatch;
547 0 ▶ if (my $ref = ref $vin) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $ref = ref $vin) 0 0 0
548 0 ▶ unless ($dispatch = $TYPES{$ref}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($dispatch = $TYPES{$ref}) 0 0 0
549 0 ▶ if (Scalar::Util::blessed($vin)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($vin)) 0 0 0
550 0 for my $t (keys %TYPES) {
551 0 ▶ if ($vin->isa($t)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($vin->isa($t)) 0 0 0
552 0 $dispatch = $TYPES{$t};
553 0 last;
554 }
555 }
556 }
557 }
558 }
559  
560 0 my ($kout, $vout);
561 0 ▶ if ($dispatch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dispatch) { } 0 0 0
562 0 my $t = $r->template($kin);
563 0 ($kout, $vout) = $dispatch->($kin, $vin, $t);
564 }
565 else {
566 0 ($kout, $vout) = ($kin, $vin);
567 }
568  
569 0 ▶ $out{$kout} = $vout if $kout;
Branch: 1 branch
BranchTrueFalseTotal
if $kout 0 0 0
570 }
571  
572 0 ▶ wantarray ? %out : \%out;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
573 }
574  
575 =head2 as_string
576  
577 Generates the canonical URI query string according to the template.
578  
579 =cut
580  
581 sub as_string {
582 0 ▶ my $self = shift;
Subroutine as_string: not called CC=4 SCAR=30.0
Pod documented
583 0 my $r = $self->registry;
584  
585 # this just creates [key => \@values], ...
586 0 my (@out, %comp);
587 0 for my $k ($r->sequence) {
588 # skip unless the parameter is present. this gets around
589 # 'empty'-marked params that we don't actually have.
590 0 ▶ next unless $self->exists($k);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->exists($k) 0 0 0
591  
592 0 my $t = $r->template($k);
593 0 my $v = $self->get($k);
594  
595 # get dependencies
596 0 my @dep = map { $self->get($_) } $t->depends;
597  
598 # retrieve un-processed ARRAY ref
599 0 (my $obj, $comp{$k}) = $t->unprocess($v, @dep);
600  
601 # skip empties
602 0 ▶ next unless defined $obj;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $obj 0 0 0
603  
604 # in general we want to be conservative about what we escape:
605 # control characters, space
606  
607 # accumulate
608 0 push @out, [$k, $obj];
609 }
610  
611 # XXX we have to handle complements here
612  
613 # for sets/composites, check if displaying '&complement=key' is
614 # shorter than just displaying the contents of the set
615 # (e.g. &key=val&key=val&key=val... it almost certainly will be).
616  
617 # XXX we *also* need to handle escaping
618  
619 return join '&', map {
620 0 my $x = $_->[0];
621 map {
622 0 sprintf '%s=%s', $x, URI::Escape::uri_escape_utf8
623 0 ($_, q{^-._~0-9A-Za-z\Q:/[]@!\$'()*+,;\E}) } @{$_->[1]} } @out;
624 }
625  
626 =head2 make_uri $URI
627  
628 Accepts a L<URI> object and returns a clone of that object with its
629 query string overwritten with the contents of the instance. This is a
630 convenience method for idioms like:
631  
632 my $new_uri = $instance->clone(foo => undef)->make_uri($old_uri);
633  
634 As expected, this will produce a new instance with the C<foo>
635 parameter removed, which is then used to generate a URI, suitable for
636 a link.
637  
638 =cut
639  
640 sub make_uri {
641 0 ▶ my ($self, $uri) = @_;
Subroutine make_uri: not called CC=3 SCAR=24.8
Pod documented
642 0 $uri = $uri->canonical;
643 0 my $q = $self->as_string;
644 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
645 0 $uri->query($q);
646 0 $uri;
647 }
648  
649 =head1 AUTHOR
650  
651 Dorian Taylor, C<< <dorian at cpan.org> >>
652  
653 =head1 SEE ALSO
654  
655 =over 4
656  
657 =item
658  
659 L<Params::Registry>
660  
661 =item
662  
663 L<Params::Registry::Template>
664  
665 =back
666  
667 =head1 LICENSE AND COPYRIGHT
668  
669 Copyright 2013 Dorian Taylor.
670  
671 Licensed under the Apache License, Version 2.0 (the "License"); you
672 may not use this file except in compliance with the License. You may
673 obtain a copy of the License at
674 L<http://www.apache.org/licenses/LICENSE-2.0> .
675  
676 Unless required by applicable law or agreed to in writing, software
677 distributed under the License is distributed on an "AS IS" BASIS,
678 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
679 implied. See the License for the specific language governing
680 permissions and limitations under the License.
681  
682 =cut
683  
684 1 __PACKAGE__->meta->make_immutable;
685  
686 1 1; # End of Params::Registry::Instance
↑ Summary Registry/Template.pm »