File Coverage

blib/lib/Declare/Constraints/Simple/Result.pm
Criterion Covered Total %
statement 19 19 100.0
branch n/a
condition 1 2 50.0
subroutine 11 11 100.0
pod 8 8 100.0
total 39 40 97.5


line stmt bran cond sub pod time code
1             =head1 NAME
2              
3             Declare::Constraints::Simple::Result - Validation Result
4              
5             =cut
6              
7             package Declare::Constraints::Simple::Result;
8 13     13   14826 use warnings;
  13         23  
  13         481  
9 13     13   68 use strict;
  13         29  
  13         633  
10              
11             use overload
12 13         151 bool => \&is_valid,
13 13     13   70 fallback => 1;
  13         21  
14              
15             =head1 SYNOPSIS
16              
17             my $result = $constraint->($value);
18              
19             my $message = $result->message;
20             my $path = $result->path;
21              
22             =head1 DESCRIPTION
23              
24             This represents a result returned by a L
25             constraint. Objects of this kind overload their boolean context, so the
26             value of the L accessor is reflected to it.
27              
28             =cut
29              
30             my %init = (
31             message => '',
32             valid => 0,
33             );
34              
35             =head1 METHODS
36              
37             =head2 new()
38              
39             Constructor. As you will mostly just receive result objects, you should
40             never be required to call this yourself.
41              
42             =cut
43              
44 601     601 1 3742 sub new { bless {%init, stack => []} => shift }
45              
46             =head2 set_valid($bool)
47              
48             Sets the results validity flag.
49              
50             =head2 is_valid()
51              
52             Boolean accessor telling if this is a true result or not.
53              
54             =cut
55              
56 601     601 1 5421 sub set_valid { $_[0]->{valid} = $_[1] }
57 2489     2489 1 23151 sub is_valid { shift->{valid} }
58              
59             =head2 set_message($message)
60              
61             The error message. Useful only on non-valid results.
62              
63             =head2 message()
64              
65             Returns the message of the result object.
66              
67             =cut
68              
69 159     159 1 451 sub set_message { $_[0]->{message} = $_[1] }
70 12     12 1 87 sub message { shift->{message} }
71              
72             =head2 add_to_stack($constraint_name)
73              
74             This adds another level at the beginning (!) of the results constraint
75             stack. This is mostly intended to use for the Cmethod
76             in L package.
77              
78             =head2 path([$separator])
79              
80             Returns a string containing the L contents joined together by
81             the C<$separator> string (defaulting to C<.>).
82              
83             =cut
84              
85 238     238 1 305 sub add_to_stack { unshift @{shift->{stack}}, shift }
  238         801  
86 17   50 17 1 121 sub path { join( ($_[1]||'.'), @{$_[0]->stack} ) }
  17         89  
87              
88             =head2 stack()
89              
90             Returns an array reference containing the results currrent stack. This
91             is a list of the constraints path parts. This is usually just the
92             constraints name. If there's additional info, it is appended to the
93             name like C<[$info]>.
94              
95             =cut
96              
97 17     17 1 116 sub stack { $_[0]->{stack} }
98              
99             =head1 SEE ALSO
100              
101             L
102              
103             =head1 AUTHOR
104              
105             Robert 'phaylon' Sedlacek Cphaylon@dunkelheit.atE>
106              
107             =head1 LICENSE AND COPYRIGHT
108              
109             This module is free software, you can redistribute it and/or modify it
110             under the same terms as perl itself.
111              
112             =cut
113              
114             1;