File Coverage

blib/lib/Directory/Scratch/Structured.pm
Criterion Covered Total %
statement 51 51 100.0
branch 4 4 100.0
condition n/a
subroutine 14 14 100.0
pod 3 3 100.0
total 72 72 100.0


line stmt bran cond sub pod time code
1              
2             package Directory::Scratch::Structured ;
3              
4 4     4   3894383 use strict;
  4         13  
  4         357  
5 4     4   26 use warnings ;
  4         9  
  4         278  
6              
7             BEGIN 
8             {
9 4     4   8131 use Sub::Exporter -setup => { exports => [ qw(create_structured_tree), piggyback_directory_scratch => \&piggyback ] } ;
  4         56117  
  4         77  
10 4     4   1389 use Sub::Install ;
  4         8  
  4         19  
11              
12 4     4   142 use vars qw ($VERSION);
  4         7  
  4         292  
13 4     4   78 $VERSION  = '0.04';
14             }
15              
16             #-------------------------------------------------------------------------------
17              
18 4     4   10583 use English qw( -no_match_vars ) ;
  4         22209  
  4         26  
19              
20 4     4   553047 use Readonly ;
  4         12960  
  4         321  
21             Readonly my $EMPTY_STRING => q{} ;
22             Readonly my $ROOT_DIRECTORY => q{.} ;
23              
24 4     4   29 use Carp qw(carp croak confess) ;
  4         5  
  4         216  
25              
26 4     4   4541 use Directory::Scratch ;
  4         391054  
  4         74  
