File Coverage

blib/lib/Egg/Model/FsaveDate/Base.pm
Criterion Covered Total %
statement 12 12 100.0
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 16 16 100.0


line stmt bran cond sub pod time code
1             package Egg::Model::FsaveDate::Base;
2             #
3             # Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
4             #
5             # $Id: Base.pm 283 2008-02-27 05:27:43Z lushe $
6             #
7 2     2   11 use strict;
  2         3  
  2         69  
8 2     2   10 use warnings;
  2         4  
  2         52  
9 2     2   10 use Carp qw/ croak /;
  2         3  
  2         156  
10 2     2   11 use base qw/ Egg::Base /;
  2         3  
  2         3545  
11             use File::Path;
12             use FileHandle;
13              
14             our $VERSION= '0.01';
15              
16             sub save {
17             my $self= shift;
18             my $body= shift || croak q{ I want save data. };
19             my $c= $self->config || $self->config_to(qw/ Model FsaveDate /);
20             my $base= shift ||
21             do { $c->{base_path} ||= $self->e->path_to(qw{ etc FsaveDate }) };
22             my $path= "$base/". $self->create_dir_name( time );
23             unless ( -e $path ) {
24             my $count= $c->{amount_save} || 90;
25             for my $dir (sort{$b cmp $a}(<$base/*>)) { ## no critic.
26             next if --$count> 0;
27             rmtree($dir);
28             }
29             mkpath($path, 0, 0755); ## no critic.
30             }
31             my $ext= $c->{extention} || 'txt'; $ext=~s{^\.+} [];
32             my $fname= $self->create_file_name(\$body);
33             my $output_path= "${path}/${fname}.${ext}";
34             my $fh= FileHandle->new("> $output_path")
35             || die qq{ save error (${path}/${fname}.${ext}): $! };
36             print $fh $self->create_body(\$body);
37             $fh->close;
38             $output_path;
39             }
40             sub create_dir_name {
41             my $self= shift;
42             my @t= localtime( shift || time );
43             sprintf("%02d%02d", ($t[5]+ 1900), ++$t[4], $t[3]);
44             }
45             sub create_file_name {
46             my($self, $body)= @_;
47             require Digest::SHA1;
48             Digest::SHA1::sha1_hex($$body);
49             }
50             sub create_body {
51             my($self, $body)= @_;
52             return $$body;
53             }
54              
55             1;
56              
57             __END__
58              
59             =head1 NAME
60              
61             Egg::Model::FsaveDate::Base - Base class for model 'FsaveDate'.
62              
63             =head1 DESCRIPTION
64              
65             This module is succeeded to from L<Egg::Model::FsaveDate> in case of not being
66             from the module if the controller module of L<Egg::Model::FsaveDate> is prepared.
67              
68             =head1 METHODS
69              
70             In addition, L<Egg::Base> has been succeeded to.
71              
72             =head2 save ([SAVE_TEXT], [BASE_PATH])
73              
74             The directory of the date is made under the control of BASE_PATH, and SAVE_TEXT
75             is preserved as a file in that.
76              
77             When BASE_PATH is omitted, 'base_path' of the configuration is used.
78              
79             After the file is output, the passing is returned.
80              
81             my $output_path= $e->model('fsavedate')->save($text);
82              
83             The exception is generated when failing in the file output.
84              
85             =head2 create_dir_name ([TIME_VALUE])
86              
87             The date is returned from TIME_VALUE as a receipt directory name.
88              
89             =head2 create_file_name ([SAVE_TEXT])
90              
91             The HEX value generated with L<Digest::SHA1> by SAVE_TEXT is returned
92             as a file name.
93              
94             =head2 create_body ([SAVE_TEXT])
95              
96             It is a method for the processing of the preservation data and the return.
97             Data is only returned as it is usually.
98             Please do override from the controller when processing it.
99              
100             =head1 SEE ALSO
101              
102             L<Egg::Release>,
103             L<Egg::Model::FsaveDate>,
104             L<Egg::Helper::Model::FsaveDate>,
105             L<File::Path>,
106             L<FileHandle>,
107              
108             =head1 AUTHOR
109              
110             Masatoshi Mizuno E<lt>lusheE<64>cpan.orgE<gt>
111              
112             =head1 COPYRIGHT AND LICENSE
113              
114             Copyright (C) 2008 Bee Flag, Corp. E<lt>L<http://egg.bomcity.com/>E<gt>, All Rights Reserved.
115              
116             This library is free software; you can redistribute it and/or modify
117             it under the same terms as Perl itself, either Perl version 5.8.6 or,
118             at your option, any later version of Perl 5 you may have available.
119              
120             =cut
121