blib/lib/QWizard/Storage/File.pm

filter: Statementstmt 96.0% 48 / 50 Branchbran 66.6% 8 / 12 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 100% 9 / 9 Podpod 0.0% 0 / 7 totaltotal 79.5% 66 / 83 CC 9 SCAR 12.3
CC
9
Cov
88%
load_data
5.1
save_parms
5.0
xlat_data_fw
1.0
CRAP 3.4
« blib/lib/QWizard/Storage/SQL.pm ↑ Summary blib/lib/QWizard/Storage/Base.pm »
1 package QWizard::Storage::File;
2  
3 2 ▶use QWizard::Storage::Memory;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 our @ISA = qw(QWizard::Storage::Memory);
7  
8 2 our $VERSION = '3.15';
9  
10 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
11 2 my $class = shift;
12 2 my %args = @_;
13 2 bless \%args, $class;
14 }
15  
16 7 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod undocumented
17 7 my ($self, $it, $val) = @_;
18 7 $self->SUPER::set($it, $val);
19 7 $self->save_parms();
20 }
21  
22 2 ▶sub set_all {
Subroutine set_all: called CC=1 SCAR=0.0
Pod undocumented
23 2 my ($self, $vals) = @_;
24 2 $self->SUPER::set_all($vals);
25 2 $self->save_parms();
26 }
27  
28 9 ▶sub save_parms {
Subroutine save_parms: called CC=5 SCAR=16.2
Pod undocumented
29 9 my $self = shift;
30 9 ▶ return if (!$self);
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 9 9
31 9 ▶ return if ($self->{'dontsave'});
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dontsave"} 2 7 9
32 7 my $vars = $self->get_all();
33 7 ▶ if ($self->{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"}) 7 0 7
34 7 open(OPFILE, ">$self->{'file'}");
35 7 foreach my $var (keys(%$vars)) {
36 10 print OPFILE xlat_data_fw($var),"\n";
37 10 print OPFILE xlat_data_fw($vars->{$var}),"\n";
38 }
39 7 close(OPFILE);
40 }
41 }
42  
43 1 ▶sub load_data {
Subroutine load_data: called CC=5 SCAR=16.4
Pod undocumented
44 1 my $self = shift;
45 1 ▶ return if (!$self);
Branch: 1 branch
BranchTrueFalseTotal
unless $self 0 1 1
46 1 $self->reset();
47 1 $self->{'dontsave'} = 1;
48 1 ▶ if ($self->{'file'} && -f $self->{'file'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"file"} and -f $self->{"file"}) 1 0 1
Condition33%1/3
$self->{"file"} and -f $self->{"file"}
!ll&&!rl&&r
001
MC/DC0%0/2
$self->{"file"} and -f $self->{"file"}
$self->{"file"} -f $self->{"file"}
Truth table33%1/3
A and B
A= $self->{"file"}B= -f $self->{"file"}
ABresult
0X0
100
111
49 1 open(IPFILE, "$self->{'file'}");
50 1 ▶ while (<IPFILE>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline IPFILE)) 2 1 3
51 2 chomp();
52 2 my $key = xlat_data_rv($_);
53 2 $_ = <IPFILE>;
54 2 chomp();
55 2 my $val = xlat_data_rv($_);
56 2 $self->set($key, $val);
57 }
58 }
59 1 $self->{'dontsave'} = 0;
60 }
61  
62 20 ▶sub xlat_data_fw {
Subroutine xlat_data_fw: called CC=1 SCAR=0.2
Pod undocumented
63 20 my $data = shift;
64 0 $data =~ s/([^a-zA-Z0-9])/'%' . ord($1) . ";"/eg;
65 20 return $data;
66 }
67  
68 4 ▶sub xlat_data_rv {
Subroutine xlat_data_rv: called CC=1 SCAR=0.2
Pod undocumented
69 4 my $data = shift;
70 0 $data =~ s/\%([0-9]+);/chr($1)/eg;
71 4 return $data;
72 }
73  
74 2 1;
75  
76 =pod
77  
78 =head1 NAME
79  
80 QWizard::Storage::File - Stores data in a file
81  
82 =head1 SYNOPSIS
83  
84 my $st = new QWizard::Storage::File(file => '/path/to/some/file');
85 $st->set('var', 'value');
86 $st->get('var');
87  
88 # optionally bootstrap from an existing file:
89 $st->load_data();
90  
91 =head1 DESCRIPTION
92  
93 Stores data passed to it in a file (and in memory for faster lookups).
94  
95 Generally speaking this should *not* be used except when being copied
96 to from another faster storage mechanism (like a
97 QWizard::Storage::Memory object).
98  
99 This module is actually a child class of QWizard::Storage::Memory.
100  
101 =head1 AUTHOR
102  
103 Wes Hardaker, hardaker@users.sourceforge.net
104  
105 =head1 SEE ALSO
106  
107 perl(1)
108  
109 Net-Policy: http://net-policy.sourceforge.net/
110  
111 =cut
« blib/lib/QWizard/Storage/SQL.pm ↑ Summary blib/lib/QWizard/Storage/Base.pm »