File Coverage

blib/lib/Syccess/Result.pm
Criterion Covered Total %
statement 32 39 82.0
branch 6 12 50.0
condition 2 6 33.3
subroutine 7 7 100.0
pod n/a
total 47 64 73.4


line stmt bran cond sub pod time code
1             package Syccess::Result;
2             BEGIN {
3 9     9   5503 $Syccess::Result::AUTHORITY = 'cpan:GETTY';
4             }
5             # ABSTRACT: A validation process result
6             $Syccess::Result::VERSION = '0.103';
7 9     9   295 use Moo;
  9         11  
  9         56  
8 9     9   2448 use Module::Runtime qw( use_module );
  9         12  
  9         54  
9              
10             with qw(
11             MooX::Traits
12             );
13              
14             has syccess => (
15             is => 'ro',
16             required => 1,
17             );
18              
19             has params => (
20             is => 'ro',
21             required => 1,
22             );
23              
24             has success => (
25             is => 'lazy',
26             init_arg => undef,
27             );
28              
29             sub _build_success {
30 19     19   19111 my ( $self ) = @_;
31 19 100       237 return $self->error_count ? 0 : 1;
32             }
33              
34             has error_count => (
35             is => 'lazy',
36             init_arg => undef,
37             );
38              
39             sub _build_error_count {
40 19     19   2864 my ( $self ) = @_;
41 19         28 return scalar @{$self->errors};
  19         365  
42             }
43              
44             has errors => (
45             is => 'lazy',
46             init_arg => undef,
47             );
48              
49             around errors => sub {
50             my ( $orig, $self, @args ) = @_;
51             my @errors = @{$self->$orig()};
52             return [ @errors ] unless scalar @args > 0;
53             my @args_errors;
54             for my $error (@errors) {
55             for my $arg (@args) {
56             push @args_errors, $error if $error->syccess_field->name eq $arg;
57             }
58             }
59             return [ @args_errors ];
60             };
61              
62             has error_class => (
63             is => 'lazy',
64             init_arg => undef,
65             );
66              
67             sub _build_error_class {
68 11     11   3232 my ( $self ) = @_;
69 11         157 my $error_class = use_module($self->syccess->error_class);
70 11 100       222 if ($self->syccess->has_error_traits) {
71 2         3 $error_class = $error_class->with_traits(@{$self->syccess->error_traits});
  2         19  
72             }
73 11         476 return $error_class;
74             }
75              
76             sub _build_errors {
77 19     19   135 my ( $self ) = @_;
78 19         23 my %params = %{$self->params};
  19         115  
79 19         33 my @fields = @{$self->syccess->fields};
  19         398  
80 0         0 my %errors_args = $self->syccess->has_errors_args
81 19 50       273 ? (%{$self->syccess->errors_args}) : ();
82 19         35 my @errors;
83 19         38 for my $field (@fields) {
84 43         5820 my @messages = $field->validate( %params );
85 43         93 for my $message (@messages) {
86 22         66 my $ref = ref $message;
87 22 50 66     115 if ($ref eq 'ARRAY' or !ref) {
    0          
88 22         286 push @errors, $self->error_class->new(
89             %errors_args,
90             message => $message,
91             syccess_field => $field,
92             syccess_result => $self,
93             );
94             } elsif ($ref eq 'HASH') {
95 0         0 my %error_args = %{$message};
  0         0  
96 0         0 push @errors, $self->error_class->new(
97             %errors_args,
98             %error_args,
99             syccess_field => $field,
100             syccess_result => $self,
101             );
102             } else {
103 0 0 0     0 if (%errors_args && $message->can('errors_args')) {
104 0         0 $message->errors_args({ %errors_args });
105             }
106 0         0 push @errors, $message;
107             }
108             }
109             }
110 19         4106 return [ @errors ];
111             }
112              
113             1;
114              
115             __END__
116              
117             =pod
118              
119             =head1 NAME
120              
121             Syccess::Result - A validation process result
122              
123             =head1 VERSION
124              
125             version 0.103
126              
127             =head1 DESCRIPTION
128              
129             The result class of I<Syccess> is generated by calling L<Syccess/validate>. The
130             object of this class contains the L<success|/success> of the validation
131             procedure, and if this is false, also the L<Errors|/errors> of it.
132              
133             =head1 METHODS
134              
135             =head2 errors
136              
137             Gives back an ArrayRef of all errors, if no parameter is given. If you give
138             a list of field names as parameters, then you will only get the errors of
139             those specific fields. The errors are given back as L<Syccess::Error>.
140              
141             =head2 error_count
142              
143             Gives back the amount of errors.
144              
145             =head2 success
146              
147             Gives back a Bool value which indicates if the result of the validation was
148             a general success or not, or other said, if there are no errors.
149              
150             =encoding utf8
151              
152             =head1 SUPPORT
153              
154             IRC
155              
156             Join #sycontent on irc.perl.org. Highlight Getty for fast reaction :).
157              
158             Repository
159              
160             http://github.com/SyContent/Syccess
161             Pull request and additional contributors are welcome
162              
163             Issue Tracker
164              
165             http://github.com/SyContent/Syccess/issues
166              
167             =cut
168              
169             =head1 AUTHOR
170              
171             Torsten Raudssus <torsten@raudss.us>
172              
173             =head1 COPYRIGHT AND LICENSE
174              
175             This software is copyright (c) 2014 by Torsten Raudssus.
176              
177             This is free software; you can redistribute it and/or modify it under
178             the same terms as the Perl 5 programming language system itself.
179              
180             =cut