File Coverage

blib/lib/Form/Factory/Feature/Control/MatchRegex.pm
Criterion Covered Total %
statement 13 16 81.2
branch 3 6 50.0
condition n/a
subroutine 4 4 100.0
pod 2 2 100.0
total 22 28 78.5


line stmt bran cond sub pod time code
1             package Form::Factory::Feature::Control::MatchRegex;
2             $Form::Factory::Feature::Control::MatchRegex::VERSION = '0.022';
3 1     1   517 use Moose;
  1         2  
  1         6  
4              
5             with qw(
6             Form::Factory::Feature
7             Form::Factory::Feature::Role::Check
8             Form::Factory::Feature::Role::Control
9             Form::Factory::Feature::Role::CustomControlMessage
10             );
11              
12 1     1   5122 use Carp ();
  1         2  
  1         174  
13              
14             # ABSTRACT: Match a control value against a regex
15              
16              
17             has regex => (
18             is => 'ro',
19             isa => 'RegexpRef',
20             required => 1,
21             );
22              
23              
24             sub check_control {
25 5     5 1 41 my ($self, $control) = @_;
26              
27 5 50       19 return if $control->does('Form::Factory::Control::Role::ScalarValue');
28              
29 0         0 Carp::croak("the match_regex feature only works with scalar value controls, not $control");
30             }
31              
32              
33             sub check {
34 1     1 1 1 my $self = shift;
35 1         34 my $value = $self->control->current_value;
36              
37 1         32 my $regex = $self->regex;
38 1 50       9 unless ($value =~ /$regex/) {
39 0         0 $self->control_error("the %s does not match $regex");
40 0         0 $self->result->is_valid(0);
41             }
42              
43 1 50       30 $self->result->is_valid(1) unless $self->result->is_validated;
44             }
45              
46             __PACKAGE__->meta->make_immutable;
47              
48             __END__
49              
50             =pod
51              
52             =encoding UTF-8
53              
54             =head1 NAME
55              
56             Form::Factory::Feature::Control::MatchRegex - Match a control value against a regex
57              
58             =head1 VERSION
59              
60             version 0.022
61              
62             =head1 SYNOPSIS
63              
64             has_control five_char_palindrome => (
65             control => 'text',
66             features => {
67             match_regex => {
68             regex => qr/(.)(.).\2\1/,
69             message => 'the %s is not a palindrome',
70             },
71             },
72             );
73              
74             =head1 DESCRIPTION
75              
76             Checks that the control value matches a regular expression. Returns an error if it does not.
77              
78             =head1 ATTRIBUTES
79              
80             =head2 regex
81              
82             The regular expression to use.
83              
84             =head1 METHODS
85              
86             =head2 check_control
87              
88             Checks that the control does L<Form::Factory::Control::Role::ScalarValue>.
89              
90             =head2 check
91              
92             Runs the regular expression against the current value of the control and reports an error if it does not match.
93              
94             =head1 AUTHOR
95              
96             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
97              
98             =head1 COPYRIGHT AND LICENSE
99              
100             This software is copyright (c) 2015 by Qubling Software LLC.
101              
102             This is free software; you can redistribute it and/or modify it under
103             the same terms as the Perl 5 programming language system itself.
104              
105             =cut