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   193610 use v5.12;
  2         5  
6 2     2   853 use Moo::Role;
  2         27271  
  2         12  
7              
8 2     2   1623 use Data::Record::Serialize::Error { errors => ['yaml_backend'] }, -all;
  2         4  
  2         24  
9 2     2   1710 use Types::Standard qw[ Enum ];
  2         237993  
  2         20  
10              
11 2     2   5576 use JSON::PP; # needed for JSON::PP::true/false
  2         14529  
  2         201  
12              
13 2     2   599 use namespace::clean;
  2         14450  
  2         25  
14              
15             our $VERSION = '2.02';
16              
17             BEGIN {
18 2     2   943 my $YAML_XS_VERSION = 0.67;
19              
20 2 50       6 if ( eval { require YAML::XS; YAML::XS->VERSION( $YAML_XS_VERSION ); 1; } ) {
  2 0       1027  
  2         5531  
  2         11  
21             *encode = sub {
22             ## no critic (Variables::ProhibitPackageVars)
23 2     2   11 local $YAML::XS::Boolean = 'JSON::PP';
24 2     1   114 YAML::XS::Dump( $_[1] );
  1     1   7  
  1         1  
  1         86  
  1         6  
  1         2  
  1         59  
25 2         188 };
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   88 sub _needs_eol { 1 }
41              
42              
43              
44              
45              
46              
47              
48              
49              
50 1 50   1 1 23 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__