| 1 | package Brick::Result::Item; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | $VERSION = '0.904'; | ||
| 9 | |||
| 10 | =encoding utf8 | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Brick::Result::Item - The result from a single profile element | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | use Brick; | ||
| 19 | |||
| 20 | my $result = $brick->apply( $Profile, $Input ); | ||
| 21 | |||
| 22 | $result->explain; | ||
| 23 | |||
| 24 | =head1 DESCRIPTION | ||
| 25 | |||
| 26 | This class provides methods to turn the data structure returned | ||
| 27 | by apply() into a useable form for particular situations. | ||
| 28 | |||
| 29 | =over | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | use constant LABEL => 0; | ||
| 34 | use constant METHOD => 1; | ||
| 35 | use constant RESULT => 2; | ||
| 36 | use constant MESSAGES => 3; | ||
| 37 | |||
| 38 | =item new( HASH_REF ) | ||
| 39 | |||
| 40 | Keys: | ||
| 41 | |||
| 42 | label - the label for the item | ||
| 43 | |||
| 44 | method - the responsible subroutine | ||
| 45 | |||
| 46 | result - 1 | 0 | undef (See set_result) | ||
| 47 | |||
| 48 | messages - the error reference that comes back from the brick | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | use Data::Dumper; | ||
| 53 | |||
| 54 | sub new { | ||
| 55 | my( $class, @args ) = @_; | ||
| 56 | |||
| 57 | my $hash = { @args }; | ||
| 58 | |||
| 59 | my $self = bless [], $class; | ||
| 60 | |||
| 61 | $self->set_label( $hash->{label} ); | ||
| 62 | $self->set_method( $hash->{method} ); | ||
| 63 | $self->set_result( $hash->{result} ); | ||
| 64 | $self->set_messages( $hash->{messages} ); | ||
| 65 | |||
| 66 | $self; | ||
| 67 | } | ||
| 68 | |||
| 69 | =item get_label | ||
| 70 | |||
| 71 | =item set_label( STRING ) | ||
| 72 | |||
| 73 | Get or set the label for the item. This is the label that the profile | ||
| 74 | object used to mark the item. This joins the items in the results to the | ||
| 75 | items in the profile. | ||
| 76 | |||
| 77 | =cut | ||
| 78 | |||
| 79 | sub get_label { $_[0]->[ LABEL ] } | ||
| 80 | |||
| 81 | sub set_label { $_[0]->[ LABEL ] = $_[1] } | ||
| 82 | |||
| 83 | =item get_method | ||
| 84 | |||
| 85 | =item set_method( STRING ) | ||
| 86 | |||
| 87 | Get or set the method name responsible for the validation. When you need | ||
| 88 | to track down the subroutine causing the problems, this should be it's | ||
| 89 | name. | ||
| 90 | |||
| 91 | =cut | ||
| 92 | |||
| 93 | sub get_method { $_[0]->[ METHOD ] } | ||
| 94 | |||
| 95 | sub set_method { $_[0]->[ METHOD ] = $_[1] } | ||
| 96 | |||
| 97 | =item get_result | ||
| 98 | |||
| 99 | =item set_result( 1 | 0 | undef ) | ||
| 100 | |||
| 101 | Get or set the result of the element. The result is one of three values | ||
| 102 | depending on what happened: | ||
| 103 | |||
| 104 | 1 - passed | ||
| 105 | 0 - failed by validation | ||
| 106 | undef - failed by program error | ||
| 107 | |||
| 108 | =cut | ||
| 109 | |||
| 110 | sub get_result { $_[0]->[ RESULT ] } | ||
| 111 | |||
| 112 | sub set_result { $_[0]->[ RESULT ] = $_[1] } | ||
| 113 | |||
| 114 | =item get_messages | ||
| 115 | |||
| 116 | =item set_messages( HASH_REF ) | ||
| 117 | |||
| 118 | Get or set the message hash for the errors. | ||
| 119 | |||
| 120 | =cut | ||
| 121 | |||
| 122 | sub get_messages { $_[0]->[ MESSAGES ] } | ||
| 123 | |||
| 124 | sub set_messages { $_[0]->[ MESSAGES ] = $_[1] } | ||
| 125 | |||
| 126 | =item passed | ||
| 127 | |||
| 128 | Returns true if the item passed validation. | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | sub passed { !! $_[0]->[ RESULT ] } | ||
| 133 | |||
| 134 | =item failed | ||
| 135 | |||
| 136 | Returns true if the item failed validation. This ight mean that the | ||
| 137 | validation fails or that there was a programming error. See | ||
| 138 | C<is_validation_error> and C<is_code_error>. | ||
| 139 | |||
| 140 | =cut | ||
| 141 | |||
| 142 | sub failed { ! $_[0]->[ RESULT ] } | ||
| 143 | |||
| 144 | =item is_validation_error | ||
| 145 | |||
| 146 | Returns true if the failure was the result of a validation error (so not | ||
| 147 | a programming error). | ||
| 148 | |||
| 149 | =cut | ||
| 150 | |||
| 151 | sub is_validation_error { ! $_[0]->[ RESULT ] and defined $_[0]->[ RESULT ] } | ||
| 152 | |||
| 153 | =item is_code_error | ||
| 154 | |||
| 155 | Returns true if the failure was the result of a programming error (so | ||
| 156 | not a validation error). In hash from C<get_messages> will have a key | ||
| 157 | C<program_error> with the value of C<1>, and the C<message> key will | ||
| 158 | have the program error message. | ||
| 159 | |||
| 160 | =cut | ||
| 161 | |||
| 162 | sub is_code_error { ! defined $_[0]->[ RESULT ] } | ||
| 163 | |||
| 164 | =back | ||
| 165 | |||
| 166 | =head1 TO DO | ||
| 167 | |||
| 168 | TBA | ||
| 169 | |||
| 170 | =head1 SEE ALSO | ||
| 171 | |||
| 172 | L<Brick::Profile> | ||
| 173 | |||
| 174 | =head1 SOURCE AVAILABILITY | ||
| 175 | |||
| 176 | This source is in Github: | ||
| 177 | |||
| 178 | https://github.com/briandfoy/brick | ||
| 179 | |||
| 180 | =head1 AUTHOR | ||
| 181 | |||
| 182 | brian d foy, C<< <briandfoy@pobox.com> >> | ||
| 183 | |||
| 184 | =head1 COPYRIGHT | ||
| 185 | |||
| 186 | Copyright © 2007-2025, brian d foy <briandfoy@pobox.com>. All rights reserved. | ||
| 187 | |||
| 188 | You may redistribute this under the terms of the Artistic License 2.0. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | 1; |