File Coverage

blib/lib/Data/Record/Serialize/Encode/yaml.pm
Criterion Covered Total %
statement 33 38 86.8
branch 2 6 33.3
condition n/a
subroutine 12 12 100.0
pod 1 1 100.0
total 48 57 84.2


line stmt bran cond sub pod time code
1             package Data::Record::Serialize::Encode::yaml;
2              
3             # ABSTRACT: encode a record as YAML
4              
5 2     2   171713 use v5.12;
  2         9  
6 2     2   746 use Moo::Role;
  2         23131  
  2         11  
7              
8 2     2   1362 use Data::Record::Serialize::Error { errors => ['yaml_backend'] }, -all;
  2         4  
  2         21  
9 2     2   1473 use Types::Standard qw[ Enum ];
  2         196650  
  2         19  
10              
11 2     2   5043 use JSON::PP; # needed for JSON::PP::true/false
  2         13062  
  2         192  
12              
13 2     2   476 use namespace::clean;
  2         13482  
  2         24  
14              
15             our $VERSION = '2.03';
16              
17             BEGIN {
18 2     2   886 my $YAML_XS_VERSION = 0.67;
19              
20 2 50       3 if ( eval { require YAML::XS; YAML::XS->VERSION( $YAML_XS_VERSION ); 1; } ) {
  2 0       930  
  2         5554  
  2         10  
21             *encode = sub {
22             ## no critic (Variables::ProhibitPackageVars)
23 2     2   11 local $YAML::XS::Boolean = 'JSON::PP';
24 2     1   112 YAML::XS::Dump( $_[1] );
  1     1   7  
  1         1  
  1         123  
  1         8  
  1         2  
  1         65  
25 2         233 };
26             }
27 0         0 elsif ( eval { require YAML::PP } ) {
28 0         0 my $processor = YAML::PP->new( boolean => 'JSON::PP' );
29 0         0 *encode = sub { $processor->dump_string( $_[1] ) };
  0         0  
30             }
31             else {
32 0         0 error( 'yaml_backend',
33             "can't find either YAML::XS (>= $YAML_XS_VERSION) or YAML::PP. Please install one of them" );
34             }
35             }
36              
37             has '+numify' => ( is => 'ro', default => 1 );
38             has '+stringify' => ( is => 'ro', default => 1 );
39              
40 2     2   11 sub _needs_eol { 1 }
41              
42              
43              
44              
45              
46              
47              
48              
49              
50 1 50   1 1 9 sub to_bool { $_[1] ? JSON::PP::true : JSON::PP::false }
51              
52              
53              
54              
55              
56              
57             with 'Data::Record::Serialize::Role::Encode';
58              
59             1;
60              
61             #
62             # This file is part of Data-Record-Serialize
63             #
64             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
65             #
66             # This is free software, licensed under:
67             #
68             # The GNU General Public License, Version 3, June 2007
69             #
70              
71             __END__