File Coverage

blib/lib/Data/Record/Serialize/Encode/yaml.pm
Criterion Covered Total %
statement 31 36 86.1
branch 2 6 33.3
condition n/a
subroutine 11 11 100.0
pod 1 1 100.0
total 45 54 83.3


line stmt bran cond sub pod time code
1              
2             # ABSTRACT: encode a record as YAML
3              
4             use Moo::Role;
5 1     1   797  
  1         10291  
  1         4  
6             use Data::Record::Serialize::Error { errors => [ 'yaml_backend' ] }, -all;
7 1     1   415 use Types::Standard qw[ Enum ];
  1         2  
  1         7  
8 1     1   603  
  1         80153  
  1         11  
9             use JSON::PP; # needed for JSON::PP::true/false
10 1     1   1345  
  1         11308  
  1         69  
11             use namespace::clean;
12 1     1   8  
  1         2  
  1         6  
13             our $VERSION = '1.04';
14              
15             BEGIN {
16             my $YAML_XS_VERSION = 0.67;
17 1     1   452  
18             if ( eval { require YAML::XS; YAML::XS->VERSION( $YAML_XS_VERSION ); 1; } )
19 1 50       4 {
  1 0       377  
  1         2209  
  1         5  
20             *encode = sub {
21             local $YAML::XS::Boolean = 'JSON::PP';
22 2     2   5 YAML::XS::Dump( $_[1] );
23 2     1   81 }
  1     1   6  
  1         2  
  1         84  
  1         6  
  1         2  
  1         60  
24             }
25 1         86 elsif ( eval { require YAML::PP } ) {
26 0         0 my $processor = YAML::PP->new( boolean => 'JSON::PP' );
27 0         0 *encode = sub { $processor->dump_string( $_[1] ) };
28 0         0 }
  0         0  
29             else {
30             error( 'yaml_backend',
31 0         0 "can't find either YAML::XS (>= $YAML_XS_VERSION) or YAML::PP. Please install one of them"
32             );
33             }
34             }
35              
36             has '+numify' => ( is => 'ro', default => 1 );
37             has '+stringify' => ( is => 'ro', default => 1 );
38              
39              
40 2     2   17  
41              
42              
43              
44              
45              
46              
47              
48              
49              
50 1 50   1 1 8  
51              
52              
53              
54             with 'Data::Record::Serialize::Role::Encode';
55              
56             1;
57              
58             #
59             # This file is part of Data-Record-Serialize
60             #
61             # This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
62             #
63             # This is free software, licensed under:
64             #
65             # The GNU General Public License, Version 3, June 2007
66             #
67              
68              
69             =pod
70              
71             =for :stopwords Diab Jerius Smithsonian Astrophysical Observatory
72              
73             =head1 NAME
74              
75             Data::Record::Serialize::Encode::yaml - encode a record as YAML
76              
77             =head1 VERSION
78              
79             version 1.04
80              
81             =head1 SYNOPSIS
82              
83             use Data::Record::Serialize;
84              
85             my $s = Data::Record::Serialize->new( encode => 'yaml', ... );
86              
87             $s->send( \%record );
88              
89             =head1 DESCRIPTION
90              
91             B<Data::Record::Serialize::Encode::yaml> encodes a record as YAML. It uses uses either L<YAML::XS> or L<YAML::PP>.
92              
93             It performs the L<Data::Record::Serialize::Role::Encode> role.
94              
95             =head1 METHODS
96              
97             =head2 to_bool
98              
99             $bool = $self->to_bool( $truthy );
100              
101             Convert a truthy value to something that the YAML encoders will recognize as a boolean.
102              
103             =for Pod::Coverage encode
104              
105             =for Pod::Coverage numify
106             stringify
107              
108             =head1 CONSTRUCTOR OPTIONS
109              
110             =over
111              
112             =item backend => C<YAML::XS> | C<YAML::PP>
113              
114             Optional. Which YAML backend to use. If not specified, searches for one of the two.
115              
116             =back
117              
118             =head1 SUPPORT
119              
120             =head2 Bugs
121              
122             Please report any bugs or feature requests to bug-data-record-serialize@rt.cpan.org or through the web interface at: https://rt.cpan.org/Public/Dist/Display.html?Name=Data-Record-Serialize
123              
124             =head2 Source
125              
126             Source is available at
127              
128             https://gitlab.com/djerius/data-record-serialize
129              
130             and may be cloned from
131              
132             https://gitlab.com/djerius/data-record-serialize.git
133              
134             =head1 SEE ALSO
135              
136             Please see those modules/websites for more information related to this module.
137              
138             =over 4
139              
140             =item *
141              
142             L<Data::Record::Serialize|Data::Record::Serialize>
143              
144             =back
145              
146             =head1 AUTHOR
147              
148             Diab Jerius <djerius@cpan.org>
149              
150             =head1 COPYRIGHT AND LICENSE
151              
152             This software is Copyright (c) 2017 by Smithsonian Astrophysical Observatory.
153              
154             This is free software, licensed under:
155              
156             The GNU General Public License, Version 3, June 2007
157              
158             =cut