File Coverage

lib/Unexpected/TraitFor/StringifyingError.pm
Criterion Covered Total %
statement 19 19 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 29 29 100.0


line stmt bran cond sub pod time code
1             package Unexpected::TraitFor::StringifyingError;
2              
3 4     4   1689 use namespace::autoclean;
  4         5  
  4         18  
4              
5 4     4   1146 use Unexpected::Functions qw( inflate_placeholders parse_arg_list );
  4         4  
  4         21  
6 4     4   1266 use Unexpected::Types qw( ArrayRef Bool Str );
  4         9  
  4         35  
7 4     4   2908 use Moo::Role;
  4         6  
  4         31  
8              
9             requires qw( BUILD );
10              
11             # Object attributes (public)
12             has 'args' => is => 'ro', isa => ArrayRef, default => sub { [] };
13              
14             has 'error' => is => 'ro', isa => Str, default => 'Unknown error';
15              
16             has 'no_quote_bind_values' => is => 'ro', isa => Bool, default => 0;
17              
18             # Construction
19             around 'BUILDARGS' => sub {
20             my ($orig, $self, @args) = @_; my $attr = parse_arg_list( @args );
21              
22             my $e = delete $attr->{error};
23              
24             $e and ref $e eq 'CODE' and $e = $e->( $self, $attr );
25             $e and $e .= q() and chomp $e;
26             $e and $attr->{error} = $e;
27             return $attr;
28             };
29              
30             after 'BUILD' => sub {
31             # Fixes 98c94be8-d01e-11e2-8bc5-3f0fbdbf7481 WTF? Stringify fails.
32             # Bug only happens when Moose class inherits from Moo class which
33             # uses overload string. Moose class inherits from Moose class which
34             # has consumed a ::Role::WithOverloading works. Moo inherits from
35             # Moo also works
36             my $self = shift; $self->as_string; return;
37             };
38              
39             # Public methods
40             sub as_boolean {
41 28     28 1 156 return 1;
42             }
43              
44             sub as_string { # Stringifies the error and inflates the placeholders
45 53     53 1 52 my $self = shift; my $e = $self->error;
  53         95  
46              
47 53 100       199 0 > index $e, '[_' and return "${e}\n";
48              
49 14         34 my $opts = [ '[?]', '[]', $self->no_quote_bind_values ];
50              
51 14         16 return inflate_placeholders( $opts, $e, @{ $self->args } )."\n";
  14         41  
52             }
53              
54             1;
55              
56             __END__