Valiant/Error.pm

filter: Statementstmt 96.7% 181 / 187 Branchbran 77.6% 59 / 76 Conditioncond 67.3% 35 / 52 MC/DCmcdc 32.1% 9 / 28 Subroutinesub 94.4% 17 / 18 Podpod 50.0% 6 / 12 totaltotal 82.3% 307 / 373 CC 62 SCAR 24.2
CC
62
Cov
87%
generate_message
18.1
message
15.8
match
11.4
CRAP 11.3
« Valiant/HTML/Util/TagBuilder.pm ↑ Summary Valiant/HTML/Util/FormTags.pm »
1 package Valiant::Error;
2  
3 40 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 40 ▶use Text::Autoformat 'autoformat';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 40 ▶use Module::Runtime;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 40 ▶use FreezeThaw;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 40 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # These groups are often present in the options hash and need to be removed
10 # before passing options onto other classes in some cases
11  
12 40 my @CALLBACKS_OPTIONS = (qw(if unless on allow_undef allow_blank strict));
13 40 my @MESSAGE_OPTIONS = (qw(message));
14  
15 # object is the underlying object that has an error. This object
16 # must do the 'validatable' role.
17  
18 40 has 'object' => (
19 is => 'ro',
20 required => 1,
21 weak_ref => 1, # not sure about this...
22 );
23  
24 # The type of the error, a string
25 40 has ['type', 'raw_type'] => (
26 is => 'ro',
27 required => 1,
28 );
29  
30 # The attribute that has the error. If undef that means its
31 # a model error (an error on the model itself in general).
32 40 has attribute => (is=>'ro', required=>0, predicate=>'has_attribute');
33  
34 # The value which caused the error.
35 40 has bad_value => (is=>'ro', required=>1);
36  
37 # A hashref of extra meta info (it is allowed to be an empty hash)
38 40 has options => (is=>'ro', required=>1);
39  
40 # holds a local reference to the i18n object
41 40 has i18n => (is=>'ro', required=>1);
42  
43 # Easier to override for subclassers. Do we want to check the options attribute
44 # to make it easier to set without subclassing? Something to think about.
45 4 ▶sub i18n_class { 'Valiant::I18N' }
Subroutine i18n_class: called CC=1 SCAR=0.0
Pod undocumented
46 237 ▶sub default_format { '{{attribute}} {{message}}' }
Subroutine default_format: called CC=1 SCAR=0.0
Pod undocumented
47  
48 285 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=8 SCAR=21.2
49 285 my ($orig, $class, @args) = @_;
50 285 my $options = $class->$orig(@args);
51  
52 # Pull the main attributes out of the options hashref
53 285 my ($object, $attribute, $type, $i18n, $set_options, $bad_value) = delete @{$options}{qw/object attribute type i18n options bad_value/};
54 # Get the i18n from options if passed, otherwise from the model if the model so
55 # defines it, lastly just make one if we need it.
56 285 ▶ $i18n ||= $object->can('i18n') ?
Branch: 1 branch
BranchTrueFalseTotal
$object->can('i18n') ? : 4 4 8
Condition66%2/3
$i18n ||= $object->can("i18n") ? $object->i18n : &Module::Runtime::use_module(scalar $class->i18n_class)
l!l&&r!l&&!r
27780
MC/DC0%0/2
$i18n ||= $object->can("i18n") ? $object->i18n : &Module::Runtime::use_module(scalar $class->i18n_class)
$i18n $object->can("i18n") ? $object->i18n : &Module::Runtime::use_module(scalar $class->i18n_class)
Truth table66%2/3
A ||= B
A= $i18nB= $object->can("i18n") ? $object->i18n : &Module::Runtime::use_module(scalar $class->i18n_class)
ABresult
1X1
011
000
57 $object->i18n :
58 Module::Runtime::use_module($class->i18n_class);
59  
60 # set a default error type
61 285 ▶ unless(defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 285 285
62 0 $type = $i18n->make_tag('invalid');
63 }
64  
65 285 ▶ unless(defined($bad_value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $bad_value) 285 0 285
66 285 ▶ if($attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute) { } 267 18 285
67 267 $bad_value = $object->read_attribute_for_validation($attribute);
68 } else {
69 18 $bad_value = $object; # Its a model error
70 }
71 }
72  
73 return +{
74 object => $object,
75 attribute => $attribute,
76 type => $type,
77 i18n => $i18n,
78 raw_type => $type,
79 bad_value => $bad_value,
80 options => +{
81 285 ▶ %{$options||{}},
Condition50%1/2
$options || {}
l!l
2850
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
82 285 ▶ %{$set_options||{}}
Condition100%2/2
$set_options || {}
l!l
2787
MC/DC100%1/1
$set_options || {}
$set_options
Truth table100%2/2
A
A= $set_options
Aresult
11
00
83 },
84 }
85 40 };
86  
87 # This takes an already translated error message part and creates a full message
88 # by combining it with the attribute human name (which itself needs to be translated
89 # if translation info exists for it) using a template 'format'. You can have a format
90 # for each attribute or model/attribute combination or use a default format.
91  
92 248 ▶sub full_message {
Subroutine full_message: called CC=11 SCAR=24.2
Pod undocumented
93 248 my $self = shift;
94  
95 # We need to do this dance since full_message needs to be called with two
96 # different signatures. In some places we even call it as a class method
97 # (that's why we do the i18n dance below as well)
98 248 my ($attribute, $message, $object, $i18n) = @_;
99 248 ▶ $attribute ||= $self->attribute if $self->has_attribute;
Branch: 1 branch
BranchTrueFalseTotal
if $self->has_attribute 248 0 248
Condition66%2/3
$attribute ||= $self->attribute
l!l&&r!l&&!r
023711
MC/DC50%1/2
$attribute ||= $self->attribute
$attribute $self->attribute
Truth table66%2/3
A ||= B
A= $attributeB= $self->attribute
ABresult
1X1
011
000
100 248 ▶ $message ||= $self->message;
Condition33%1/3
$message ||= $self->message
l!l&&r!l&&!r
02480
MC/DC0%0/2
$message ||= $self->message
$message $self->message
Truth table33%1/3
A ||= B
A= $messageB= $self->message
ABresult
1X1
011
000
101 248 ▶ $object ||= $self->object;
Condition33%1/3
$object ||= $self->object
l!l&&r!l&&!r
02480
MC/DC0%0/2
$object ||= $self->object
$object $self->object
Truth table33%1/3
A ||= B
A= $objectB= $self->object
ABresult
1X1
011
000
102 248 ▶ $i18n ||= Scalar::Util::blessed($self) ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 248 0 248
Condition33%1/3
$i18n ||= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
l!l&&r!l&&!r
02480
MC/DC0%0/2
$i18n ||= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
$i18n builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
Truth table33%1/3
A ||= B
A= $i18nB= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
ABresult
1X1
011
000
103 $self->i18n :
104 Module::Runtime::use_module($self->i18n_class);
105  
106 248 ▶ return $message unless defined($attribute);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attribute 11 237 248
107  
108 237 my @defaults = ();
109 237 ▶ if($object->can('i18n_scope')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("i18n_scope")) 230 7 237
110 230 $attribute =~s/\.\d+//g;
111 230 $attribute =~s/\[\d+\]//g;
112 230 my $i18n_scope = $object->i18n_scope;
113 230 my @parts = split '\.', $attribute; # For nested attributes
114 230 my $attribute_name = pop @parts;
115 230 ▶ my $namespace = join '/', @parts if @parts;
Branch: 1 branch
BranchTrueFalseTotal
if @parts 19 211 230
116 230 my $attributes_scope = "${i18n_scope}.errors.models";
117 230 ▶ if($namespace) {
Branch: 1 branch
BranchTrueFalseTotal
if ($namespace) { } 19 211 230
118 @defaults = map {
119 19 my $class = $_;
120 19 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.attributes.${attribute_name}.format/@{[ $self->type ]}",
121 19 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.attributes.${attribute_name}.format",
122 19 "${attributes_scope}.${\$class->model_name->i18n_key}/${namespace}.format";
123 19 } grep { $_->model_name->can('i18n_key') } $object->i18n_lookup;
124 } else {
125 @defaults = map {
126 233 my $class = $_;
127 233 "${attributes_scope}.${\$class->model_name->i18n_key}.attributes.${attribute_name}.format/@{[ $self->type ]}",
128 233 "${attributes_scope}.${\$class->model_name->i18n_key}.attributes.${attribute_name}.format",
129 233 "${attributes_scope}.${\$class->model_name->i18n_key}.format";
130 211 } grep { $_->model_name->can('i18n_key') } $object->i18n_lookup;
131 }
132 }
133  
134 237 @defaults = map { $i18n->make_tag($_) } @defaults;
135  
136 237 push @defaults, $i18n->make_tag("errors.format.attributes.${attribute}"); # This isn't in Rails but I find it useful
137 237 push @defaults, $i18n->make_tag("errors.format");
138  
139 # This last one
140 237 push @defaults, $self->default_format;
141  
142 # We do this dance to cope with nested attributes like 'user.name'.
143 237 my $attr_name = do {
144 237 my $human_attr = $attribute;
145 237 $human_attr =~s/\./ /g;
146 237 $human_attr =~s/\[\d+\]//g;
147 237 $human_attr =~s/_id$//; # remove trailing _id
148 237 $human_attr =~s/_/ /g;
149 237 $human_attr = autoformat $human_attr, {case=>'title'};
150 237 $human_attr =~s/[\n]//g; # Is this a bug in Text::Autoformat???
151 237 $human_attr;
152 };
153  
154 237 $attr_name = $object->human_attribute_name($attribute, +{default=>$attr_name});
155  
156 237 return my $translated = $i18n->translate(
157 shift @defaults,
158 default => \@defaults,
159 attribute => $attr_name,
160 message => $message
161 );
162 }
163  
164 # Generates a message part which is a text message of the error
165 # without the attribute or other bits. In rails this is a class
166 # method; I preserved the given API for now but for the most
167 # part you should just call ->message which does the right
168 # thing for this error (or ->full_message).
169  
170 161 ▶sub generate_message {
Subroutine generate_message: called CC=17 SCAR=29.0
Pod undocumented
171 161 my ($self, $attribute, $type, $object, $options, $i18n) = @_;
172 161 ▶ $i18n ||= Scalar::Util::blessed($self) ?
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($self) ? : 161 0 161
Condition33%1/3
$i18n ||= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
l!l&&r!l&&!r
01610
MC/DC0%0/2
$i18n ||= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
$i18n builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
Truth table33%1/3
A ||= B
A= $i18nB= builtin::blessed($self) ? $self->i18n : &Module::Runtime::use_module(scalar $self->i18n_class)
ABresult
1X1
011
000
173 $self->i18n :
174 Module::Runtime::use_module($self->i18n_class);
175  
176 161 ▶ $options ||= +{};
Condition50%1/2
$options ||= {}
l!l
1610
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
177 161 ▶ $type = delete $options->{message} if $i18n->is_i18n_tag($options->{message}||'');
Branch: 1 branch
BranchTrueFalseTotal
if $i18n->is_i18n_tag($options->{"message"} || "") 4 157 161
Condition100%2/2
$options->{'message'} || ''
l!l
4157
MC/DC100%1/1
$options->{'message'} || ''
$options->{'message'}
Truth table100%2/2
A
A= $options->{'message'}
Aresult
11
00
178  
179 161 my $local_attribute;
180 161 ▶ if(defined $attribute) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $attribute) 160 1 161
181 160 ▶ $local_attribute = $attribute if defined $attribute;
Branch: 1 branch
BranchTrueFalseTotal
if defined $attribute 160 0 160
182 160 $local_attribute =~s/\.\d+//g;
183 160 $local_attribute =~s/\[\d+\]//g;
184 }
185  
186 # There's only a value associated with this error if there is an attribute
187 # as well. Otherwise its just an error on the model as a whole
188 161 ▶ my $value = defined($local_attribute) ?
Branch: 1 branch
BranchTrueFalseTotal
defined $local_attribute ? : 160 1 161
189 $object->read_attribute_for_validation($local_attribute) :
190 undef;
191  
192 my %options = (
193 model => $object->model_name->human,
194 attribute => defined($local_attribute) ? $object->human_attribute_name($local_attribute, $options) : undef,
195 value => $value,
196 object => $object,
197 161 ▶ %{$options||+{}},
Branch: 1 branch
BranchTrueFalseTotal
defined $local_attribute ? : 160 1 161
Condition50%1/2
$options || {}
l!l
1610
MC/DC0%0/1
$options || {}
$options
Truth table50%1/2
A
A= $options
Aresult
11
00
198 );
199  
200 161 my @defaults = ();
201 161 ▶ if($object->can('i18n_scope')) {
Branch: 1 branch
BranchTrueFalseTotal
if ($object->can("i18n_scope")) 154 7 161
202 154 my $i18n_scope = $object->i18n_scope;
203 @defaults = map {
204 165 my $class = $_;
205 165 ▶ (defined($local_attribute) ? "${i18n_scope}.errors.models.${\$class->model_name->i18n_key}.attributes.${local_attribute}.${$type}" : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $local_attribute ? : 162 3 165
206 165 "${i18n_scope}.errors.models.${\$class->model_name->i18n_key}.${$type}";
207 } grep {
208 154 ▶ $_->model_name->can('i18n_key')
Branch: 1 branch
BranchTrueFalseTotal
if $object->can("i18n_lookup") 153 1 154
209 } $object->i18n_lookup if $object->can('i18n_lookup');
210 154 push @defaults, "${i18n_scope}.errors.messages.${$type}";
211 }
212  
213 161 ▶ push @defaults, "errors.attributes.${local_attribute}.${$type}" if defined($local_attribute);
Branch: 1 branch
BranchTrueFalseTotal
if defined $local_attribute 160 1 161
214 161 push @defaults, "errors.messages.${$type}";
215  
216 161 @defaults = map { $i18n->make_tag($_) } @defaults;
217  
218 161 my $key = shift(@defaults);
219 161 ▶ if($options->{message}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"message"}) 0 161 161
220 0 my $message = delete $options->{message};
221 0 ▶ @defaults = ref($message) ? @$message : ($message);
Branch: 1 branch
BranchTrueFalseTotal
ref $message ? : 0 0 0
222 }
223 161 $options{default} = \@defaults;
224 161 return my $translated = $i18n->translate($key, %options);
225 }
226  
227 363 ▶sub message {
Subroutine message: called CC=15 SCAR=27.6
Pod documented
228 363 my $self = shift;
229 363 ▶ my $type = exists($self->options->{message}) ? $self->options->{message} : $self->raw_type;
Branch: 1 branch
BranchTrueFalseTotal
exists $self->options->{'message'} ? : 21 342 363
230
231 363 ▶ if($self->i18n->is_i18n_tag($type)) {
Branch: 3 branches
BranchTrueFalseTotal
if ($self->i18n->is_i18n_tag($type)) { } 161 202 363
elsif ((ref $type || '') eq 'CODE') { } 1 201 202
elsif ((ref $type || '') eq 'SCALAR') { } 1 200 201
232 161 my %options = %{$self->options};
233 161 delete @options{@CALLBACKS_OPTIONS};
234 161 return $self->generate_message($self->attribute, $type, $self->object, \%options);
235 ▶ } elsif((ref($type)||'') eq 'CODE') {
Condition100%2/2
ref $type || ''
l!l
2200
MC/DC100%1/1
ref $type || ''
ref $type
Truth table100%2/2
A
A= ref $type
Aresult
11
00
236 1 my $attribute = $self->attribute;
237 1 ▶ my $value = defined($attribute) ? $self->object->read_attribute_for_validation($attribute) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute ? : 1 0 1
238 my %options = (
239 model => $self->object->model_name->human,
240 attribute => defined($attribute) ? $self->object->human_attribute_name($attribute, $self->options) : undef,
241 value => $value,
242 object => $self->object,
243 1 ▶ %{$self->options||+{}},
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute ? : 1 0 1
Condition50%1/2
$self->options || {}
l!l
10
MC/DC0%0/1
$self->options || {}
$self->options
Truth table50%1/2
A
A= $self->options
Aresult
11
00
244 );
245 1 delete @options{@CALLBACKS_OPTIONS, @MESSAGE_OPTIONS};
246 1 my $return = $type->($self->object, $attribute, $value, \%options);
247 # Allow a coderef to either return a straight up string or a translation tag
248 1 ▶ return $self->i18n->is_i18n_tag($return) ?
Branch: 1 branch
BranchTrueFalseTotal
$self->i18n->is_i18n_tag($return) ? : 0 1 1
249 $self->generate_message($self->attribute, $return, $self->object, \%options) :
250 $return;
251 ▶ } elsif((ref($type)||'') eq 'SCALAR') {
Condition100%2/2
ref $type || ''
l!l
1200
MC/DC100%1/1
ref $type || ''
ref $type
Truth table100%2/2
A
A= ref $type
Aresult
11
00
252 1 my $attribute = $self->attribute;
253 1 ▶ my $value = defined($attribute) ? $self->object->read_attribute_for_validation($attribute) : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute ? : 1 0 1
254 my %options = (
255 model => $self->object->model_name->human,
256 attribute => defined($attribute) ? $self->object->human_attribute_name($attribute, $self->options) : undef,
257 value => $value,
258 object => $self->object,
259 1 ▶ %{$self->options||+{}},
Branch: 1 branch
BranchTrueFalseTotal
defined $attribute ? : 1 0 1
Condition50%1/2
$self->options || {}
l!l
10
MC/DC0%0/1
$self->options || {}
$self->options
Truth table50%1/2
A
A= $self->options
Aresult
11
00
260 );
261 1 delete @options{@CALLBACKS_OPTIONS, @MESSAGE_OPTIONS};
262  
263 1 my $translated = $$type;
264 1 ▶ $translated =~ s/\{\{([^}]+)\}\}/ defined($options{$1}) ? $options{$1} : '' /gex;
Branch: 1 branch
BranchTrueFalseTotal
defined $options{$1} ? : 4 0 4
265 1 return $translated;
266 } else {
267 # Its just a plain string
268 200 return $type;
269 }
270 }
271  
272 3 ▶sub detail {
Subroutine detail: called CC=1 SCAR=0.0
Pod documented
273 3 my $self = shift;
274 3 my %options = %{$self->options};
275 3 delete @options{@CALLBACKS_OPTIONS, @MESSAGE_OPTIONS};
276 return +{
277 3 error => $self->raw_type,
278 %options,
279 };
280 }
281  
282 # This match returns true if at least some bits match
283 2629 ▶sub match {
Subroutine match: called CC=11 SCAR=24.3
Pod documented
284 2629 my ($self, $attribute, $type, $options) = @_;
285 2629 ▶ if(
Branch: 1 branch
BranchTrueFalseTotal
if (($attribute || "") ne ($self->attribute || "") or $type and $self->type ne $type) 2366 263 2629
Condition100%10/10
($attribute || "") ne ($self->attribute || "") or $type and $self->type ne $type
l!l&&r!l&&!r
23624263
$attribute || ''
l!l
26272
$self->attribute || ''
l!l
2348281
$type and $self->type ne $type
!ll&&!rl&&r
25584
MC/DC66%2/3
($attribute || "") ne ($self->attribute || "") or $type and $self->type ne $type
$attribute $type $self->type ne $type
MC/DC100%1/1
$self->attribute || ''
$self->attribute
Truth table75%3/4
A or B and C
A= $attributeB= $typeC= $self->type ne $type
ABCresult
1XX1
0111
00X0
0100
Truth table100%2/2
A
A= $self->attribute
Aresult
11
00
286 ($attribute||'') ne ($self->attribute||'')
287 ||
288 ($type && ($self->type ne $type))
289 ) {
290 2366 return 0;
291 }
292  
293 # only the passed options need to match. So if there's options
294 # in the error object that are not in the passed options its
295 # still ok to match.
296 263 ▶ foreach my $key (keys %{$options||+{}}) {
Condition100%2/2
$options || {}
l!l
2261
MC/DC100%1/1
$options || {}
$options
Truth table100%2/2
A
A= $options
Aresult
11
00
297 3 ▶ if( ($self->options->{$key}||'') ne ($options->{$key}||'')) {
Branch: 1 branch
BranchTrueFalseTotal
if (($self->options->{$key} || "") ne ($options->{$key} || "")) 0 3 3
Condition50%2/4
$self->options->{$key} || ''
l!l
30
$options->{$key} || ''
l!l
30
MC/DC0%0/1
$self->options->{$key} || ''
$self->options->{$key}
MC/DC0%0/1
$options->{$key} || ''
$options->{$key}
Truth table50%1/2
A
A= $self->options->{$key}
Aresult
11
00
Truth table50%1/2
A
A= $options->{$key}
Aresult
11
00
298 0 return 0;
299 }
300 }
301 263 return 1;
302 }
303  
304 2 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
305 2 my $self = shift;
306 2 my $class = ref $self;
307 2 return $class->new(
308 object => $self->object,
309 attribute => $self->attribute,
310 type => $self->type,
311 i18n => $self->i18n,
312 options => $self->options,
313 );
314 }
315  
316 6 ▶sub strict_match {
Subroutine strict_match: called CC=4 SCAR=13.9
Pod documented
317 6 my ($self, $attribute, $type, $options) = @_;
318 6 ▶ return 0 unless $self->match($attribute, $type);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->match($attribute, $type) 2 4 6
319 4 ▶ return 1 unless defined $options;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $options 2 2 4
320  
321 # This is different from match because ALL the keys/values in options need to match
322 # exactly. Its possible my approach here is suspect around object comparisons.
323 2 my %options = %{$self->options};
324 2 delete @options{@CALLBACKS_OPTIONS, @MESSAGE_OPTIONS};
325  
326 2 ▶ return FreezeThaw::cmpStr(\%options, $options) == 0 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
FreezeThaw::cmpStr(\%options, $options) == 0 ? : 1 1 2
327 }
328  
329 # Are two errors the same?
330 9 ▶sub equals {
Subroutine equals: called CC=2 SCAR=7.0
Pod documented
331 9 my ($self, $target) = @_;
332  
333 9 ▶ return 0 unless ref($self) eq ref($target);
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self eq ref $target 0 9 9
334  
335 9 my $a = FreezeThaw::freeze $self->attributes_for_hash;
336 9 my $b = FreezeThaw::freeze $target->attributes_for_hash;
337  
338 9 return $a eq $b;
339 }
340  
341 0 ▶sub hash {
Subroutine hash: not called CC=1 SCAR=6.9
Pod undocumented
342 0 my $self = shift;
343 0 return +{ $self->attributes_for_hash };
344 }
345  
346 18 ▶sub attributes_for_hash {
Subroutine attributes_for_hash: called CC=1 SCAR=0.0
Pod undocumented
347 18 my $self = shift;
348 18 my %options = %{$self->options};
349 18 delete @options{@CALLBACKS_OPTIONS};
350  
351 return (
352 object => $self->object,
353 attribute => $self->attribute,
354 raw_type => $self->raw_type,
355 18 map { $_ => $options{$_} } sort keys %options, # This needs sorting to support checking equality
356 );
357 }
358  
359 =head1 NAME
360  
361 Valiant::Error - A single error encountered during validation.
362  
363 =head1 SYNOPSIS
364  
365 This won't be used standalone. Its always a collection of Error objects
366 inside the Valiant::Errors module.
367  
368 =head1 DESCRIPTION
369  
370 A Single Error.
371  
372 This is generally an internal class and you are unlikely to use it directly. For
373 the most part its used by L<Valiant::Errors>.
374  
375 =head1 ATTRIBUTES
376  
377 This class defines the following attributes
378  
379 =head2 type
380  
381 Either a translation tag or text string of the error
382  
383 =head2 attribute
384  
385 The attribute which is associated with the error or undef if the error
386 is for the model
387  
388 =head1 METHODS
389  
390 This class exposes the following methods for public users.
391  
392 =head2 message
393  
394 Returns the translated, human readable message for this error. Resolves the
395 error's type: a translation tag is localized via L<Valiant::I18N> (an override
396 passed as the C<message> option wins over the original type), a coderef is
397 called with C<< ($object, $attribute, $value, \%options) >> and may return
398 either a string or a translation tag, a scalar ref is treated as a template
399 with C<{{placeholder}}> substitution, and a plain string is returned as is.
400  
401 =head2 detail
402  
403 Returns a hashref of the raw error information: the untranslated error type
404 under the C<error> key, plus any options that were passed when the error was
405 added (things like C<count> or C<minimum>). Useful for programmatic
406 inspection when you need to know I<what> failed rather than the rendered
407 message. See L<Valiant::Errors/details> for the collection level version.
408  
409 =head2 match ($attribute, $type, $options)
410  
411 Returns true of the error matches (has the same parameters) as the arguments
412 passed. This differs from L</strict_match> in that C<$options> don't need to
413 be completely identical; it must ony be the case that the C<$options> passed
414 in the arguments intersects with that actual options of the error (that is
415 the error has to have everything in C<$options> BUT it can have more things.
416  
417 =head2 strict_match ($attribute, $type, $options)
418  
419 Same as L</match> except <$options> must exactly match and not just intersect.
420  
421 =head2 equals ($error)
422  
423 Returna a boolean based on if C<$error> is functionally equal. By this it is meant
424 that it has the same attribute values (not that it is the same error instance).
425  
426 This includes C<options> matching.
427  
428 =head2 clone
429  
430 Create a new copy of the error.
431  
432 =head1 SEE ALSO
433
434 L<Valiant>, L<Valiant::Errors>.
435  
436 =head1 AUTHOR
437
438 See L<Valiant>
439
440 =head1 COPYRIGHT & LICENSE
441
442 See L<Valiant>
443  
444 =cut
445  
446 40 1;
« Valiant/HTML/Util/TagBuilder.pm ↑ Summary Valiant/HTML/Util/FormTags.pm »