File Coverage

lib/Class/Usul/File.pm
Criterion Covered Total %
statement 46 59 77.9
branch 2 10 20.0
condition 6 26 23.0
subroutine 15 17 88.2
pod 7 7 100.0
total 76 119 63.8


line stmt bran cond sub pod time code
1             package Class::Usul::File;
2              
3 21     21   609 use namespace::autoclean;
  21         59  
  21         116  
4              
5 21     21   1307 use Class::Usul::Constants qw( EXCEPTION_CLASS TRUE );
  21         45  
  21         134  
6 21         131 use Class::Usul::Functions qw( arg_list create_token is_arrayref io
7 21     21   10049 merge_attributes throw );
  21         68  
8 21     21   29774 use Class::Usul::Types qw( ConfigProvider Locker Logger );
  21         47  
  21         137  
9 21     21   20395 use English qw( -no_match_vars );
  21         42  
  21         190  
10 21     21   16019 use File::DataClass::Schema;
  21         2048687  
  21         721  
11 21     21   165 use File::Spec::Functions qw( catfile );
  21         226  
  21         1333  
12 21     21   140 use Scalar::Util qw( blessed );
  21         76  
  21         1106  
13 21     21   149 use Unexpected::Functions qw( Unspecified );
  21         47  
  21         209  
14 21     21   4841 use Moo;
  21         58  
  21         119  
