File Coverage

blib/lib/YAML/Dumper/Base.pm
Criterion Covered Total %
statement 48 50 96.0
branch 35 46 76.0
condition 6 6 100.0
subroutine 6 7 85.7
pod 0 4 0.0
total 95 113 84.0


line stmt bran cond sub pod time code
1             package YAML::Dumper::Base;
2              
3 34     34   279 use YAML::Mo;
  34         58  
  34         178  
4              
5 34     34   906 use YAML::Node;
  34         61  
  34         26224  
6              
7             # YAML Dumping options
8             has spec_version => default => sub {'1.0'};
9             has indent_width => default => sub {2};
10             has use_header => default => sub {1};
11             has use_version => default => sub {0};
12             has sort_keys => default => sub {1};
13             has anchor_prefix => default => sub {''};
14             has dump_code => default => sub {0};
15             has use_block => default => sub {0};
16             has use_fold => default => sub {0};
17             has compress_series => default => sub {1};
18             has inline_series => default => sub {0};
19             has use_aliases => default => sub {1};
20             has purity => default => sub {0};
21             has stringify => default => sub {0};
22             has quote_numeric_strings => default => sub {0};
23              
24             # Properties
25             has stream => default => sub {''};
26             has document => default => sub {0};
27             has transferred => default => sub {{}};
28             has id_refcnt => default => sub {{}};
29             has id_anchor => default => sub {{}};
30             has anchor => default => sub {1};
31             has level => default => sub {0};
32             has offset => default => sub {[]};
33             has headless => default => sub {0};
34             has blessed_map => default => sub {{}};
35              
36             # Global Options are an idea taken from Data::Dumper. Really they are just
37             # sugar on top of real OO properties. They make the simple Dump/Load API
38             # easy to configure.
39             sub set_global_options {
40 158     158 0 237 my $self = shift;
41 158 50       340 $self->spec_version($YAML::SpecVersion)
42             if defined $YAML::SpecVersion;
43 158 100       303 $self->indent_width($YAML::Indent)
44             if defined $YAML::Indent;
45 158 100       297 $self->use_header($YAML::UseHeader)
46             if defined $YAML::UseHeader;
47 158 50       279 $self->use_version($YAML::UseVersion)
48             if defined $YAML::UseVersion;
49 158 100       267 $self->sort_keys($YAML::SortKeys)
50             if defined $YAML::SortKeys;
51 158 50       301 $self->anchor_prefix($YAML::AnchorPrefix)
52             if defined $YAML::AnchorPrefix;
53 158 100 100     656 $self->dump_code($YAML::DumpCode || $YAML::UseCode)
      100        
54             if defined $YAML::DumpCode or defined $YAML::UseCode;
55 158 100       289 $self->use_block($YAML::UseBlock)
56             if defined $YAML::UseBlock;
57 158 50       281 $self->use_fold($YAML::UseFold)
58             if defined $YAML::UseFold;
59 158 100       263 $self->compress_series($YAML::CompressSeries)
60             if defined $YAML::CompressSeries;
61 158 100       262 $self->inline_series($YAML::InlineSeries)
62             if defined $YAML::InlineSeries;
63 158 100       286 $self->use_aliases($YAML::UseAliases)
64             if defined $YAML::UseAliases;
65 158 50       285 $self->purity($YAML::Purity)
66             if defined $YAML::Purity;
67 158 100       247 $self->stringify($YAML::Stringify)
68             if defined $YAML::Stringify;
69 158 100       331 $self->quote_numeric_strings($YAML::QuoteNumericStrings)
70             if defined $YAML::QuoteNumericStrings;
71             }
72              
73             sub dump {
74 0     0 0 0 my $self = shift;
75 0         0 $self->die('dump() not implemented in this class.');
76             }
77              
78             sub blessed {
79 1     1 0 3 my $self = shift;
80 1         3 my ($ref) = @_;
81 1 50       5 $ref = \$_[0] unless ref $ref;
82 1         3 my (undef, undef, $node_id) = YAML::Mo::Object->node_info($ref);
83 1         15 $self->{blessed_map}->{$node_id};
84             }
85              
86             sub bless {
87 4     4 0 6 my $self = shift;
88 4         9 my ($ref, $blessing) = @_;
89 4         5 my $ynode;
90 4 50       9 $ref = \$_[0] unless ref $ref;
91 4         10 my (undef, undef, $node_id) = YAML::Mo::Object->node_info($ref);
92 4 100       12 if (not defined $blessing) {
    100          
93 2         5 $ynode = YAML::Node->new($ref);
94             }
95             elsif (ref $blessing) {
96 1 50       3 $self->die() unless ynode($blessing);
97 1         2 $ynode = $blessing;
98             }
99             else {
100 34     34   305 no strict 'refs';
  34         76  
  34         4701  
101 1         4 my $transfer = $blessing . "::yaml_dump";
102 1 50       1 $self->die() unless defined &{$transfer};
  1         4  
103 1         2 $ynode = &{$transfer}($ref);
  1         17  
104 1 50       6 $self->die() unless ynode($ynode);
105             }
106 4         12 $self->{blessed_map}->{$node_id} = $ynode;
107 4 50       8 my $object = ynode($ynode) or $self->die();
108 4         42 return $object;
109             }
110              
111             1;