blib/lib/Venus/Assert.pm

filter: Statementstmt 90.6% 184 / 203 Branchbran 79.7% 67 / 84 Conditioncond 50.9% 26 / 51 MC/DCmcdc 15.6% 5 / 32 Subroutinesub 93.7% 45 / 48 Podpod 72.7% 16 / 22 totaltotal 77.9% 343 / 440 SCAR 44.4
CC
62
Cov
82%
received
76.0
_type_parse_pipes
14.2
expression
5.1
CRAP 84.6
« blib/lib/Venus/Check.pm ↑ Summary blib/lib/Venus/Role/Optional.pm »
1 package Venus::Assert;
2  
3 11 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 11 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 11 base 'Venus::Kind::Utility';
11  
12 11 with 'Venus::Role::Buildable';
13  
14 use overload (
15 0 ▶ '&{}' => sub{$_[0]->validator},
Subroutine __ANON__: not called CC=1 SCAR=6.9
16 11 fallback => 1,
17 11 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # ATTRIBUTES
20  
21 11 attr 'name';
22  
23 # BUILDERS
24  
25 sub build_arg {
26 186 ▶ my ($self, $name) = @_;
Subroutine build_arg: called CC=1 SCAR=0.0
Pod undocumented
27  
28 return {
29 186 name => $name,
30 };
31 }
32  
33 sub build_self {
34 206 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=1 SCAR=0.0
Pod undocumented
35  
36 206 $self->conditions;
37  
38 206 return $self;
39 }
40  
41 # METHODS
42  
43 sub accept {
44 179 ▶ my ($self, $name, @args) = @_;
Subroutine accept: called CC=2 SCAR=7.0
Pod documented
45  
46 179 ▶ return $self if !$name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 179 179
47  
48 179 $self->check->accept($name, @args);
49  
50 179 return $self;
51 }
52  
53 sub check {
54 438 ▶ my ($self, @args) = @_;
Subroutine check: called CC=3 SCAR=11.0
Pod documented
55  
56 438 require Venus::Check;
57  
58 438 ▶ $self->{check} = $args[0] if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 436 438
59  
60 438 ▶ $self->{check} ||= Venus::Check->new;
Condition66%2/3
$self->{"check"} ||= "Venus::Check"->new
l!l&&r!l&&!r
2591790
MC/DC0%0/2
$self->{"check"} ||= "Venus::Check"->new
$self->{"check"} "Venus::Check"->new
Truth table66%2/3
A ||= B
A= $self->{"check"}B= "Venus::Check"->new
ABresult
1X1
011
000
61  
62 438 return $self->{check};
63 }
64  
65 sub clear {
66 1 ▶ my ($self) = @_;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
67  
68 1 $self->check->clear;
69 1 $self->constraint->clear;
70 1 $self->coercion->clear;
71  
72 1 return $self;
73 }
74  
75 sub coerce {
76 56 ▶ my ($self, $data) = @_;
Subroutine coerce: called CC=1 SCAR=0.0
Pod documented
77  
78 56 return $self->coercion->result($self->value($data));
79 }
80  
81 sub coercion {
82 66 ▶ my ($self, @args) = @_;
Subroutine coercion: called CC=3 SCAR=11.0
Pod documented
83  
84 66 require Venus::Coercion;
85  
86 66 ▶ $self->{coercion} = $args[0] if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 64 66
87  
88 66 ▶ $self->{coercion} ||= Venus::Coercion->new->do('check', $self->check);
Condition66%2/3
$self->{"coercion"} ||= "Venus::Coercion"->new->do("check", $self->check)
l!l&&r!l&&!r
10560
MC/DC0%0/2
$self->{"coercion"} ||= "Venus::Coercion"->new->do("check", $self->check)
$self->{"coercion"} "Venus::Coercion"->new->do("check", $self->check)
Truth table66%2/3
A ||= B
A= $self->{"coercion"}B= "Venus::Coercion"->new->do("check", $self->check)
ABresult
1X1
011
000
89  
90 66 return $self->{coercion};
91 }
92  
93 sub conditions {
94 206 ▶ my ($self) = @_;
Subroutine conditions: called CC=1 SCAR=0.0
Pod documented
95  
96 206 return $self;
97 }
98  
99 sub constraint {
100 206 ▶ my ($self, @args) = @_;
Subroutine constraint: called CC=3 SCAR=11.0
Pod documented
101  
102 206 require Venus::Constraint;
103  
104 206 ▶ $self->{constraint} = $args[0] if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 2 204 206
105  
106 206 ▶ $self->{constraint} ||= Venus::Constraint->new->do('check', $self->check);
Condition66%2/3
$self->{"constraint"} ||= "Venus::Constraint"->new->do("check", $self->check)
l!l&&r!l&&!r
371690
MC/DC0%0/2
$self->{"constraint"} ||= "Venus::Constraint"->new->do("check", $self->check)
$self->{"constraint"} "Venus::Constraint"->new->do("check", $self->check)
Truth table66%2/3
A ||= B
A= $self->{"constraint"}B= "Venus::Constraint"->new->do("check", $self->check)
ABresult
1X1
011
000
107  
108 206 return $self->{constraint};
109 }
110  
111 sub ensure {
112 3 ▶ my ($self, @code) = @_;
Subroutine ensure: called CC=1 SCAR=0.0
Pod documented
113  
114 3 $self->constraint->ensure(@code);
115  
116 3 return $self;
117 }
118  
119 sub expression {
120 102 ▶ my ($self, $data) = @_;
Subroutine expression: called CC=5 SCAR=16.3
Pod documented
121  
122 102 ▶ return $self if !$data;
Branch: 1 branch
BranchTrueFalseTotal
unless $data 0 102 102
123  
124 102 $data =
125 $data =~ s/\s*\n+\s*/ /gr =~ s/^\s+|\s+$//gr =~ s/\[\s+/[/gr =~ s/\s+\]/]/gr;
126  
127 102 ▶ $self->name($data) if !$self->name;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->name 18 84 102
128  
129 102 my $parsed = $self->parse($data);
130  
131 $self->accept(
132 102 ▶ @{$parsed} > 0
Branch: 1 branch
BranchTrueFalseTotal
ref $parsed->[0] eq 'ARRAY' ? : 13 89 102
133 13 ? ((ref $parsed->[0] eq 'ARRAY') ? @{$parsed->[0]} : @{$parsed})
134 102 ▶ : @{$parsed}
Branch: 1 branch
BranchTrueFalseTotal
@{$parsed;} > 0 ? : 102 0 102
135 );
136  
137 102 return $self;
138 }
139  
140 sub format {
141 5 ▶ my ($self, @code) = @_;
Subroutine format: called CC=1 SCAR=0.0
Pod documented
142  
143 5 $self->coercion->format(@code);
144  
145 5 return $self;
146 }
147  
148 sub match {
149 228 ▶ my ($self, @args) = @_;
Subroutine match: called CC=1 SCAR=0.0
Pod undocumented
150  
151 228 require Venus::Coercion;
152 228 my $match = Venus::Coercion->new->accept(@args);
153  
154 228 push @{$self->matches}, sub {
155 675 ▶ my ($source, $value) = @_;
Subroutine __ANON__: called CC=1 SCAR=0.0
156 675 local $_ = $value;
157 675 return $match->result($value);
158 228 };
159  
160 228 return $match;
161 }
162  
163 sub matches {
164 535 ▶ my ($self) = @_;
Subroutine matches: called CC=3 SCAR=11.0
Pod undocumented
165  
166 535 ▶ my $matches = $self->{'matches'} ||= [];
Condition100%2/2
$self->{"matches"} ||= []
l!l
360175
MC/DC100%1/1
$self->{"matches"} ||= []
$self->{"matches"}
Truth table100%2/2
A
A= $self->{"matches"}
Aresult
11
00
167  
168 535 ▶ return wantarray ? (@{$matches}) : $matches;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 307 228 535
169 }
170  
171 sub parse {
172 126 ▶ my ($self, $expr) = @_;
Subroutine parse: called CC=2 SCAR=6.9
Pod documented
173  
174 126 ▶ $expr ||= '';
Condition100%2/2
$expr ||= ""
l!l
1251
MC/DC100%1/1
$expr ||= ""
$expr
Truth table100%2/2
A
A= $expr
Aresult
11
00
175  
176 126 $expr =
177 $expr =~ s/\s*\n+\s*/ /gr =~ s/^\s+|\s+$//gr =~ s/\[\s+/[/gr =~ s/\s+\]/]/gr;
178  
179 126 return _type_parse($expr);
180 }
181  
182 sub received {
183 29 ▶ my ($self, $data) = @_;
Subroutine received: called CC=22 SCAR=43.3
Pod undocumented
184  
185 29 require Scalar::Util;
186  
187 29 ▶ if (!defined $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $data) 9 20 29
188 9 return '';
189 }
190  
191 20 my $blessed = Scalar::Util::blessed($data);
192 20 ▶ my $isvenus = $blessed && $data->isa('Venus::Core') && $data->can('does');
Condition33%2/6
$blessed && $data->isa('Venus::Core') && $data->can('does')
!ll&&!rl&&r
2000
$blessed and $data->isa("Venus::Core")
!ll&&!rl&&r
2000
MC/DC0%0/3
$blessed && $data->isa('Venus::Core') && $data->can('does')
$blessed $data->isa("Venus::Core") $data->can('does')
Truth table25%1/4
A and B && C
A= $blessedB= $data->isa("Venus::Core")C= $data->can('does')
ABCresult
0XX0
10X0
1100
1111
193  
194 20 ▶ if (!$blessed && !ref $data) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($blessed or ref $data) 12 8 20
Condition66%2/3
$blessed or ref $data
l!l&&r!l&&!r
0812
MC/DC50%1/2
$blessed or ref $data
$blessed ref $data
Truth table66%2/3
A or B
A= $blessedB= ref $data
ABresult
1X1
011
000
195 12 return $data;
196 }
197 8 ▶ if ($blessed && ref($data) eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed and ref $data eq "Regexp") 0 8 8
Condition33%1/3
$blessed and ref $data eq "Regexp"
!ll&&!rl&&r
800
MC/DC0%0/2
$blessed and ref $data eq "Regexp"
$blessed ref $data eq "Regexp"
Truth table33%1/3
A and B
A= $blessedB= ref $data eq "Regexp"
ABresult
0X0
100
111
198 0 return "$data";
199 }
200 8 ▶ if ($isvenus && $data->does('Venus::Role::Explainable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isvenus and $data->does("Venus::Role::Explainable")) 0 8 8
Condition33%1/3
$isvenus and $data->does("Venus::Role::Explainable")
!ll&&!rl&&r
800
MC/DC0%0/2
$isvenus and $data->does("Venus::Role::Explainable")
$isvenus $data->does("Venus::Role::Explainable")
Truth table33%1/3
A and B
A= $isvenusB= $data->does("Venus::Role::Explainable")
ABresult
0X0
100
111
201 0 ▶ return $self->dump(sub{$data->explain});
Subroutine __ANON__: not called CC=1 SCAR=6.9
202 }
203 8 ▶ if ($isvenus && $data->does('Venus::Role::Valuable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isvenus and $data->does("Venus::Role::Valuable")) 0 8 8
Condition33%1/3
$isvenus and $data->does("Venus::Role::Valuable")
!ll&&!rl&&r
800
MC/DC0%0/2
$isvenus and $data->does("Venus::Role::Valuable")
$isvenus $data->does("Venus::Role::Valuable")
Truth table33%1/3
A and B
A= $isvenusB= $data->does("Venus::Role::Valuable")
ABresult
0X0
100
111
204 0 ▶ return $self->dump(sub{$data->value});
Subroutine __ANON__: not called CC=1 SCAR=6.9
205 }
206 8 ▶ if ($isvenus && $data->does('Venus::Role::Dumpable')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($isvenus and $data->does("Venus::Role::Dumpable")) 0 8 8
Condition33%1/3
$isvenus and $data->does("Venus::Role::Dumpable")
!ll&&!rl&&r
800
MC/DC0%0/2
$isvenus and $data->does("Venus::Role::Dumpable")
$isvenus $data->does("Venus::Role::Dumpable")
Truth table33%1/3
A and B
A= $isvenusB= $data->does("Venus::Role::Dumpable")
ABresult
0X0
100
111
207 0 return $data->dump;
208 }
209 8 ▶ if ($blessed && overload::Method($data, '""')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed and overload::Method($data, "\"\"")) 0 8 8
Condition33%1/3
$blessed and overload::Method($data, "\"\"")
!ll&&!rl&&r
800
MC/DC0%0/2
$blessed and overload::Method($data, "\"\"")
$blessed overload::Method($data, "\"\"")
Truth table33%1/3
A and B
A= $blessedB= overload::Method($data, "\"\"")
ABresult
0X0
100
111
210 0 return "$data";
211 }
212 8 ▶ if ($blessed && $data->can('as_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed and $data->can("as_string")) 0 8 8
Condition33%1/3
$blessed and $data->can("as_string")
!ll&&!rl&&r
800
MC/DC0%0/2
$blessed and $data->can("as_string")
$blessed $data->can("as_string")
Truth table33%1/3
A and B
A= $blessedB= $data->can("as_string")
ABresult
0X0
100
111
213 0 return $data->as_string;
214 }
215 8 ▶ if ($blessed && $data->can('to_string')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed and $data->can("to_string")) 0 8 8
Condition33%1/3
$blessed and $data->can("to_string")
!ll&&!rl&&r
800
MC/DC0%0/2
$blessed and $data->can("to_string")
$blessed $data->can("to_string")
Truth table33%1/3
A and B
A= $blessedB= $data->can("to_string")
ABresult
0X0
100
111
216 0 return $data->to_string;
217 }
218 8 ▶ if ($blessed && $data->isa('Venus::Kind')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($blessed and $data->isa('Venus::Kind')) { } 0 8 8
Condition33%1/3
$blessed and $data->isa("Venus::Kind")
!ll&&!rl&&r
800
MC/DC0%0/2
$blessed and $data->isa("Venus::Kind")
$blessed $data->isa("Venus::Kind")
Truth table33%1/3
A and B
A= $blessedB= $data->isa("Venus::Kind")
ABresult
0X0
100
111
219 0 return $data->stringified;
220 }
221 else {
222 8 ▶ return $self->dump(sub{$data});
Subroutine __ANON__: called CC=1 SCAR=0.0
223 }
224 }
225  
226 sub render {
227 23 ▶ my ($self, $into, $data) = @_;
Subroutine render: called CC=1 SCAR=0.0
Pod documented
228  
229 23 return _type_render($into, $data);
230 }
231  
232 sub result {
233 53 ▶ my ($self, $data) = @_;
Subroutine result: called CC=1 SCAR=0.0
Pod documented
234  
235 53 return $self->coerce($self->validate($self->value($data)));
236 }
237  
238 sub valid {
239 198 ▶ my ($self, $data) = @_;
Subroutine valid: called CC=1 SCAR=0.0
Pod documented
240  
241 198 return $self->constraint->result($self->value($data));
242 }
243  
244 sub validate {
245 133 ▶ my ($self, $data) = @_;
Subroutine validate: called CC=5 SCAR=16.2
Pod documented
246  
247 133 my $valid = $self->valid($data);
248  
249 133 ▶ return $data if $valid;
Branch: 1 branch
BranchTrueFalseTotal
if $valid 104 29 133
250  
251 29 my $error = $self->check->catch('result');
252  
253 29 my $received = $self->received($data);
254  
255 29 ▶ my $message = join("\n\n",
Branch: 2 branches
BranchTrueFalseTotal
defined $data ? : 20 9 29
$received eq '' ? : 0 20 20
Condition50%1/2
$self->name || 'Unknown'
l!l
290
MC/DC0%0/1
$self->name || 'Unknown'
$self->name
Truth table50%1/2
A
A= $self->name
Aresult
11
00
256 'Type:',
257 ($self->name || 'Unknown'),
258 'Failure:',
259 $error->message,
260 'Received:',
261 (defined $data ? ($received eq '' ? '""' : $received) : ('(undefined)')),
262 );
263  
264 29 $error->message($message);
265  
266 29 return $error->throw;
267 }
268  
269 sub validator {
270 2 ▶ my ($self) = @_;
Subroutine validator: called CC=1 SCAR=0.0
Pod documented
271  
272 2 return $self->defer('validate');
273 }
274  
275 sub value {
276 307 ▶ my ($self, $data) = @_;
Subroutine value: called CC=2 SCAR=6.9
Pod undocumented
277  
278 307 my $result = $data;
279  
280 307 for my $match ($self->matches) {
281 675 $result = $match->($self, $result);
282 }
283  
284 307 return $result;
285 }
286  
287 # ROUTINES
288  
289 sub _type_parse {
290 389 ▶ my @items = _type_parse_pipes(@_);
Subroutine _type_parse: called CC=4 SCAR=13.9
291  
292 389 my $either = @items > 1;
293  
294 389 @items = map _type_parse_nested($_), @items;
295  
296 389 ▶ return wantarray && !$either ? (@items) : [$either ? ("either") : (), @items];
Branch: 2 branches
BranchTrueFalseTotal
wantarray && !$either ? : 246 143 389
$either ? : 55 88 143
Condition100%3/3
wantarray and not $either
!ll&&!rl&&r
12617246
MC/DC100%2/2
wantarray and not $either
wantarray not $either
Truth table100%3/3
A and not(B)
A= wantarrayB= not $either
ABresult
0X0
100
111
297 }
298  
299 sub _type_parse_lists {
300 133 ▶ my @items = @_;
Subroutine _type_parse_lists: called CC=1 SCAR=0.0
301  
302 133 my $r0 = '[\"\'\[\]]';
303 133 my $r1 = '[^\"\'\[\]]';
304 133 my $r2 = _type_subexpr_type_2();
305 133 my $r3 = _type_subexpr_delimiter();
306  
307 return (
308 grep length,
309 133 map {split/,\s*(?=(?:$r1*$r0$r1*$r0)*$r1*$)(${r2}(?:${r3}[^,]*)?)?/}
310 @items
311 );
312 }
313  
314 sub _type_parse_nested {
315 467 ▶ my ($expr) = @_;
Subroutine _type_parse_nested: called CC=4 SCAR=13.9
316  
317 467 ▶ return ($expr) if $expr !~ _type_regexp(_type_subexpr_type_2());
Branch: 1 branch
BranchTrueFalseTotal
unless $expr =~ _type_regexp(_type_subexpr_type_2()) 401 66 467
318  
319 66 my @items = ($expr);
320  
321 66 @items = ($expr =~ /^(\w+)\s*\[\s*(.*)\s*\]+$/g);
322  
323 66 @items = map _type_parse_lists($_), @items;
324  
325 66 ▶ @items = map +(
Branch: 1 branch
BranchTrueFalseTotal
$_ =~ qr/^@{[_type_subexpr_type_2()];},.*$/u ? : 1 260 261
326 261 $_ =~ qr/^@{[_type_subexpr_type_2()]},.*$/ ? _type_parse_lists($_) : $_
327 ),
328 @items;
329  
330 66 @items = map {s/^["']+|["']+$//gr} @items;
331  
332 66 @items = map _type_parse($_), @items;
333  
334 66 ▶ return (@items > 1 ? [@items] : @items);
Branch: 1 branch
BranchTrueFalseTotal
@items > 1 ? : 61 5 66
335 }
336  
337 sub _type_parse_pipes {
338 460 ▶ my ($expr) = @_;
Subroutine _type_parse_pipes: called CC=12 SCAR=26.6
339  
340 460 my @items;
341  
342 # i.e. tuple[number, string] | tuple[string, number]
343 460 ▶ if
Branch: 11 branches
BranchTrueFalseTotal
if (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_2()))) { } 3 457 460
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_1(), _type_subexpr_type_2()))) { } 4 453 457
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_1()))) { } 3 450 453
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_2()))) { } 49 401 450
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_2()))) { } 4 397 401
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_3()))) { } 3 394 397
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_4(), _type_subexpr_type_3()))) { } 61 333 394
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_4()))) { } 0 333 333
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_1(), _type_subexpr_type_3()))) { } 0 333 333
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_1()))) { } 0 333 333
elsif (_type_regexp_eval($expr, _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_3()))) { } 0 333 333
344 (
345 _type_regexp_eval(
346 $expr, _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_2())
347 )
348 )
349 {
350 3 @items = map _type_parse_tuples($_),
351 _type_regexp_eval($expr,
352 _type_regexp_groups(_type_subexpr_type_2(), _type_subexpr_type_2()));
353 }
354 # i.e. string | tuple[number, string]
355 elsif
356 (
357 _type_regexp_eval($expr,
358 _type_regexp(_type_subexpr_type_1(), _type_subexpr_type_2()))
359 )
360 {
361 4 @items = map _type_parse_tuples($_),
362 _type_regexp_eval($expr,
363 _type_regexp_groups(_type_subexpr_type_1(), _type_subexpr_type_2()));
364 }
365 # i.e. tuple[number, string] | string
366 elsif
367 (
368 _type_regexp_eval($expr,
369 _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_1()))
370 )
371 {
372 3 @items = map _type_parse_tuples($_),
373 _type_regexp_eval($expr,
374 _type_regexp_groups(_type_subexpr_type_2(), _type_subexpr_type_1()));
375 }
376 # special condition: i.e. tuple[number, string]
377 elsif
378 (
379 _type_regexp_eval($expr, _type_regexp(_type_subexpr_type_2()))
380 )
381 {
382 49 @items = ($expr);
383 }
384 # i.e. "..." | tuple[number, string]
385 elsif
386 (
387 _type_regexp_eval($expr,
388 _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_2()))
389 )
390 {
391 4 @items = _type_regexp_eval($expr,
392 _type_regexp_groups(_type_subexpr_type_3(), _type_subexpr_type_2()));
393 4 @items = (_type_parse_pipes($items[0]), _type_parse_tuples($items[1]));
394 }
395 # i.e. tuple[number, string] | "..."
396 elsif
397 (
398 _type_regexp_eval($expr,
399 _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_3()))
400 )
401 {
402 3 @items = _type_regexp_eval($expr,
403 _type_regexp_groups(_type_subexpr_type_2(), _type_subexpr_type_3()));
404 3 @items = (_type_parse_tuples($items[0]), _type_parse_pipes($items[1]));
405 }
406 # i.e. Package::Name | "..."
407 elsif
408 (
409 _type_regexp_eval($expr,
410 _type_regexp(_type_subexpr_type_4(), _type_subexpr_type_3()))
411 )
412 {
413 61 @items = _type_regexp_eval($expr,
414 _type_regexp_groups(_type_subexpr_type_4(), _type_subexpr_type_3()));
415 61 @items = ($items[0], _type_parse_pipes($items[1]));
416 }
417 # i.e. "..." | Package::Name
418 elsif
419 (
420 _type_regexp_eval($expr,
421 _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_4()))
422 )
423 {
424 0 @items = _type_regexp_eval($expr,
425 _type_regexp_groups(_type_subexpr_type_3(), _type_subexpr_type_4()));
426 0 @items = (_type_parse_pipes($items[0]), $items[1]);
427 }
428 # i.e. string | "..."
429 elsif
430 (
431 _type_regexp_eval($expr,
432 _type_regexp(_type_subexpr_type_1(), _type_subexpr_type_3()))
433 )
434 {
435 0 @items = _type_regexp_eval($expr,
436 _type_regexp_groups(_type_subexpr_type_1(), _type_subexpr_type_3()));
437 0 @items = ($items[0], _type_parse_pipes($items[1]));
438 }
439 # i.e. "..." | string
440 elsif
441 (
442 _type_regexp_eval($expr,
443 _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_1()))
444 )
445 {
446 0 @items = _type_regexp_eval($expr,
447 _type_regexp_groups(_type_subexpr_type_3(), _type_subexpr_type_1()));
448 0 @items = (_type_parse_pipes($items[0]), $items[1]);
449 }
450 # i.e. "..." | "..."
451 elsif
452 (
453 _type_regexp_eval($expr,
454 _type_regexp(_type_subexpr_type_3(), _type_subexpr_type_3()))
455 )
456 {
457 0 @items = map _type_parse_pipes($_),
458 _type_regexp_eval($expr,
459 _type_regexp_groups(_type_subexpr_type_3(), _type_subexpr_type_3()));
460 }
461 else {
462 333 @items = ($expr);
463 }
464  
465 460 return (@items);
466 }
467  
468 sub _type_parse_tuples {
469 27 ▶ map +(scalar(_type_regexp_eval($_,
Subroutine _type_parse_tuples: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
scalar _type_regexp_eval($_, _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_2())) ? : 3 24 27
470 _type_regexp(_type_subexpr_type_2(), _type_subexpr_type_2())))
471 ? (_type_parse_pipes($_))
472 : ($_)), @_
473 }
474  
475 sub _type_regexp {
476 4838 ▶ qr/^@{[_type_regexp_joined(@_)]}$/
Subroutine _type_regexp: called CC=1 SCAR=0.0
477 }
478  
479 sub _type_regexp_eval {
480 4449 ▶ map {s/^\s+|\s+$//gr} ($_[0] =~ $_[1])
Subroutine _type_regexp_eval: called CC=1 SCAR=0.0
481 }
482  
483 sub _type_regexp_groups {
484 78 ▶ qr/^@{[_type_regexp_joined(_type_subexpr_groups(@_))]}$/
Subroutine _type_regexp_groups: called CC=1 SCAR=0.0
485 }
486  
487 sub _type_regexp_joined {
488 4916 ▶ join(_type_subexpr_delimiter(), @_)
Subroutine _type_regexp_joined: called CC=1 SCAR=0.0
489 }
490  
491 sub _type_render {
492 77 ▶ my ($into, $data) = @_;
Subroutine _type_render: called CC=4 SCAR=13.9
493  
494 77 ▶ if (ref $data eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "HASH") 33 44 77
495 $data = join ', ', map +(qq("$_"), _type_render($into, $$data{$_})),
496 33 sort keys %{$data};
497 33 $data = "$into\[$data\]";
498 }
499  
500 77 ▶ if (ref $data eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $data eq "ARRAY") 1 76 77
501 1 ▶ $data = join ', ', map +(/^\w+$/ ? qq("$_") : $_), @{$data};
Branch: 1 branch
BranchTrueFalseTotal
/^\w+$/u ? : 2 0 2
502 1 $data = "$into\[$data\]";
503 }
504  
505 77 return $data;
506 }
507  
508 sub _type_subexpr_delimiter {
509 5049 ▶ '\s*\|\s*'
Subroutine _type_subexpr_delimiter: called CC=1 SCAR=0.0
510 }
511  
512 sub _type_subexpr_groups {
513 78 ▶ map "($_)", @_
Subroutine _type_subexpr_groups: called CC=1 SCAR=0.0
514 }
515  
516 sub _type_subexpr_type_1 {
517 1583 ▶ '\w+'
Subroutine _type_subexpr_type_1: called CC=1 SCAR=0.0
518 }
519  
520 sub _type_subexpr_type_2 {
521 4013 ▶ '\w+\s*\[.*\]+'
Subroutine _type_subexpr_type_2: called CC=1 SCAR=0.0
522 }
523  
524 sub _type_subexpr_type_3 {
525 2925 ▶ '.*'
Subroutine _type_subexpr_type_3: called CC=1 SCAR=0.0
526 }
527  
528 sub _type_subexpr_type_4 {
529 788 ▶ '[A-Za-z][:\^\w]+\w*'
Subroutine _type_subexpr_type_4: called CC=1 SCAR=0.0
530 }
531  
532 11 1;
533  
534  
535  
536 =head1 NAME
537  
538 Venus::Assert - Assert Class
539  
540 =cut
541  
542 =head1 ABSTRACT
543  
544 Assert Class for Perl 5
545  
546 =cut
547  
548 =head1 SYNOPSIS
549  
550 package main;
551  
552 use Venus::Assert;
553  
554 my $assert = Venus::Assert->new('Float');
555  
556 # $assert->accept('float');
557  
558 # $assert->format(sub{sprintf('%.2f', $_)});
559  
560 # $assert->result(123.456);
561  
562 # 123.46
563  
564 =cut
565  
566 =head1 DESCRIPTION
567  
568 This package provides a mechanism for asserting type constraints and coercions
569 on data. Type constraints are handled via L<Venus::Constraint>, and coercions
570 are handled via L<Venus::Coercion>, using L<Venus::Check> to perform data type
571 validations.
572  
573 =cut
574  
575 =head1 ATTRIBUTES
576  
577 This package has the following attributes:
578  
579 =cut
580  
581 =head2 name
582  
583 name(string $data) (string)
584  
585 The name attribute is read-write, accepts C<(string)> values, and is
586 optional.
587  
588 I<Since C<1.40>>
589  
590 =over 4
591  
592 =item name example 1
593  
594 # given: synopsis
595  
596 package main;
597  
598 my $set_name = $assert->name("Example");
599  
600 # "Example"
601  
602 =back
603  
604 =over 4
605  
606 =item name example 2
607  
608 # given: synopsis
609  
610 # given: example-1 name
611  
612 package main;
613  
614 my $get_name = $assert->name;
615  
616 # "Example"
617  
618 =back
619  
620 =cut
621  
622 =head1 INHERITS
623  
624 This package inherits behaviors from:
625  
626 L<Venus::Kind::Utility>
627  
628 =cut
629  
630 =head1 INTEGRATES
631  
632 This package integrates behaviors from:
633  
634 L<Venus::Role::Buildable>
635  
636 =cut
637  
638 =head1 METHODS
639  
640 This package provides the following methods:
641  
642 =cut
643  
644 =head2 accept
645  
646 accept(string $name, any @args) (Venus::Assert)
647  
648 The accept method registers a condition via L</check> based on the arguments
649 provided. The built-in types are defined as methods in L<Venus::Check>.
650  
651 I<Since C<1.40>>
652  
653 =over 4
654  
655 =item accept example 1
656  
657 # given: synopsis
658  
659 package main;
660  
661 $assert = $assert->accept('float');
662  
663 # bless(..., "Venus::Assert")
664  
665 # $assert->valid;
666  
667 # false
668  
669 # $assert->valid(1.01);
670  
671 # true
672  
673 =back
674  
675 =over 4
676  
677 =item accept example 2
678  
679 # given: synopsis
680  
681 package main;
682  
683 $assert = $assert->accept('number');
684  
685 # bless(..., "Venus::Assert")
686  
687 # $assert->valid(1.01);
688  
689 # false
690  
691 # $assert->valid(1_01);
692  
693 # true
694  
695 =back
696  
697 =over 4
698  
699 =item accept example 3
700  
701 # given: synopsis
702  
703 package Example1;
704  
705 sub new {
706 bless {};
707 }
708  
709 package Example2;
710  
711 sub new {
712 bless {};
713 }
714  
715 package main;
716  
717 $assert = $assert->accept('object');
718  
719 # bless(..., "Venus::Assert")
720  
721 # $assert->valid;
722  
723 # false
724  
725 # $assert->valid(qr//);
726  
727 # false
728  
729 # $assert->valid(Example1->new);
730  
731 # true
732  
733 # $assert->valid(Example2->new);
734  
735 # true
736  
737 =back
738  
739 =over 4
740  
741 =item accept example 4
742  
743 # given: synopsis
744  
745 package Example1;
746  
747 sub new {
748 bless {};
749 }
750  
751 package Example2;
752  
753 sub new {
754 bless {};
755 }
756  
757 package main;
758  
759 $assert = $assert->accept('Example1');
760  
761 # bless(..., "Venus::Assert")
762  
763 # $assert->valid;
764  
765 # false
766  
767 # $assert->valid(qr//);
768  
769 # false
770  
771 # $assert->valid(Example1->new);
772  
773 # true
774  
775 # $assert->valid(Example2->new);
776  
777 # false
778  
779 =back
780  
781 =cut
782  
783 =head2 check
784  
785 check(Venus::Check $data) (Venus::Check)
786  
787 The check method gets or sets the L<Venus::Check> object used for performing
788 runtime data type validation.
789  
790 I<Since C<3.55>>
791  
792 =over 4
793  
794 =item check example 1
795  
796 # given: synopsis
797  
798 package main;
799  
800 my $check = $assert->check(Venus::Check->new);
801  
802 # bless(..., 'Venus::Check')
803  
804 =back
805  
806 =over 4
807  
808 =item check example 2
809  
810 # given: synopsis
811  
812 package main;
813  
814 $assert->check(Venus::Check->new);
815  
816 my $check = $assert->check;
817  
818 # bless(..., 'Venus::Check')
819  
820 =back
821  
822 =cut
823  
824 =head2 clear
825  
826 clear() (Venus::Assert)
827  
828 The clear method resets the L</check>, L</constraint>, and L</coercion>
829 attributes and returns the invocant.
830  
831 I<Since C<1.40>>
832  
833 =over 4
834  
835 =item clear example 1
836  
837 # given: synopsis
838  
839 package main;
840  
841 $assert->accept('string');
842  
843 $assert = $assert->clear;
844  
845 # bless(..., "Venus::Assert")
846  
847 =back
848  
849 =cut
850  
851 =head2 coerce
852  
853 coerce(any $data) (any)
854  
855 The coerce method dispatches to the L</coercion> object and returns the result
856 of the L<Venus::Coercion/result> operation.
857  
858 I<Since C<3.55>>
859  
860 =over 4
861  
862 =item coerce example 1
863  
864 # given: synopsis
865  
866 package main;
867  
868 $assert->accept('float');
869  
870 $assert->format(sub{sprintf('%.2f', $_)});
871  
872 my $coerce = $assert->coerce(123.456);
873  
874 # 123.46
875  
876 =back
877  
878 =over 4
879  
880 =item coerce example 2
881  
882 # given: synopsis
883  
884 package main;
885  
886 $assert->accept('string');
887  
888 $assert->format(sub{ucfirst lc $_});
889  
890 my $coerce = $assert->coerce('heLLo');
891  
892 # "Hello"
893  
894 =back
895  
896 =cut
897  
898 =head2 coercion
899  
900 coercion(Venus::Coercion $data) (Venus::Coercion)
901  
902 The coercion method gets or sets the L<Venus::Coercion> object used for
903 performing runtime data type coercions.
904  
905 I<Since C<3.55>>
906  
907 =over 4
908  
909 =item coercion example 1
910  
911 # given: synopsis
912  
913 package main;
914  
915 my $coercion = $assert->coercion(Venus::Coercion->new);
916  
917 # bless(..., 'Venus::Coercion')
918  
919 =back
920  
921 =over 4
922  
923 =item coercion example 2
924  
925 # given: synopsis
926  
927 package main;
928  
929 $assert->coercion(Venus::Coercion->new);
930  
931 my $coercion = $assert->coercion;
932  
933 # bless(..., 'Venus::Coercion')
934  
935 =back
936  
937 =cut
938  
939 =head2 conditions
940  
941 conditions() (Venus::Assert)
942  
943 The conditions method is an object construction hook that allows subclasses to
944 configure the object on construction setting up constraints and coercions and
945 returning the invocant.
946  
947 I<Since C<1.40>>
948  
949 =over 4
950  
951 =item conditions example 1
952  
953 # given: synopsis
954  
955 package main;
956  
957 $assert = $assert->conditions;
958  
959 # bless(..., 'Venus::Assert')
960  
961 =back
962  
963 =over 4
964  
965 =item conditions example 2
966  
967 package Example::Type::PositveNumber;
968  
969 use base 'Venus::Assert';
970  
971 sub conditions {
972 my ($self) = @_;
973  
974 $self->accept('number', sub {
975 $_ >= 0
976 });
977  
978 return $self;
979 }
980  
981 package main;
982  
983 my $assert = Example::Type::PositveNumber->new;
984  
985 # $assert->valid(0);
986  
987 # true
988  
989 # $assert->valid(1);
990  
991 # true
992  
993 # $assert->valid(-1);
994  
995 # false
996  
997 =back
998  
999 =cut
1000  
1001 =head2 constraint
1002  
1003 constraint(Venus::Constraint $data) (Venus::Constraint)
1004  
1005 The constraint method gets or sets the L<Venus::Constraint> object used for
1006 performing runtime data type constraints.
1007  
1008 I<Since C<3.55>>
1009  
1010 =over 4
1011  
1012 =item constraint example 1
1013  
1014 # given: synopsis
1015  
1016 package main;
1017  
1018 my $constraint = $assert->constraint(Venus::Constraint->new);
1019  
1020 # bless(..., 'Venus::Constraint')
1021  
1022 =back
1023  
1024 =over 4
1025  
1026 =item constraint example 2
1027  
1028 # given: synopsis
1029  
1030 package main;
1031  
1032 $assert->constraint(Venus::Constraint->new);
1033  
1034 my $constraint = $assert->constraint;
1035  
1036 # bless(..., 'Venus::Constraint')
1037  
1038 =back
1039  
1040 =cut
1041  
1042 =head2 ensure
1043  
1044 ensure(coderef $code) (Venus::Assert)
1045  
1046 The ensure method registers a custom (not built-in) constraint condition and
1047 returns the invocant.
1048  
1049 I<Since C<3.55>>
1050  
1051 =over 4
1052  
1053 =item ensure example 1
1054  
1055 # given: synopsis
1056  
1057 package main;
1058  
1059 $assert->accept('number');
1060  
1061 my $ensure = $assert->ensure(sub {
1062 $_ >= 0
1063 });
1064  
1065 # bless(.., "Venus::Assert")
1066  
1067 =back
1068  
1069 =cut
1070  
1071 =head2 expression
1072  
1073 expression(string $expr) (Venus::Assert)
1074  
1075 The expression method parses a string representation of an type assertion,
1076 registers the subexpressions using the L</accept> method, and returns the
1077 invocant.
1078  
1079 I<Since C<1.71>>
1080  
1081 =over 4
1082  
1083 =item expression example 1
1084  
1085 # given: synopsis
1086  
1087 package main;
1088  
1089 $assert = $assert->expression('string');
1090  
1091 # bless(..., 'Venus::Assert')
1092  
1093 # $assert->valid('hello');
1094  
1095 # true
1096  
1097 # $assert->valid(['goodbye']);
1098  
1099 # false
1100  
1101 =back
1102  
1103 =over 4
1104  
1105 =item expression example 2
1106  
1107 # given: synopsis
1108  
1109 package main;
1110  
1111 $assert = $assert->expression('string | coderef');
1112  
1113 # bless(..., 'Venus::Assert')
1114  
1115 # $assert->valid('hello');
1116  
1117 # true
1118  
1119 # $assert->valid(sub{'hello'});
1120  
1121 # true
1122  
1123 # $assert->valid(['goodbye']);
1124  
1125 # false
1126  
1127 =back
1128  
1129 =over 4
1130  
1131 =item expression example 3
1132  
1133 # given: synopsis
1134  
1135 package main;
1136  
1137 $assert = $assert->expression('string | coderef | Venus::Assert');
1138  
1139 # bless(..., 'Venus::Assert')
1140  
1141 # $assert->valid('hello');
1142  
1143 # true
1144  
1145 # $assert->valid(sub{'hello'});
1146  
1147 # true
1148  
1149 # $assert->valid($assert);
1150  
1151 # true
1152  
1153 # $assert->valid(['goodbye']);
1154  
1155 # false
1156  
1157 =back
1158  
1159 =over 4
1160  
1161 =item expression example 4
1162  
1163 # given: synopsis
1164  
1165 package main;
1166  
1167 $assert = $assert->expression('Venus::Assert | within[arrayref, Venus::Assert]');
1168  
1169 # bless(..., 'Venus::Assert')
1170  
1171 # $assert->valid('hello');
1172  
1173 # false
1174  
1175 # $assert->valid(sub{'hello'});
1176  
1177 # false
1178  
1179 # $assert->valid($assert);
1180  
1181 # true
1182  
1183 # $assert->valid(['goodbye']);
1184  
1185 # false
1186  
1187 # $assert->valid([$assert]);
1188  
1189 # true
1190  
1191 =back
1192  
1193 =over 4
1194  
1195 =item expression example 5
1196  
1197 # given: synopsis
1198  
1199 package main;
1200  
1201 $assert = $assert->expression('
1202 string
1203 | within[
1204 arrayref, within[
1205 hashref, string
1206 ]
1207 ]
1208 ');
1209  
1210 # bless(..., 'Venus::Assert')
1211  
1212 # $assert->valid('hello');
1213  
1214 # true
1215  
1216 # $assert->valid(sub{'hello'});
1217  
1218 # false
1219  
1220 # $assert->valid($assert);
1221  
1222 # false
1223  
1224 # $assert->valid([]);
1225  
1226 # false
1227  
1228 # $assert->valid([{'test' => ['okay']}]);
1229  
1230 # false
1231  
1232 # $assert->valid([{'test' => 'okay'}]);
1233  
1234 # true
1235  
1236 =back
1237  
1238 =cut
1239  
1240 =head2 format
1241  
1242 format(coderef $code) (Venus::Assert)
1243  
1244 The format method registers a custom (not built-in) coercion condition and
1245 returns the invocant.
1246  
1247 I<Since C<3.55>>
1248  
1249 =over 4
1250  
1251 =item format example 1
1252  
1253 # given: synopsis
1254  
1255 package main;
1256  
1257 $assert->accept('number');
1258  
1259 my $format = $assert->format(sub {
1260 sprintf '%.2f', $_
1261 });
1262  
1263 # bless(.., "Venus::Assert")
1264  
1265 =back
1266  
1267 =cut
1268  
1269 =head2 parse
1270  
1271 parse(string $expr) (any)
1272  
1273 The parse method accepts a string representation of a type assertion and
1274 returns a data structure representing one or more method calls to be used for
1275 validating the assertion signature.
1276  
1277 I<Since C<2.01>>
1278  
1279 =over 4
1280  
1281 =item parse example 1
1282  
1283 # given: synopsis
1284  
1285 package main;
1286  
1287 my $parsed = $assert->parse('');
1288  
1289 # ['']
1290  
1291 =back
1292  
1293 =over 4
1294  
1295 =item parse example 2
1296  
1297 # given: synopsis
1298  
1299 package main;
1300  
1301 my $parsed = $assert->parse('any');
1302  
1303 # ['any']
1304  
1305 =back
1306  
1307 =over 4
1308  
1309 =item parse example 3
1310  
1311 # given: synopsis
1312  
1313 package main;
1314  
1315 my $parsed = $assert->parse('string | number');
1316  
1317 # ['either', 'string', 'number']
1318  
1319 =back
1320  
1321 =over 4
1322  
1323 =item parse example 4
1324  
1325 # given: synopsis
1326  
1327 package main;
1328  
1329 my $parsed = $assert->parse('enum[up,down,left,right]');
1330  
1331 # [['enum', 'up', 'down', 'left', 'right']]
1332  
1333 =back
1334  
1335 =over 4
1336  
1337 =item parse example 5
1338  
1339 # given: synopsis
1340  
1341 package main;
1342  
1343 my $parsed = $assert->parse('number | float | boolean');
1344  
1345 # ['either', 'number', 'float', 'boolean']
1346  
1347 =back
1348  
1349 =over 4
1350  
1351 =item parse example 6
1352  
1353 # given: synopsis
1354  
1355 package main;
1356  
1357 my $parsed = $assert->parse('Example');
1358  
1359 # ['Example']
1360  
1361 =back
1362  
1363 =over 4
1364  
1365 =item parse example 7
1366  
1367 # given: synopsis
1368  
1369 package main;
1370  
1371 my $parsed = $assert->parse('coderef | Venus::Code');
1372  
1373 # ['either', 'coderef', 'Venus::Code']
1374  
1375 =back
1376  
1377 =over 4
1378  
1379 =item parse example 8
1380  
1381 # given: synopsis
1382  
1383 package main;
1384  
1385 my $parsed = $assert->parse('tuple[number, arrayref, coderef]');
1386  
1387 # [['tuple', 'number', 'arrayref', 'coderef']]
1388  
1389 =back
1390  
1391 =over 4
1392  
1393 =item parse example 9
1394  
1395 # given: synopsis
1396  
1397 package main;
1398  
1399 my $parsed = $assert->parse('tuple[number, within[arrayref, hashref], coderef]');
1400  
1401 # [['tuple', 'number', ['within', 'arrayref', 'hashref'], 'coderef']]
1402  
1403 =back
1404  
1405 =over 4
1406  
1407 =item parse example 10
1408  
1409 # given: synopsis
1410  
1411 package main;
1412  
1413 my $parsed = $assert->parse(
1414 'tuple[number, within[arrayref, hashref] | arrayref, coderef]'
1415 );
1416  
1417 # [
1418 # ['tuple', 'number',
1419 # ['either', ['within', 'arrayref', 'hashref'], 'arrayref'], 'coderef']
1420 # ]
1421  
1422  
1423  
1424  
1425 =back
1426  
1427 =over 4
1428  
1429 =item parse example 11
1430  
1431 # given: synopsis
1432  
1433 package main;
1434  
1435 my $parsed = $assert->parse(
1436 'hashkeys["id", number | float, "upvotes", within[arrayref, boolean]]'
1437 );
1438  
1439 # [[
1440 # 'hashkeys',
1441 # 'id',
1442 # ['either', 'number', 'float'],
1443 # 'upvotes',
1444 # ['within', 'arrayref', 'boolean']
1445 # ]]
1446  
1447 =back
1448  
1449 =cut
1450  
1451 =head2 render
1452  
1453 render(string $into, string $expression) (string)
1454  
1455 The render method builds and returns a type expressions suitable for providing
1456 to L</expression> based on the data provided.
1457  
1458 I<Since C<2.55>>
1459  
1460 =over 4
1461  
1462 =item render example 1
1463  
1464 # given: synopsis
1465  
1466 package main;
1467  
1468 $assert = $assert->render;
1469  
1470 # undef
1471  
1472 =back
1473  
1474 =over 4
1475  
1476 =item render example 2
1477  
1478 # given: synopsis
1479  
1480 package main;
1481  
1482 $assert = $assert->render(undef, 'string');
1483  
1484 # "string"
1485  
1486 =back
1487  
1488 =over 4
1489  
1490 =item render example 3
1491  
1492 # given: synopsis
1493  
1494 package main;
1495  
1496 $assert = $assert->render('routines', ['say', 'say_pretty']);
1497  
1498 # 'routines["say", "say_pretty"]'
1499  
1500 =back
1501  
1502 =over 4
1503  
1504 =item render example 4
1505  
1506 # given: synopsis
1507  
1508 package main;
1509  
1510 $assert = $assert->render('hashkeys', {id => 'number', name => 'string'});
1511  
1512 # 'hashkeys["id", number, "name", string]'
1513  
1514 =back
1515  
1516 =over 4
1517  
1518 =item render example 5
1519  
1520 # given: synopsis
1521  
1522 package main;
1523  
1524 $assert = $assert->render('hashkeys', {
1525 id => 'number',
1526 profile => {
1527 level => 'string',
1528 },
1529 });
1530  
1531 # 'hashkeys["id", number, "profile", hashkeys["level", string]]'
1532  
1533 =back
1534  
1535 =cut
1536  
1537 =head2 result
1538  
1539 result(any $data) (any)
1540  
1541 The result method validates the value provided against the registered
1542 constraints and if valid returns the result of the value having any registered
1543 coercions applied. If the value is invalid an exception from L<Venus::Check>
1544 will be thrown.
1545  
1546 I<Since C<3.55>>
1547  
1548 =over 4
1549  
1550 =item result example 1
1551  
1552 # given: synopsis
1553  
1554 package main;
1555  
1556 $assert->accept('number')->format(sub{sprintf '%.2f', $_});
1557  
1558 my $result = $assert->result(1);
1559  
1560 # "1.00"
1561  
1562 =back
1563  
1564 =over 4
1565  
1566 =item result example 2
1567  
1568 # given: synopsis
1569  
1570 package main;
1571  
1572 $assert->accept('number')->format(sub{sprintf '%.2f', $_});
1573  
1574 my $result = $assert->result('hello');
1575  
1576 # Exception! (isa Venus::Check::Error) (see error_on_coded)
1577  
1578 =back
1579  
1580 =cut
1581  
1582 =head2 valid
1583  
1584 valid(any $data) (any)
1585  
1586 The valid method dispatches to the L</constraint> object and returns the result
1587 of the L<Venus::Constraint/result> operation.
1588  
1589 I<Since C<3.55>>
1590  
1591 =over 4
1592  
1593 =item valid example 1
1594  
1595 # given: synopsis
1596  
1597 package main;
1598  
1599 $assert->accept('float');
1600  
1601 $assert->ensure(sub{$_ >= 1});
1602  
1603 my $valid = $assert->valid('1.00');
1604  
1605 # true
1606  
1607 =back
1608  
1609 =over 4
1610  
1611 =item valid example 2
1612  
1613 # given: synopsis
1614  
1615 package main;
1616  
1617 $assert->accept('float');
1618  
1619 $assert->ensure(sub{$_ >= 1});
1620  
1621 my $valid = $assert->valid('0.99');
1622  
1623 # false
1624  
1625 =back
1626  
1627 =cut
1628  
1629 =head2 validate
1630  
1631 validate(any $data) (any)
1632  
1633 The validate method validates the value provided against the registered
1634 constraints and if valid returns the value. If the value is invalid an
1635 exception from L<Venus::Check> will be thrown.
1636  
1637 I<Since C<3.55>>
1638  
1639 =over 4
1640  
1641 =item validate example 1
1642  
1643 # given: synopsis
1644  
1645 package main;
1646  
1647 $assert->accept('number');
1648  
1649 my $validate = $assert->validate(1);
1650  
1651 # 1
1652  
1653 =back
1654  
1655 =over 4
1656  
1657 =item validate example 2
1658  
1659 # given: synopsis
1660  
1661 package main;
1662  
1663 $assert->accept('number');
1664  
1665 my $validate = $assert->validate('hello');
1666  
1667 # Exception! (isa Venus::Check::Error) (see error_on_coded)
1668  
1669 =back
1670  
1671 =cut
1672  
1673 =head2 validator
1674  
1675 validator(any @args) (coderef)
1676  
1677 The validator method returns a coderef which calls the L</validate> method with
1678 the invocant when called.
1679  
1680 I<Since C<3.55>>
1681  
1682 =over 4
1683  
1684 =item validator example 1
1685  
1686 # given: synopsis
1687  
1688 package main;
1689  
1690 $assert->accept('string');
1691  
1692 my $validator = $assert->validator;
1693  
1694 # sub{...}
1695  
1696 # my $result = $validator->();
1697  
1698 # Exception! (isa Venus::Check::Error) (see error_on_coded)
1699  
1700 =back
1701  
1702 =over 4
1703  
1704 =item validator example 2
1705  
1706 # given: synopsis
1707  
1708 package main;
1709  
1710 $assert->accept('string');
1711  
1712 my $validator = $assert->validator;
1713  
1714 # sub{...}
1715  
1716 # my $result = $validator->('hello');
1717  
1718 # "hello"
1719  
1720 =back
1721  
1722 =cut
1723  
1724 =head1 AUTHORS
1725  
1726 Awncorp, C<awncorp@cpan.org>
1727  
1728 =cut
1729  
1730 =head1 LICENSE
1731  
1732 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
1733  
1734 This program is free software, you can redistribute it and/or modify it under
1735 the terms of the Apache license version 2.0.
1736  
1737 =cut
« blib/lib/Venus/Check.pm ↑ Summary blib/lib/Venus/Role/Optional.pm »