File Coverage

blib/lib/Form/Factory/Action/Meta/Class.pm
Criterion Covered Total %
statement 30 33 90.9
branch 9 14 64.2
condition n/a
subroutine 3 3 100.0
pod 2 2 100.0
total 44 52 84.6


line stmt bran cond sub pod time code
1             package Form::Factory::Action::Meta::Class;
2             $Form::Factory::Action::Meta::Class::VERSION = '0.022';
3 1     1   5 use Moose::Role;
  1         1  
  1         7  
4              
5             # ABSTRACT: The meta-class role for form actions
6              
7              
8             has features => (
9             is => 'ro',
10             isa => 'HashRef',
11             required => 1,
12             default => sub { {} },
13             );
14              
15              
16             sub get_controls {
17 38     38 1 10561 my ($meta, @control_names) = @_;
18 38         61 my @controls;
19              
20 38 50       202 if (@control_names) {
21 0         0 @controls = grep { $_ }
  0         0  
22 0         0 map { $meta->find_attribute_by_name($_) }
23             @control_names;
24             }
25              
26             else {
27 38         129 @controls = $meta->get_all_attributes;
28             }
29              
30 305         14639 @controls = sort { $a->placement <=> $b->placement }
  442         49921  
31 38         1823 grep { $_->does('Form::Factory::Action::Meta::Attribute::Control') }
32             @controls;
33             }
34              
35              
36             sub get_all_features {
37 15     15 1 8894 my $meta = shift;
38 15         24 my %all_features;
39              
40             # For all the classes implemented, find the features we use
41 15         86 for my $class (reverse $meta->linearized_isa) {
42 32         430 my $other_meta = $meta->initialize($class);
43              
44 32 50       494 next unless $other_meta->can('meta');
45 32 100       100 next unless $other_meta->meta->can('does_role');
46 17 50       256 next unless $other_meta->meta->does_role('Form::Factory::Action::Meta::Class');
47              
48             # Make sure inherited features don't clobber each other
49 17         2454 while (my ($name, $feature_config) = each %{ $other_meta->features }) {
  23         796  
50 6         25 my $full_name = join('#', $name, $other_meta->name);
51 6         20 $all_features{$full_name} = $feature_config;
52             }
53             }
54              
55             # Now, do the same for the roles we implement as well
56 15         73 for my $role ($meta->calculate_all_roles) {
57              
58 19 50       1769 next unless $role->can('meta');
59 19 100       70 next unless $role->meta->can('does_role');
60 2 50       25 next unless $role->meta->does_role('Form::Factory::Action::Meta::Role');
61              
62             # Make sure these don't clobber the inherited features
63 2         237 while (my ($name, $feature_config) = each %{ $role->features }) {
  4         115  
64 2         6 my $full_name = join('#', $name, $role->name);
65 2         6 $all_features{$full_name} = $feature_config;
66             }
67             }
68              
69 15         359 return \%all_features;
70             }
71              
72              
73             1;
74              
75             __END__
76              
77             =pod
78              
79             =encoding UTF-8
80              
81             =head1 NAME
82              
83             Form::Factory::Action::Meta::Class - The meta-class role for form actions
84              
85             =head1 VERSION
86              
87             version 0.022
88              
89             =head1 SYNOPSIS
90              
91             package MyApp::Action::Foo;
92             use Form::Factory::Processor;
93              
94             =head1 DESCRIPTION
95              
96             All form actions have this role attached to its meta-class.
97              
98             =head1 ATTRIBUTES
99              
100             =head2 features
101              
102             This is a hash of features. The keys are the short name of the feature to attach and the value is a hash of options to pass to the feature's constructor on instantiation.
103              
104             =head1 METHODS
105              
106             =head2 get_controls
107              
108             my @attributes = $action->meta->get_controls(@names);
109              
110             Returns all the controls for this action. This includes controls inherited from parent classes as well. This returns a list of attributes which do L<Form::Factory::Action::Meta::Attribute::Control>.
111              
112             You may pass a list of control names if you only want a subset of the available controls. If no list is given, all controls are returned.
113              
114             =head2 get_all_features
115              
116             my $features = $action->meta->get_all_features;
117              
118             Returns all the feature specs for the form. This includes all inherited features and features configured in implemented roles as well. These are returned in the same format as the L</features> attribute.
119              
120             =head1 SEE ALSO
121              
122             L<Form::Factory::Action>, L<Form::Factory::Control>, L<Form::Factory::Feature>, L<Form::Factory::Processor>
123              
124             =head1 AUTHOR
125              
126             Andrew Sterling Hanenkamp <hanenkamp@cpan.org>
127              
128             =head1 COPYRIGHT AND LICENSE
129              
130             This software is copyright (c) 2015 by Qubling Software LLC.
131              
132             This is free software; you can redistribute it and/or modify it under
133             the same terms as the Perl 5 programming language system itself.
134              
135             =cut