27              
28             #-------------------------------------------------------------------------------
29              
30             =head1 NAME
31            
32             Directory::Scratch::Structured - creates temporary files and directories from a structured description
33            
34             =head1 SYNOPSIS
35            
36             my %tree_structure =
37             (
38             dir_1 =>
39             {
40             subdir_1 =>{},
41             file_1 =>[],
42             file_a => [],
43             },
44             dir_2 =>
45             {
46             subdir_2 =>
47             {
48             file_22 =>[],
49             file_2a =>[],
50             },
51             file_2 =>[],
52             file_a =>['12345'],
53             file_b =>[],
54             },
55            
56             file_0 => [] ,
57             ) ;
58            
59             use Directory::Scratch::Structured qw(create_structured_tree) ;
60             my $temporary_directory = create_structured_tree(%tree_structure) ;
61            
62             or
63            
64             use Directory::Scratch ;
65             use Directory::Scratch::Structured qw(piggyback_directory_scratch) ;
66            
67             my $temporary_directory = Directory::Scratch->new;
68             $temporary_directory->create_structured_tree(%tree_structure) ;
69            
70            
71             =head1 DESCRIPTION
72            
73             This module adds a I<create_structured_tree> subroutine to the L<Directory::Scratch>.
74            
75             =head1 DOCUMENTATION
76            
77             I needed a subroutine to create a bunch of temporary directories and files while running tests. I used the excellent
78             L<Directory::Scratch> to implement such a functionality. I proposed the subroutine to the L<Directory::Scratch> author
79             but he preferred to implement a subroutine using an unstructured input data based on the fact that L<Directory::Scratch>
80             didn't use structured data. This is, IMHO, flawed design, though it may require slightly less typing.
81            
82             I proposed a hybrid solution to reduce the amount of subroutines and integrate the subroutine using structured input into
83             L<Directory::Scratch> but we didn't reach an agreement on the API. Instead I decided that I would piggyback on L<Directory::Scratch>.
84            
85             You can access I<create_structured_tree> through a subroutine or a method through a L<Directory::Scratch> object.
86            
87             Whichever interface you choose, the argument to the I<create_structured_tree> consists of tuples (hash entries). The key represents
88             the name of the object to create in the directory.
89            
90             If the value is of type:
91            
92             =over 2
93            
94             =item ARRAY
95            
96             A file will be created, it's contents are the contents of the array (See L<Directory::Scratch>)
97            
98             =item HASH
99            
100             A directory will be created. the element of the hash will also be , recursively, created
101            
102             =item OTHER
103            
104             The subroutine will croak.
105            
106             =back
107            
108             =head1 SUBROUTINES/METHODS
109            
110             =cut
111              
112              
113             #-------------------------------------------------------------------------------
114              
115             sub create_structured_tree
116             {
117              
118             =head2 create_structured_tree
119            
120             use Directory::Scratch::Structured qw(create_structured_tree) ;
121            
122             my $temporary_directory = create_structured_tree(%tree_structure) ;
123             my $base = $temporary_directory->base() ;
124            
125             Returns a default L<Directory::Scratch> object.
126            
127             =cut
128              
129 2     2 1 285 my (%directory_entries) = @_ ;
130              
131 2         20 my $temporary_directory = new Directory::Scratch() ;
132              
133 2         2265 _create_structured_tree($temporary_directory, \%directory_entries, $ROOT_DIRECTORY) ;
134              
135 1         6 return($temporary_directory ) ;
136             }
137              
138             #-------------------------------------------------------------------------------
139              
140             sub directory_scratch_create_structured_tree
141             {
142              
143             =head2 directory_scratch_create_structured_tree
144            
145             Adds I<create_structured_tree> to L<Directory::Scratch> when you Load B<Directory::Scratch::Structured>
146             with the B<piggyback_directory_scratch> option.
147            
148             use Directory::Scratch ;
149             use Directory::Scratch::Structured qw(piggyback_directory_scratch) ;
150            
151             my $temporary_directory = Directory::Scratch->new;
152             $temporary_directory->create_structured_tree(%tree_structure) ;
153            
154             =cut
155              
156 1     1 1 3274 my ($temporary_directory, @directory_entries) = @_ ;
157              
158 1         7 Directory::Scratch::Structured::_create_structured_tree($temporary_directory, {@directory_entries}, $ROOT_DIRECTORY) ; ## no critic
159              
160 1         7 return($temporary_directory) ;
161             }
162              
163             #-------------------------------------------------------------------------------
164              
165             sub _create_structured_tree
166             {
167              
168             =head2 _create_structured_tree
169            
170             Used internally by both interfaces
171            
172             =cut
173              
174 11     11   36 my ($temporary_directory, $directory, $path) = @_ ;
175              
176 11         123 while( my ($entry_name, $contents) = each %{$directory})
  35         141  
177             {
178 25         49 for($contents)
179             {
180             'ARRAY' eq ref $_ and do
181 25 100       76 {
182 16         28 my $file = $temporary_directory->touch("$path/$entry_name", @{$contents}) ;
  16         65  
183 16         21401 last ;
184             } ;
185            
186             'HASH' eq ref $_ and do
187 9 100       37 {
188 8         41 $temporary_directory->mkdir("$path/$entry_name");
189 8         4076 _create_structured_tree($temporary_directory, $contents, "$path/$entry_name") ;
190 8         19 last ;
191             } ;
192            
193 1         33 croak "invalid element '$path/$entry_name' in tree structure\n" ;
194             }
195             }
196            
197 10         17 return(1) ;
198             }
199              
200             #-------------------------------------------------------------------------------
201              
202             sub piggyback
203             {
204              
205             =head2 piggyback
206            
207             Used internally to piggyback L<Directory::Scratch>.
208            
209             =cut
210              
211 2     2 1 475 Sub::Install::install_sub({
212                code => \&directory_scratch_create_structured_tree,
213                into => 'Directory::Scratch',
214                as => 'create_structured_tree',
215               });
216              
217 2         155 return('Directory::Scratch::create_structured_tree') ;
218             }
219              
220             #-------------------------------------------------------------------------------
221              
222             1 ;
223              
224             =head1 BUGS AND LIMITATIONS
225            
226             None so far.
227            
228             =head1 AUTHOR
229            
230             Khemir Nadim ibn Hamouda
231             CPAN ID: NKH
232             mailto:nadim@khemir.net
233            
234             =head1 LICENSE AND COPYRIGHT
235            
236             This program is free software; you can redistribute
237             it and/or modify it under the same terms as Perl itself.
238            
239             =head1 SUPPORT
240            
241             You can find documentation for this module with the perldoc command.
242            
243             perldoc Directory::Scratch::Structured
244            
245             You can also look for information at:
246            
247             =over 4
248            
249             =item * AnnoCPAN: Annotated CPAN documentation
250            
251             L<http://annocpan.org/dist/Directory-Scratch-Structured>
252            
253             =item * RT: CPAN's request tracker
254            
255             Please report any bugs or feature requests to L <bug-directory-scratch-structured@rt.cpan.org>.
256            
257             We will be notified, and then you'll automatically be notified of progress on
258             your bug as we make changes.
259            
260             =item * Search CPAN
261            
262             L<http://search.cpan.org/dist/Directory-Scratch-Structured>
263            
264             =back
265            
266             =head1 SEE ALSO
267            
268             L<Directory::Scratch>
269            
270             =cut
271