Saftpresse/Notes.pm

filter: Statementstmt 97.9% 47 / 48 Branchbran 83.3% 10 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 6 totaltotal 88.0% 66 / 75 SCAR 20.8
CC
8
Cov
95%
remove
4.1
expire
3.0
set
2.0
CRAP 8.0
« Saftpresse/Plugin/Postfix/Smtp.pm ↑ Summary Saftpresse/Plugin/Postfix.pm »
1 package Log::Saftpresse::Notes;
2  
3 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: object to hold informations across log events
6 2 our $VERSION = '1.5'; # VERSION
7  
8 has 'data' => ( is => 'rw', isa => 'HashRef', lazy => 1,
9 traits => [ 'Hash' ],
10 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
11 2 handles => {
12 'reset_data' => 'clear',
13 },
14 );
15  
16 has 'ring' => ( is => 'rw', isa => 'ArrayRef', lazy => 1,
17 traits => [ 'Array' ],
18 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
19 2 handles => {
20 'size' => 'count',
21 'reset_ring' => 'clear',
22 },
23 );
24  
25 2 has 'max_entries' => ( is => 'rw', isa => 'Int', default => 10000 );
26  
27 sub reset {
28 1 ▶ my $self = shift;
Subroutine reset: called CC=1 SCAR=0.0
Pod undocumented
29 1 $self->reset_data;
30 1 $self->reset_ring;
31 1 return;
32 }
33  
34 sub get {
35 5 ▶ my ( $self, $key ) = @_;
Subroutine get: called CC=1 SCAR=0.0
Pod undocumented
36 5 return( $self->data->{$key} );
37 }
38  
39 sub set {
40 10014 ▶ my ( $self, $key, $value ) = @_;
Subroutine set: called CC=2 SCAR=6.9
Pod undocumented
41  
42 10014 ▶ if( defined $self->data->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->data->{$key}) 1 10013 10014
43 1 $self->remove( $key );
44 }
45  
46 10014 push( @{$self->ring}, $key );
47 10014 $self->data->{$key} = $value;
48  
49 10014 $self->expire;
50  
51 10014 return;
52 }
53  
54 sub remove {
55 2 ▶ my ( $self, $key ) = @_;
Subroutine remove: called CC=4 SCAR=14.1
Pod undocumented
56  
57 2 ▶ if( ! defined $self->data->{$key} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->data->{$key}) 0 2 2
58 0 return;
59 }
60 2 delete $self->data->{$key};
61  
62 # search the array for the key and remove it
63 # iterating may be slow, but remove should be rare
64 2 ▶ for( my $i = 0 ; $i < scalar(@{$self->ring}) ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @{$self->ring;}) 10001 0 10001
65 10001 ▶ if( $self->ring->[$i] eq $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ring->[$i] eq $key) 2 9999 10001
66 2 splice(@{$self->ring}, $i, 1);
67 2 last;
68 }
69 }
70  
71 2 return;
72 }
73  
74 sub is_full {
75 4 ▶ my $self = shift;
Subroutine is_full: called CC=2 SCAR=6.9
Pod undocumented
76 4 ▶ if( $self->size >= $self->max_entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size >= $self->max_entries) 2 2 4
77 2 return 1;
78 }
79 2 return 0;
80 }
81  
82 sub expire {
83 10014 ▶ my $self = shift;
Subroutine expire: called CC=3 SCAR=11.0
Pod undocumented
84 10014 ▶ if( $self->size <= $self->max_entries ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->size <= $self->max_entries) 10005 9 10014
85 10005 return;
86 }
87 9 my $num = $self->size - $self->max_entries;
88 9 foreach my $i ( 1..$num ) {
89 9 my $key = shift @{$self->ring};
90 9 delete $self->data->{$key};
91 }
92 9 return;
93 }
94  
95 2 1;
96  
97 __END__
98  
99 =pod
100  
101 =encoding UTF-8
102  
103 =head1 NAME
104  
105 Log::Saftpresse::Notes - object to hold informations across log events
106  
107 =head1 VERSION
108  
109 version 1.5
110  
111 =head1 AUTHOR
112  
113 Markus Benning <ich@markusbenning.de>
114  
115 =head1 COPYRIGHT AND LICENSE
116  
117 This software is Copyright (c) 1998 by James S. Seymour, 2015 by Markus Benning.
118  
119 This is free software, licensed under:
120  
121 The GNU General Public License, Version 2, June 1991
122  
123 =cut
« Saftpresse/Plugin/Postfix/Smtp.pm ↑ Summary Saftpresse/Plugin/Postfix.pm »