File Coverage

blib/lib/Mail/MtPolicyd/Plugin/Role/ConfigurableFields.pm
Criterion Covered Total %
statement 28 30 93.3
branch 6 8 75.0
condition 3 6 50.0
subroutine 8 8 100.0
pod 0 1 0.0
total 45 53 84.9


line stmt bran cond sub pod time code
1             package Mail::MtPolicyd::Plugin::Role::ConfigurableFields;
2              
3 4     4   3105 use strict; # make critic happy
  4         5  
  4         110  
4 4     4   849 use MooseX::Role::Parameterized;
  4         78457  
  4         21  
5              
6 4     4   60408 use Moose::Util::TypeConstraints;
  4         5  
  4         32  
7              
8             our $VERSION = '2.01'; # VERSION
9             # ABSTRACT: role for plugins using configurable fields
10              
11             parameter fields => (
12             isa => 'HashRef[HashRef]',
13             required => 1,
14             );
15              
16             role {
17             my $p = shift;
18              
19             foreach my $attr ( keys %{$p->fields} ) {
20             my $value_isa = $p->fields->{$attr}->{'value_isa'};
21             delete $p->fields->{$attr}->{'value_isa'};
22             has $attr.'_field' => (
23             is => 'rw',
24             isa => 'Maybe[Str]',
25             %{$p->fields->{$attr}},
26             );
27             method 'get_'.$attr.'_value' => sub {
28 1     1   2 my ( $self, $r ) = @_;
  10     10   338  
        10      
        6      
29 1         4 return $self->get_configurable_field_value( $r, $attr,
  10         32  
30             $value_isa );
31             };
32             }
33             };
34              
35             sub get_configurable_field_value {
36 11     11 0 24 my ( $self, $r, $name, $type ) = @_;
37 11         17 my $conf_field = $name.'_field';
38              
39 11         353 my $request_field = $self->$conf_field;
40 11 50 33     52 if( ! defined $request_field || $request_field eq '' ) {
41 0         0 $self->log( $r, 'no request field configured in '.$conf_field );
42 0         0 return;
43             }
44              
45 11         322 my $value = $r->attr( $request_field );
46 11 100 66     58 if( ! defined $value || $value eq '' ) {
47 1         9 $self->log( $r, 'value of field '.$request_field.
48             ' not defined or empty' );
49 1         6 return;
50             }
51              
52 10 50       24 if( defined $type ) {
53 10         42 my $constraint = find_type_constraint( $type );
54 10         831 my $err = $constraint->validate( $value );
55 10 100       936 if( defined $err ) {
56 1         6 $self->log( $r, 'value of field '.$request_field.
57             ' failed validation for '.$type.': '.$err );
58 1         12 return;
59             }
60             }
61              
62 9         33 return $value;
63             }
64              
65             1;
66              
67             __END__
68              
69             =pod
70              
71             =encoding UTF-8
72              
73             =head1 NAME
74              
75             Mail::MtPolicyd::Plugin::Role::ConfigurableFields - role for plugins using configurable fields
76              
77             =head1 VERSION
78              
79             version 2.01
80              
81             =head1 AUTHOR
82              
83             Markus Benning <ich@markusbenning.de>
84              
85             =head1 COPYRIGHT AND LICENSE
86              
87             This software is Copyright (c) 2014 by Markus Benning <ich@markusbenning.de>.
88              
89             This is free software, licensed under:
90              
91             The GNU General Public License, Version 2, June 1991
92              
93             =cut