File Coverage

blib/lib/Tree/File/YAML.pm
Criterion Covered Total %
statement 35 39 89.7
branch 5 10 50.0
condition n/a
subroutine 10 10 100.0
pod 3 3 100.0
total 53 62 85.4


line stmt bran cond sub pod time code
1             package Tree::File::YAML;
2              
3 16     16   537217 use warnings;
  16         36  
  16         515  
4 16     16   84 use strict;
  16         27  
  16         559  
5              
6 16     16   79 use base qw(Tree::File);
  16         30  
  16         8727  
7              
8 16     16   85 use Carp qw(croak);
  16         28  
  16         773  
9 16     16   83 use File::Basename ();
  16         29  
  16         236  
10 16     16   138 use File::Path ();
  16         43  
  16         255  
11 16     16   9286 use YAML ();
  16         102865  
  16         5074  
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             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.
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             sub load_file {
61 43     43 1 81 my ($class, $filename) = @_;
62 43         180 my ($head, @tail) = YAML::LoadFile($filename);
63 43 100       334529 croak "YAML file $filename contains multiple sections" if @tail;
64 42         189 return $head;
65             }
66              
67             =head2 C<< $tree->as_yaml() >>
68              
69             This method returns the entire tree of data (returned by the C method),
70             serialized into YAML.
71              
72             =cut
73              
74             sub as_yaml {
75 1     1 1 2 my ($self) = @_;
76 1         4 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             sub write_file {
86 16     16 1 30 my ($self, $filename, $data) = @_;
87              
88 16         61 $filename =~ s{//}{/}g;
89 16         45 $filename =~ s{/\Z}{};
90              
91 16 50       379 if (-d $filename) {
92 0         0 File::Path::rmtree($filename);
93 0         0 return YAML::DumpFile($filename, $data);
94             }
95              
96 16 50       223 if (-f $filename) {
97 0         0 return YAML::DumpFile($filename, $data);
98             }
99              
100 16         503 my $dir = File::Basename::dirname($filename);
101 16 50       309 unless (-d $dir) {
102             # die "this is the problem" if -f $dir;
103 0 0       0 File::Path::mkpath($dir) unless -d $dir;
104             }
105 16         60 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<< >>
119              
120             =head1 BUGS
121              
122             Please report any bugs or feature requests to C, or
123             through the web interface at L. 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             1;