File/YAML.pm

filter: Statementstmt 90.2% 37 / 41 Branchbran 50.0% 5 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 10 / 10 Podpod 100% 3 / 3 totaltotal 85.9% 55 / 64 CC 6 SCAR 11.7
CC
6
Cov
82%
write_file
7.3
load_file
2.0
BEGIN
1.0
CRAP 3.2
« File.pm ↑ Summary
1 package Tree::File::YAML;
2  
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 ▶use base qw(Tree::File);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 17 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use File::Basename ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 17 ▶use File::Path ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11 17 ▶use YAML ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Tree::File::YAML - (DEPRECATED) store a data structure in a file tree (using YAML)
16  
17 =head1 VERSION
18  
19 version 0.112
20  
21 =cut
22  
23 17 our $VERSION = '0.112';
24  
25 =head1 SYNOPSIS
26  
27 use Tree::File::YAML;
28  
29 my $tree = Tree::File::YAML->new($treerot);
30  
31 die "death mandated" if $tree->get("/master/die")
32  
33 print "Hello, ", $tree->get("/login/user/name");
34  
35 $tree->set("/login/user/lastlogin", time);
36 $tree->write;
37  
38 =head1 DESCRIPTION
39  
40 This module stores configuration in a series of YAML files spread across a
41 directory tree, and provides uniform access to the data structure.
42  
43 It can load a single YAML file or a directory tree containing YAML files as
44 leaves. The tree's branches can be returned as data structures or YAML
45 documents, and the tree can be modified and rewritten. Directory-based
46 branches can be collapsed back into files and file-based branches can be
47 exploded into directories.
48  
49 For more information, see L<Tree::File>.
50  
51 =head1 METHODS
52  
53 =head2 C<< $tree->load_file($filename) >>
54  
55 This method loads the given filename as YAML, croaks if it contains more than
56 one section, and otherwise returns the contained data.
57  
58 =cut
59  
60 43 ▶sub load_file {
Subroutine load_file: called CC=2 SCAR=6.9
Pod documented
61 43 my ($class, $filename) = @_;
62 43 my ($head, @tail) = YAML::LoadFile($filename);
63 43 ▶ croak "YAML file $filename contains multiple sections" if @tail;
Branch: 1 branch
BranchTrueFalseTotal
if @tail 1 42 43
64 42 return $head;
65 }
66  
67 =head2 C<< $tree->as_yaml() >>
68  
69 This method returns the entire tree of data (returned by the C<data> method),
70 serialized into YAML.
71  
72 =cut
73  
74 1 ▶sub as_yaml {
Subroutine as_yaml: called CC=1 SCAR=0.0
Pod documented
75 1 my ($self) = @_;
76 1 YAML::Dump($self->data);
77 }
78  
79 =head2 C<< $tree->write_file($filename, $data) >>
80  
81 This method writes the given data, as YAML, to the given filename.
82  
83 =cut
84  
85 16 ▶sub write_file {
Subroutine write_file: called CC=5 SCAR=19.8
Pod documented
86 16 my ($self, $filename, $data) = @_;
87  
88 16 $filename =~ s{//}{/}g;
89 16 $filename =~ s{/\Z}{};
90  
91 16 ▶ if (-d $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $filename) 0 16 16
92 0 File::Path::rmtree($filename);
93 0 return YAML::DumpFile($filename, $data);
94 }
95  
96 16 ▶ if (-f $filename) {
Branch: 1 branch
BranchTrueFalseTotal
if (-f $filename) 0 16 16
97 0 return YAML::DumpFile($filename, $data);
98 }
99  
100 16 my $dir = File::Basename::dirname($filename);
101 16 ▶ unless (-d $dir) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d $dir) 0 16 16
102 # die "this is the problem" if -f $dir;
103 0 ▶ File::Path::mkpath($dir) unless -d $dir;
Branch: 1 branch
BranchTrueFalseTotal
unless -d $dir 0 0 0
104 }
105 16 return YAML::DumpFile($filename, $data);
106 }
107  
108 =head1 TODO
109  
110 =over
111  
112 =item * symlinks and references
113  
114 =back
115  
116 =head1 AUTHOR
117  
118 Ricardo SIGNES, C<< <rjbs@cpan.org> >>
119  
120 =head1 BUGS
121  
122 Please report any bugs or feature requests to C<bug-tree-file@rt.cpan.org>, or
123 through the web interface at L<http://rt.cpan.org>. I will be notified, and
124 then you'll automatically be notified of progress on your bug as I make
125 changes.
126  
127 =head1 COPYRIGHT
128  
129 Copyright 2005 Ricardo Signes, All Rights Reserved.
130  
131 This program is free software; you can redistribute it and/or modify it
132 under the same terms as Perl itself.
133  
134 =cut
135  
136 17 1;
« File.pm ↑ Summary