15              
16             # Public attributes
17             has 'config' => is => 'ro', isa => ConfigProvider, required => TRUE;
18              
19             has 'lock' => is => 'ro', isa => Locker, required => TRUE;
20              
21             has 'log' => is => 'ro', isa => Logger, required => TRUE;
22              
23             # Construction
24             around 'BUILDARGS' => sub {
25             my ($orig, $self, @args) = @_; my $attr = $orig->( $self, @args );
26              
27             my $builder = $attr->{builder} or return $attr;
28              
29             merge_attributes $attr, $builder, [ 'config', 'lock', 'log' ];
30              
31             return $attr;
32             };
33              
34             # Public methods
35             sub data_dump {
36 0     0 1 0 my ($self, @args) = @_; my $args = arg_list @args; my $attr = {};
  0         0  
  0         0  
37              
38             exists $args->{storage_class} and defined $args->{storage_class}
39 0 0 0     0 and $attr->{storage_class} = delete $args->{storage_class};
40              
41 0         0 return $self->dataclass_schema( $attr )->dump( $args );
42             }
43              
44             sub data_load {
45 0     0 1 0 my ($self, @args) = @_; my $args = arg_list @args; my $attr = {};
  0         0  
  0         0  
46              
47             exists $args->{storage_class} and defined $args->{storage_class}
48 0 0 0     0 and $attr->{storage_class} = delete $args->{storage_class};
49              
50             exists $args->{arrays} and defined $args->{arrays}
51 0 0 0     0 and $attr->{storage_attributes}->{force_array} = $args->{arrays};
52              
53 0 0 0     0 (is_arrayref $args->{paths} and defined $args->{paths}->[ 0 ])
54             or throw Unspecified, [ 'paths' ];
55              
56 0         0 return $self->dataclass_schema( $attr )->load( @{ $args->{paths} } );
  0         0  
57             }
58              
59             sub dataclass_schema {
60 3     3 1 878 my ($self, @args) = @_; my $attr = arg_list @args;
  3         17  
61              
62 3 100       20 if (blessed $self) { $attr->{builder} = $self }
  1         3  
63 2         5 else { $attr->{cache_class} = 'none' }
64              
65 3   50     23 $attr->{storage_class} //= 'Any';
66              
67 3         24 return File::DataClass::Schema->new( $attr );
68             }
69              
70             sub delete_tmp_files {
71 5   33 5 1 12554 return io( $_[ 1 ] // $_[ 0 ]->tempdir )->delete_tmp_files;
72             }
73              
74             sub tempdir {
75 9     9 1 6755 return $_[ 0 ]->config->tempdir;
76             }
77              
78             sub tempfile {
79 1   33 1 1 81125 return io( $_[ 1 ] // $_[ 0 ]->tempdir )->tempfile;
80             }
81              
82             sub tempname {
83 1     1 1 31006 my ($self, $dir) = @_; my $path;
  1         3  
84              
85 1   66     6 while (not $path or -f $path) {
86 1         7 my $file = sprintf '%6.6d%s', $PID, (substr create_token, 0, 4);
87              
88 1   33     9 $path = catfile( $dir // $self->tempdir, $file );
89             }
90              
91 1         145 return $path;
92             }
93              
94             1;
95              
96             __END__
97              
98             =pod
99              
100             =encoding utf-8
101              
102             =head1 Name
103              
104             Class::Usul::File - Data loading and dumping
105              
106             =head1 Synopsis
107              
108             package YourClass;
109              
110             use Class::Usul::File;
111              
112             my $file_obj = Class::Usul::File->new( builder => Class::Usul->new );
113              
114             =head1 Description
115              
116             Provides data loading and dumping methods, Also temporary file methods
117             and directories instantiated using the L<Class::Usul::Config> object
118              
119             =head1 Configuration and Environment
120              
121             Defined the following attributes;
122              
123             =over 3
124              
125             =item C<config>
126              
127             A required instance of type C<ConfigProvider>
128              
129             =item C<lock>
130              
131             A required instance of type C<Locker>
132              
133             =item C<log>
134              
135             A required instance of type C<Logger>
136              
137             =back
138              
139             =head1 Subroutines/Methods
140              
141             =head2 C<BUILDARGS>
142              
143             Extracts the required constructor attributes from the C<builder> attribute
144             if it was supplied
145              
146             =head2 C<data_dump>
147              
148             $self->dump( @args );
149              
150             Accepts either a list or a hash ref. Calls L</dataclass_schema> with
151             the I<storage_class> attribute if supplied. Calls the
152             L<dump|File::DataClass::Schema/dump> method
153              
154             =head2 C<data_load>
155              
156             $hash_ref = $self->load( @args );
157              
158             Accepts either a list or a hash ref. Calls L</dataclass_schema> with
159             the I<storage_class> and I<arrays> attributes if supplied. Calls the
160             L<load|File::DataClass::Schema/load> method
161              
162             =head2 C<dataclass_schema>
163              
164             $f_dc_schema_obj = $self->dataclass_schema( $attrs );
165              
166             Returns a L<File::DataClass::Schema> object. Object uses our
167             C<exception_class>, no caching and no locking by default. Works as a
168             class method
169              
170             =head2 C<delete_tmp_files>
171              
172             $self->delete_tmp_files( $dir );
173              
174             Delete this processes temporary files. Files are in the C<$dir> directory
175             which defaults to C<< $self->tempdir >>
176              
177             =head2 C<tempdir>
178              
179             $temporary_directory = $self->tempdir;
180              
181             Returns C<< $self->config->tempdir >> or L<File::Spec/tmpdir>
182              
183             =head2 C<tempfile>
184              
185             $tempfile_obj = $self->tempfile( $dir );
186              
187             Returns a L<File::Temp> object in the C<$dir> directory
188             which defaults to C<< $self->tempdir >>. File is automatically deleted
189             if the C<$tempfile_obj> reference goes out of scope
190              
191             =head2 C<tempname>
192              
193             $pathname = $self->tempname( $dir );
194              
195             Returns the pathname of a temporary file in the given directory which
196             defaults to C<< $self->tempdir >>. The file will be deleted by
197             L</delete_tmp_files> if it is called otherwise it will persist
198              
199             =head1 Diagnostics
200              
201             None
202              
203             =head1 Dependencies
204              
205             =over 3
206              
207             =item L<Class::Usul::Constants>
208              
209             =item L<File::DataClass::IO>
210              
211             =item L<File::Temp>
212              
213             =back
214              
215             =head1 Incompatibilities
216              
217             There are no known incompatibilities in this module
218              
219             =head1 Bugs and Limitations
220              
221             There are no known bugs in this module.
222             Please report problems to the address below.
223             Patches are welcome
224              
225             =head1 Author
226              
227             Peter Flanigan, C<< <pjfl@cpan.org> >>
228              
229             =head1 License and Copyright
230              
231             Copyright (c) 2017 Peter Flanigan. All rights reserved
232              
233             This program is free software; you can redistribute it and/or modify it
234             under the same terms as Perl itself. See L<perlartistic>
235              
236             This program is distributed in the hope that it will be useful,
237             but WITHOUT WARRANTY; without even the implied warranty of
238             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
239              
240             =cut
241              
242             # Local Variables:
243             # mode: perl
244             # tab-width: 3
245             # End: