Valiant/Validator/Inclusion.pm

filter: Statementstmt 100% 26 / 26 Branchbran 83.3% 5 / 6 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 100% 5 / 5 Podpod 0.0% 0 / 2 totaltotal 88.1% 37 / 42 CC 5 SCAR 9.4
CC
5
Cov
94%
validate_each
4.0
normalize_shortcut
2.0
BEGIN
1.0
CRAP 2.6
« Valiant/HTML/SafeString.pm ↑ Summary Valiant/Proxy.pm »
1 package Valiant::Validator::Inclusion;
2  
3 3 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Valiant::I18N;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 3 with 'Valiant::Validator::Each';
7  
8 3 has in => (is=>'ro', required=>1);
9 6 ▶has inclusion => (is=>'ro', required=>1, default=>sub {_t 'inclusion'});
Subroutine __ANON__: called CC=1 SCAR=0.0
10  
11 3 ▶sub normalize_shortcut {
Subroutine normalize_shortcut: called CC=2 SCAR=7.1
Pod undocumented
12 3 my ($class, $arg) = @_;
13 3 ▶ $arg = [$arg] unless ref $arg;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arg 0 3 3
14 3 return +{ in=>$arg };
15 }
16  
17 16 ▶sub validate_each {
Subroutine validate_each: called CC=4 SCAR=13.9
Pod undocumented
18 16 my ($self, $record, $attribute, $value, $options) = @_;
19  
20 16 my $in = $self->in;
21 16 my @in = ();
22 16 ▶ if(ref($in) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in eq 'CODE') { } 1 15 16
23 1 @in = $in->($record);
24 } else {
25 15 @in = @$in;
26 }
27  
28 16 ▶ my %opts = (%{$self->options}, list=>\@in, %{$options||+{}});
Condition50%1/2
$options || {}
l!l
160
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
29  
30 16 ▶ unless(grep { $_ eq $value } @in) {
Branch: 1 branch
BranchTrueFalseTotal
unless (grep {$_ eq $value;} @in) 10 6 16
31 10 $record->errors->add($attribute, $self->inclusion, \%opts)
32 }
33 }
34  
35 3 1;
36  
37 =head1 NAME
38  
39 Valiant::Validator::Inclusion - Value must be one of a list
40  
41 =head1 SYNOPSIS
42  
43 package Local::Test::Inclusion;
44  
45 use Moo;
46 use Valiant::Validations;
47  
48 has status => (is=>'ro');
49 has type => (is=>'ro');
50  
51 validates status => (
52 inclusion => +{
53 in => [qw/active retired/],
54 },
55 );
56  
57 validates type => (
58 inclusion => +{
59 in => \&available_types,
60 },
61 );
62  
63 sub available_types {
64 my $self = shift;
65 return (qw(student instructor));
66 }
67  
68 my $object = Local::Test::Inclusion->new(
69 status => 'running',
70 type => 'janitor',
71 );
72  
73 $object->validate;
74  
75 warn $object->errors->_dump;
76  
77 $VAR1 = {
78 'status' => [
79 'Status is not in the list'
80 ],
81 'type' => [
82 'Type is not in the list'
83 ],
84 };
85  
86 =head1 DESCRIPTION
87  
88 Value must be one of a list. This list can be given as an arrayref or
89 as a reference to a method (for when you need to dynamically build the list).
90  
91 =head1 ATTRIBUTES
92  
93 This validator supports the following attributes:
94  
95 =head2 in
96  
97 The list of allowed values, either an arrayref or a coderef that returns the list.
98  
99 =head2 inclusion
100  
101 The error message used when the value is not in the list. Default is translation
102 tag 'inclusion'.
103  
104 =head1 SHORTCUT FORM
105  
106 This validator supports the follow shortcut forms:
107  
108 validates attribute => ( inclusion => [qw/a b c/], ... );
109  
110 Which is the same as:
111  
112 validates attribute => (
113 inclusion => +{
114 in => [qw/a b c/],
115 },
116 );
117  
118 This also works for the coderef form:
119  
120 validates attribute => ( inclusion => \&coderef, ... );
121  
122 validates attribute => (
123 inclusion => +{
124 in => \&coderef,
125 },
126 );
127  
128 =head1 GLOBAL PARAMETERS
129  
130 This validator supports all the standard shared parameters: C<if>, C<unless>,
131 C<message>, C<strict>, C<allow_undef>, C<allow_blank>.
132  
133 =head1 SEE ALSO
134
135 L<Valiant>, L<Valiant::Validator>, L<Valiant::Validator::Each>.
136  
137 =head1 AUTHOR
138
139 See L<Valiant>
140
141 =head1 COPYRIGHT & LICENSE
142
143 See L<Valiant>
144  
145 =cut
« Valiant/HTML/SafeString.pm ↑ Summary Valiant/Proxy.pm »