File Coverage

blib/lib/Dancer2/Plugin/DataTransposeValidator.pm
Criterion Covered Total %
statement 50 51 98.0
branch 20 24 83.3
condition 4 7 57.1
subroutine 11 11 100.0
pod 1 2 50.0
total 86 95 90.5


line stmt bran cond sub pod time code
1             package Dancer2::Plugin::DataTransposeValidator;
2              
3 1     1   847703 use strict;
  1         9  
  1         30  
4 1     1   6 use warnings;
  1         2  
  1         32  
5              
6 1     1   6 use Carp 'croak';
  1         2  
  1         60  
7 1     1   5 use Dancer2::Core::Types qw(Enum HashRef Maybe Str);
  1         2  
  1         10  
8 1     1   2048 use Dancer2::Plugin::DataTransposeValidator::Validator;
  1         5  
  1         44  
9 1     1   1012 use Path::Tiny;
  1         10925  
  1         72  
10 1     1   10 use Module::Runtime qw/use_module/;
  1         3  
  1         66  
11              
12 1     1   701 use Dancer2::Plugin 0.200000;
  1         14015  
  1         14  
13              
14             =head1 NAME
15              
16             Dancer2::Plugin::DataTransposeValidator - Data::Transpose::Validator plugin for Dancer2
17              
18             =head1 VERSION
19              
20             Version 0.200
21              
22             =cut
23              
24             our $VERSION = '0.200';
25              
26             has css_error_class => (
27             is => 'ro',
28             isa => Str,
29             from_config => sub { 'has-error' },
30             );
31              
32             has errors_hash => (
33             is => 'ro',
34             isa => Maybe [ Enum [qw/arrayref joined/] ],
35             from_config => sub { undef },
36             );
37              
38             has rules => (
39             is => 'ro',
40             isa => HashRef,
41             default => sub { +{} },
42             );
43              
44             has rules_class => (
45             is => 'ro',
46             isa => Maybe [Str],
47             from_config => sub { undef },
48             );
49              
50             has rules_dir => (
51             is => 'ro',
52             isa => sub {
53             eval { path( $_[0] )->is_dir; 1 }
54             or do { croak "rules directory does not exist" };
55             },
56             default => sub {
57             my $plugin = shift;
58             my $dir =
59             $plugin->config->{rules_dir}
60             ? $plugin->config->{rules_dir}
61             : 'validation';
62             path( $plugin->app->setting('appdir') )->child($dir)->stringify;
63             },
64             );
65              
66             plugin_keywords 'validator';
67              
68             sub BUILD {
69 7     7 0 606 my $plugin = shift;
70             croak __PACKAGE__ . " cannot use both of rules_class and rules_dir"
71             if exists $plugin->config->{rules_class}
72 7 50 66     114 && exists $plugin->config->{rules_dir};
73              
74 7 100       194 if ( exists $plugin->config->{rules_class} ) {
75 1         24 use_module( $plugin->config->{rules_class} );
76             }
77             }
78              
79             sub validator {
80 16     16 1 600654 my ( $plugin, $params, $name, @additional_args ) = @_;
81 16         47 my $rules;
82              
83 16 50       86 croak "params must be a hash reference" unless ref($params) eq 'HASH';
84              
85 16 100       74 if ( ref($name) eq '' ) {
    100          
    100          
86 13 100       93 if ( !$plugin->rules->{$name} ) {
87 8 100       203 if ( my $class = $plugin->rules_class ) {
88 1 50       209 if ( $class->can($name) ) {
89 1         8 $plugin->rules->{$name} = \&{"${class}::$name"};
  1         25  
90             }
91             else {
92 0         0 croak "Rules class \"$class\" has no rule sub named: $name";
93             }
94             }
95             else {
96             # nasty old rules_dir
97 7         258 my $path = path( $plugin->rules_dir )->child($name);
98 7 100       697 croak "rules_file does not exist" unless $path->is_file;
99              
100 6 50       276 my $eval = do $path->absolute
101             or croak "bad rules file: $path - $! $@";
102              
103 6 100       2436 if ( ref($eval) eq 'CODE' ) {
104 1         8 $plugin->rules->{$name} = $eval;
105             }
106             else {
107 5     7   74 $plugin->rules->{$name} = sub { $eval };
  7         18  
108             }
109             }
110             }
111 12         62 $rules = $plugin->rules->{$name}->(@additional_args);
112             }
113             elsif ( ref($name) eq 'HASH' ) {
114 1         4 $rules = $name;
115             }
116             elsif ( ref($name) eq 'CODE' ) {
117 1         5 $rules = $name->(@additional_args);
118             }
119             else {
120 1         4 my $ref = ref($name);
121 1         315 croak "rules option reference type $ref not allowed";
122             }
123              
124             return Dancer2::Plugin::DataTransposeValidator::Validator->new(
125             params => $params,
126             rules => {
127             options => $rules->{options} || {},
128             prepare => $rules->{prepare} || {},
129             },
130 14   50     521 css_error_class => $plugin->css_error_class,
      50        
131             errors_hash => $plugin->errors_hash,
132             );
133             }
134              
135             1;
136             __END__