File Coverage

blib/lib/Exception/Reporter/Dumper/YAML.pm
Criterion Covered Total %
statement 29 37 78.3
branch 5 10 50.0
condition 3 8 37.5
subroutine 8 11 72.7
pod 0 1 0.0
total 45 67 67.1


line stmt bran cond sub pod time code
1 2     2   68388 use strict;
  2         14  
  2         60  
2 2     2   16 use warnings;
  2         4  
  2         109  
3             package Exception::Reporter::Dumper::YAML 0.015;
4             # ABSTRACT: a dumper to turn any scalar value into a plaintext YAML record
5              
6 2     2   436 use parent 'Exception::Reporter::Dumper';
  2         329  
  2         10  
7              
8 2     2   1023 use Try::Tiny;
  2         4234  
  2         109  
9 2     2   839 use YAML::XS ();
  2         5952  
  2         852  
10              
11             sub _ident_from {
12 8     8   119 my ($self, $str, $x) = @_;
13              
14 8         26 $str =~ s/\A\n+//;
15 8         32 ($str) = split /\n/, $str;
16              
17 8 50 33     82 unless (defined $str and length $str and $str =~ /\S/) {
      33        
18 0 0       0 $str = sprintf "<>", $x ? ' ($x)' : '';
19             }
20              
21 8         36 return $str;
22             }
23              
24             sub dump {
25 6     6 0 14 my ($self, $value, $arg) = @_;
26 6   50     20 my $basename = $arg->{basename} || 'dump';
27              
28             my ($dump, $error) = try {
29 6     6   1082 (YAML::XS::Dump($value), undef);
30             } catch {
31 0     0   0 (undef, $_);
32 6         37 };
33              
34 6 50       136 if (defined $dump) {
35 5     0   216 my $ident = ref $value ? (try { "$value" } catch { "" })
  0         0  
36 6 50       53 : defined $value ? "$value" # quotes in case of glob, vstr, etc.
    100          
37             : "(undef)";
38              
39 6         71 $ident = $self->_ident_from($ident);
40              
41             return {
42 6         41 filename => "$basename.yaml",
43             mimetype => 'text/plain',
44             body => $dump,
45             ident => $ident,
46             };
47             } else {
48 0     0     my $string = try { "$value" } catch { "value could not stringify: $_" };
  0            
  0            
49 0           my $ident = $self->_ident_from($string);
50              
51             return {
52 0           filename => "$basename.txt",
53             mimetype => 'text/plain',
54             body => <
55             __DATA__