File Coverage

blib/lib/Form/Factory/Feature/Role/Control.pm
Criterion Covered Total %
statement 3 3 100.0
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 4 4 100.0


line stmt bran cond sub pod time code
1             package Form::Factory::Feature::Role::Control;
2             $Form::Factory::Feature::Role::Control::VERSION = '0.022';
3 1     1   628 use Moose::Role;
  1         1  
  1         8  
4              
5             requires qw( check_control );
6              
7             # ABSTRACT: Form features tied to particular controls
8              
9              
10             has control => (
11             is => 'ro',
12             does => 'Form::Factory::Control',
13             required => 1,
14             weak_ref => 1,
15             initializer => sub {
16             my ($self, $value, $set, $attr) = @_;
17             $self->check_control($value);
18             $set->($value);
19             },
20             );
21              
22              
23             1;
24              
25             __END__
26              
27             =pod
28              
29             =encoding UTF-8
30              
31             =head1 NAME
32              
33             Form::Factory::Feature::Role::Control - Form features tied to particular controls
34              
35             =head1 VERSION
36              
37             version 0.022
38              
39             =head1 SYNOPSIS
40              
41             package MyApp::Feature::Control::Color;
42             use Moose;
43              
44             with qw(
45             Form::Factory::Feature
46             Form::Factory::Feature::Role::Check
47             Form::Factory::Feature::Role::Control
48             Form::Factory::Feature::Role::CustomControlMessage
49             );
50              
51             has recognized_colors => (
52             is => 'ro',
53             isa => 'ArrayRef[Str]',
54             required => 1,
55             default => sub { [ qw( red orange yellow green blue purple black white ) ] },
56             );
57              
58             sub check_control {
59             my ($self, $control) = @_;
60              
61             die "color feature is only for scalar valued controls"
62             unless $control->does('Form::Factory::Control::Role::ScalarValue');
63             }
64              
65             sub check {
66             my $self = shift;
67             my $value = $self->control->current_value;
68              
69             unless (grep { $value eq $_ } @{ $self->recognized_colors }) {
70             $self->control_error('the %s does not look like a color');
71             $self->result->is_valid(0);
72             }
73             }
74              
75             package Form::Factory::Feature::Control::Custom::Color;
76             sub register_implementation { 'MyApp::Feature::Control::Color' }
77              
78             And then used in an action via:
79              
80             package MyApp::Action::Foo;
81             use Form::Factory::Processor;
82              
83             has_control favorite_primary_color => (
84             control => 'select_one',
85             options => {
86             available_choices => [
87             map { Form::Factory::Control::Choice->new($_, ucfirst $_) }
88             qw( red yellow blue )
89             ],
90             },
91             features => {
92             color => {
93             recognized_colors => [ qw( red yellow blue ) ],
94             },
95             },
96             );
97              
98             =head1 DESCRIPTION
99              
100             This role is required for any feature attached directly to a control using C<has_control>.
101              
102             =head1 ATTRIBUTES
103              
104             =head2 control
105              
106             This is the control object the feature has been attached to.
107              
108             =head1 ROLE METHODS
109              
110             =head2 check_control
111              
112             All features implementing this role must implement a C<check_control> method. This method is called when the L</control> attribute is initialized during construction. It should be defined like this:
113              
114             sub check_control {
115             my ($self, $control) = @_;
116              
117             # do something...
118             }
119              
120             Here C<$self> is the feature object. Be careful when using this, though, since this object is not fully constructed.
121              
122             The C<$control> argument is the control this feature is being attached to. You are expected to verify that your feature is compatible with the control given.
123              
124             The return value of this method is ignored. If the control is incompatible with your feature, your feature should die with a message explaining the problem.
125              
126             =head1 AUTHOR
127              
128             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
129              
130             =head1 COPYRIGHT AND LICENSE
131              
132             This software is copyright (c) 2015 by Qubling Software LLC.
133              
134             This is free software; you can redistribute it and/or modify it under
135             the same terms as the Perl 5 programming language system itself.
136              
137             =cut