PlConfig/DotScheme.pm

filter: Statementstmt 33.3% 30 / 90 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 44.4% 8 / 18 Podpod 0.0% 0 / 7 totaltotal 28.1% 38 / 135 CC 8 SCAR 18.6
CC
8
Cov
28%
write_key
20.0
rename_key
12.0
read_keys
6.0
CRAP 6.4
« PlConfig.pm ↑ Summary PlConfig/Tied.pm »
1 # $Id$
2 # $Source$
3 # $Author$
4 # $HeadURL$
5 # $Revision$
6 # $Date$
7 package Config::PlConfig::DotScheme;
8  
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use 5.006001;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use version; our $VERSION = qv('0.1_02');
Subroutine BEGIN: called CC=1 SCAR=0.0
13 {
14  
15 2 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 ▶ use Class::Dot qw(-new property isa_String isa_Object);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶ use Params::Util qw(_CODELIKE);
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 ▶ use English qw( -no_match_vars );
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 2 property plconfig => isa_Object('Config::PlConfig');
21 2 property dumper => isa_String('JSON');
22  
23 2 my $GLOBAL_AUTOSAVE = 0;
24  
25 my %DUMPER = (
26 0 ▶ 'YAML' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
27 0 require YAML::Syck;
28 0 return YAML::Syck::Dump(@_);
29 },
30 0 ▶ 'XML' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
31 0 require XML::Simple;
32 0 return XML::Simple::XMLout(@_);
33 },
34 0 ▶ 'JSON' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 0 require JSON::Syck;
36 0 return JSON::Syck::Dump(@_);
37 },
38 2 );
39  
40 0 ▶ sub write_key {
Subroutine write_key: not called CC=4 SCAR=30.0
Pod undocumented
41 0 my ( $self, $key, $setvalue ) = @_;
42 0 my $plconfig = $self->plconfig;
43 0 my $config = $plconfig->config;
44 0 ▶ return if !$key || !$setvalue;
Branch: 1 branch
BranchTrueFalseTotal
unless $key and $setvalue 0 0 0
Condition0%0/3
$key and $setvalue
!ll&&!rl&&r
000
MC/DC0%0/2
$key and $setvalue
$key $setvalue
Truth table0%0/3
A and B
A= $keyB= $setvalue
ABresult
0X0
100
111
45
46 0 my $statement = $self->dotscheme_to_perlvar($key);
47  
48 # Try to check if the string evals,
49 # if it does it's a valid perl statement,
50 # everything else is quoted with power-quotes (')
51 0 eval $setvalue; ## no critic
52 0 ▶ if ($EVAL_ERROR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Config::PlConfig::DotScheme::EVAL_ERROR) 0 0 0
53 0 $setvalue = qq{'$setvalue'};
54 }
55  
56 # try the statement with a temp variable first.
57 # so we're sure it doesn't fuck up something.
58 0 $self->eval_string(qq{
59 my \$tmpvar = $setvalue;
60 });
61  
62 # then try the real operation
63 0 $self->eval_string(qq{
64 $statement = $setvalue;
65 });
66  
67 0 $plconfig->save;
68  
69 0 return 1;
70 }
71  
72 0 ▶ sub read_keys {
Subroutine read_keys: not called CC=2 SCAR=17.9
Pod undocumented
73 0 my ($self, $key) = @_;
74 0 my $plconfig = $self->plconfig;
75 0 my $config = $plconfig->config;
76  
77 0 ▶ if ( defined $key ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $key) 0 0 0
78 0 print "KEY HOOPA\n";
79 0 my $statement = $self->dotscheme_to_perlvar($key);
80 0 my $value;
81 0 eval qq{ \$value = $statement }; ## no critic
82 0 print "-----\n", qq{ \$value = $statement }, "\n----\n";
83  
84 0 return $self->dump_structure( { $key => $value } );
85 }
86
87 0 return $self->dump_structure($config);
88  
89 }
90  
91 0 ▶ sub rename_key {
Subroutine rename_key: not called CC=3 SCAR=24.8
Pod undocumented
92 0 my ( $self, $domain, $key_from, $key_to ) = @_;
93 0 my $plconfig = $self->plconfig;
94 0 my $config = $plconfig->config;
95 0 ▶ return if !$key_from || !$key_to;
Branch: 1 branch
BranchTrueFalseTotal
unless $key_from and $key_to 0 0 0
Condition0%0/3
$key_from and $key_to
!ll&&!rl&&r
000
MC/DC0%0/2
$key_from and $key_to
$key_from $key_to
Truth table0%0/3
A and B
A= $key_fromB= $key_to
ABresult
0X0
100
111
96  
97 0 my $from_stmt = $self->dotscheme_to_perlvar($key_from);
98 0 my $to_stmt = $self->dotscheme_to_perlvar($key_to);
99  
100 0 $self->eval_string(
101 qq{ my \$tmp = $from_stmt; $to_stmt = \$tmp; delete $from_stmt}
102 );
103  
104 0 $plconfig->save;
105  
106 0 return 1;
107 }
108  
109 0 ▶ sub delete_key {
Subroutine delete_key: not called CC=2 SCAR=17.9
Pod undocumented
110 0 my ( $self, $domain, $key ) = @_;
111 0 my $plconfig = $self->plconfig;
112 0 my $config = $plconfig->config;
113 0 ▶ return if !$key;
Branch: 1 branch
BranchTrueFalseTotal
unless $key 0 0 0
114  
115 0 my $key_stmt = $self->dotscheme_to_perlvar($key);
116  
117 0 $self->eval_string(qq{ delete $key_stmt });
118  
119 0 $plconfig->save;
120  
121 0 return 1;
122 }
123  
124 0 ▶ sub eval_string {
Subroutine eval_string: not called CC=1 SCAR=6.9
Pod undocumented
125 0 my ($self, $perl_code) = @_;
126 0 my $plconfig = $self->plconfig;
127 0 my $config = $plconfig->config;
128 0 eval $perl_code; ## no critic
129 0 return $EVAL_ERROR;
130 }
131  
132 0 ▶ sub dump_structure {
Subroutine dump_structure: not called CC=1 SCAR=6.9
Pod undocumented
133 0 my ($self, $data_ref) = @_;
134 0 my $curdumper = $self->dumper;
135  
136 0 return $DUMPER{$curdumper}->($data_ref);
137 }
138  
139 0 ▶ sub dotscheme_to_perlvar {
Subroutine dotscheme_to_perlvar: not called CC=1 SCAR=6.9
Pod undocumented
140 0 my ( $self, $key ) = @_;
141  
142 0 my @keys = split m/\.|\-\>/xms, $key;
143 0 my $statement = q{$} . 'config->';
144 0 $statement .= join q{}, map {"{'$_'}"} @keys;
145  
146 0 return $statement;
147 }
148  
149 }
150  
151 2 1; # Magic true value required at end of module
152 __END__
« PlConfig.pm ↑ Summary PlConfig/Tied.pm »