blib/lib/File/CounterSS.pm

filter: Statementstmt 83.3% 10 / 12 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod n/a totaltotal 87.5% 14 / 16 CC 1 SCAR 0.7
CC
1
Cov
83%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package File::CounterSS;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use File::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 0 ▶use File::Storage::Stat;
Subroutine BEGIN: called CC=1 SCAR=2.6
7  
8 require Exporter;
9  
10 our @ISA = qw(Exporter);
11 our $VERSION = '0.01';
12  
13  
14 sub new {
15 my $class = shift;
16 my $options = shift;
17  
18 croak('Id is not set.')
19 unless $options->{Id};
20  
21 croak('Format error of Id.')
22 unless $options->{Id} =~ /^[a-zA-Z0-9\-\_\/]+/;
23  
24 my $filepath = $options->{DirPath} . '/' . $options->{Id};
25 my $dirpath = $filepath;
26 $dirpath =~ s|/[^/]+$||;
27  
28 mkpath($dirpath, 0, 0777)
29 unless -d $dirpath;
30  
31 unless (-e $filepath) {
32 open(C, ">$filepath");
33 close(C);
34 chmod(0666, $filepath);
35 utime(0, 0, $filepath);
36 }
37  
38 my $fss = File::Storage::Stat->new({FilePath => $filepath});
39  
40 my $range = lc($options->{Range});
41 $range = 'day'
42 unless ($range =~ /^(hour|day|week|mon|year)$/);
43  
44 my $type = lc($options->{Type});
45 $type = 'total'
46 unless ($type =~ /^(total|last)$/);
47  
48 my $self = {
49 filepath => $filepath,
50 dirpath => $dirpath,
51  
52 fss => $fss,
53  
54 range => $range,
55 type => $type,
56 };
57  
58  
59 return bless $self, $class;
60 }
61  
62 sub count {
63 my $self = shift;
64  
65 my($atime, $mtime, $ctime) = $self->{fss}->get;
66  
67 my $last_atime = $atime;
68 my $clear = 0;
69 if ($self->{range} eq 'week') {
70 $clear = 1
71 unless (localtime($ctime))[6] eq 6 && (localtime(time))[6] eq 0;
72 } else {
73 my $f;
74 if ($self->{range} eq 'hour') {
75 $f = 2;
76 } elsif ($self->{range} eq 'day') {
77 $f = 3;
78 } elsif ($self->{range} eq 'mon') {
79 $f = 4;
80 } elsif ($self->{range} eq 'year') {
81 $f = 5;
82 }
83 $clear = 1
84 unless (localtime($ctime))[$f] eq (localtime(time))[$f];
85 }
86  
87 if ($clear) {
88 $atime = 0;
89 $mtime = $last_atime - 1
90 if $self->{type} eq 'last';
91 }
92 ++$atime; ++$mtime;
93 $self->{fss}->set($atime, $mtime);
94  
95 return ($atime, $mtime);
96 }
97  
98 1;
99 __END__
↑ Summary