File Coverage

blib/lib/Form/Factory/Action/Meta/Attribute/Control.pm
Criterion Covered Total %
statement 4 4 100.0
branch n/a
condition n/a
subroutine 2 2 100.0
pod n/a
total 6 6 100.0


line stmt bran cond sub pod time code
1             package Form::Factory::Action::Meta::Attribute::Control;
2             $Form::Factory::Action::Meta::Attribute::Control::VERSION = '0.022';
3 1     1   4 use Moose::Role;
  1         2  
  1         5  
4              
5             # ABSTRACT: Form control attribute-traits
6              
7              
8             has placement => (
9             is => 'ro',
10             isa => 'Num',
11             required => 1,
12             default => 0,
13             );
14              
15              
16             has control => (
17             is => 'ro',
18             isa => 'Str',
19             required => 1,
20             default => 'text',
21             );
22              
23              
24             has options => (
25             is => 'ro',
26             isa => 'HashRef',
27             required => 1,
28             default => sub { {} },
29             );
30              
31              
32             has features => (
33             is => 'ro',
34             isa => 'HashRef[HashRef]',
35             required => 1,
36             default => sub { {} },
37             );
38              
39              
40             around new => sub {
41             my ($next, $class, $name, %options) = @_;
42              
43             my $meta = delete $options{__meta};
44              
45             my $real_name = $name;
46             $real_name =~ s/^\+//;
47              
48             for my $feature_name (keys %{ $options{features} }) {
49             my $feature_class = Form::Factory->control_feature_class($feature_name);
50             next unless $feature_class->does('Form::Factory::Feature::Role::BuildAttribute');
51              
52             $feature_class->build_attribute(
53             $options{features}{$feature_name}, $meta, $real_name, \%options
54             );
55             }
56              
57             $class->$next($name, %options);
58             };
59              
60              
61             around clone_and_inherit_options => sub {
62             my ($next, $self, %options) = @_;
63              
64             # Merge features
65             if ($options{features}) {
66             my $parent_features = $self->features;
67             my $child_features = $options{features};
68              
69             $options{features} = { %$parent_features, %$child_features };
70             for my $key (keys %{ $options{features} }) {
71             delete $options{features}{$key} unless $options{features}{$key};
72             }
73             }
74              
75             $self->$next(%options);
76             };
77              
78              
79             package Moose::Meta::Attribute::Custom::Trait::Form::Control;
80             $Moose::Meta::Attribute::Custom::Trait::Form::Control::VERSION = '0.022';
81 1     1   392 sub register_implementation { 'Form::Factory::Action::Meta::Attribute::Control' }
82              
83             1;
84              
85             __END__
86              
87             =pod
88              
89             =encoding UTF-8
90              
91             =head1 NAME
92              
93             Form::Factory::Action::Meta::Attribute::Control - Form control attribute-traits
94              
95             =head1 VERSION
96              
97             version 0.022
98              
99             =head1 SYNOPSIS
100              
101             package MyApp::Action::Foo;
102             use Form::Factory::Processor;
103              
104             has_control name => (
105             control => 'text',
106             placement => 1,
107             options => {
108             label => 'The Name',
109             },
110             features => {
111             required => 1,
112             length => {
113             maximum => '20',
114             },
115             },
116             );
117              
118             =head1 DESCRIPTION
119              
120             Any control attribute created with the L<Form::Factory::Processor/has_control> subroutine, will have this trait assigned.
121              
122             =head1 ATTRIBUTES
123              
124             =head2 placement
125              
126             This is the sort order of the controls in an action. Normally, controls will be sorted in the order they appear in the class definition, but this lets you modify that. This is mostly useful when an action is composed of different roles or inherits controls from a parent class. This allows you to order your controls relative to the controls defined in the other classes.
127              
128             =head2 control
129              
130             This is the short name of the control. See L<Form::Factory::Interface/CONTROLS> for a list of built-in controls.
131              
132             =head2 options
133              
134             This is a hash of options to pass to the control constructor.
135              
136             =head2 features
137              
138             This is a hash of feature definitions to attach ot the control. Each key is the short name of a control-feature to attach. The value is either a "1" to indicate no additional arguments or a hash reference of arguments to pass to the feature's constructor.
139              
140             =head1 METHODS
141              
142             =head2 new
143              
144             Makes sure that features are setup properly.
145              
146             =head2 clone_and_inherit_options
147              
148             Modifies the L<Moose::Meta::Attribute> version to handle the merging of L<features>.
149              
150             =head1 SEE ALSO
151              
152             L<Form::Factory::Processor>
153              
154             =head1 AUTHOR
155              
156             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
157              
158             =head1 COPYRIGHT AND LICENSE
159              
160             This software is copyright (c) 2015 by Qubling Software LLC.
161              
162             This is free software; you can redistribute it and/or modify it under
163             the same terms as the Perl 5 programming language system itself.
164              
165             =cut