Connector/Proxy/Config/Std.pm

filter: Statementstmt 87.8% 94 / 107 Branchbran 66.6% 24 / 36 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 100% 7 / 7 totaltotal 85.0% 142 / 167 CC 19 SCAR 13.4
CC
19
Cov
83%
get_meta
8.3
exists
4.0
get_keys
3.5
CRAP 3.8
« Connector/Proxy/JSON.pm ↑ Summary Connector/Iterator.pm »
1 # Connector::Proxy::Config::Std
2 #
3 # Proxy class for reading Config::Std configuration
4 #
5 # Written by Scott Hardin and Martin Bartosch for the OpenXPKI project 2012
6 #
7 package Connector::Proxy::Config::Std;
8  
9 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use English;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Config::Std;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 2 extends 'Connector::Proxy';
17  
18 2 ▶sub _build_config {
Subroutine _build_config: called CC=1 SCAR=0.0
19 2 my $self = shift;
20  
21 2 my $config;
22 2 read_config($self->LOCATION(), $config);
23 2 $self->_config($config);
24 }
25  
26  
27 22 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
28 22 my $self = shift;
29 22 my @path = $self->_build_path_with_prefix( shift );
30  
31 # Config::Std does not allow nested data structures, emulate that
32 # by separating last element from path and using that as key
33 # in the section defined by the remaining prefix
34 22 my $key = pop @path;
35 22 my $section = $self->_build_section_name_from_path( @path);
36  
37 22 return $self->_config()->{$section}->{$key};
38 }
39  
40 3 ▶sub _get_node {
Subroutine _get_node: called CC=1 SCAR=0.0
41  
42 3 my $self = shift;
43 3 my @path = $self->_build_path_with_prefix( shift );
44 3 my $fullpath = $self->_build_section_name_from_path( @path);
45 3 return $self->_config()->{$fullpath};
46 }
47  
48  
49 1 ▶sub get_size {
Subroutine get_size: called CC=3 SCAR=12.0
Pod documented
50  
51 1 my $self = shift;
52 1 my $node = $self->get( shift );
53  
54 1 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 1 1
55 0 return 0;
56 }
57  
58 1 ▶ if (ref $node ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "ARRAY") 0 1 1
59 0 die "requested path looks not like a list";
60 }
61  
62 1 return scalar @{$node};
63 }
64  
65  
66 1 ▶sub get_list {
Subroutine get_list: called CC=3 SCAR=11.8
Pod documented
67  
68 1 my $self = shift;
69 1 my $path = shift;
70  
71 # List is similar to scalar, the last path item is a hash key
72 # in the section of the remaining prefix
73  
74 1 my $node = $self->get( $path );
75  
76 1 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 1 1
77 0 return $self->_node_not_exists( $path );
78 }
79  
80 1 ▶ if (ref $node ne "ARRAY") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "ARRAY") 0 1 1
81 0 die "requested path looks not like a hash";
82 }
83 1 return @{$node};
84 }
85  
86  
87 1 ▶sub get_keys {
Subroutine get_keys: called CC=3 SCAR=12.6
Pod documented
88  
89 1 my $self = shift;
90 1 my $node = $self->_get_node( shift );
91  
92 1 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 1 1
93 0 return @{[]};
94 }
95  
96 1 ▶ if (ref $node ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "HASH") 0 1 1
97 0 die "requested path looks not like a hash";
98 }
99 1 return keys (%{$node});
100 }
101  
102 2 ▶sub get_hash {
Subroutine get_hash: called CC=3 SCAR=12.0
Pod documented
103  
104 2 my $self = shift;
105 2 my $path = shift;
106  
107 2 my $node = $self->_get_node( $path );
108  
109 2 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 0 2 2
110 0 return $self->_node_not_exists($path);
111 }
112  
113 2 ▶ if (ref $node ne "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $node ne "HASH") 0 2 2
114 0 die "requested path looks not like a hash";
115 }
116 2 return $node;
117 }
118  
119  
120 20 ▶sub get_meta {
Subroutine get_meta: called CC=8 SCAR=21.2
Pod documented
121  
122 20 my $self = shift;
123 20 my $origin = shift;
124  
125 20 my @path = $self->_build_path_with_prefix( $origin );
126  
127 # We dont have a real tree, so we look if there is a config entry
128 # that has the full path as key
129  
130 20 my $section = $self->_build_section_name_from_path( @path );
131  
132 # As top node iteration is not supported we report a connector
133 20 ▶ if (!$section) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($section) 1 19 20
134 1 return { 'TYPE' => 'connector'};
135 }
136  
137 # This is either a hash or undef
138 19 my $node = $self->_config()->{$section};
139 19 my $meta;
140  
141 # Array and scalar exist one level above
142 19 ▶ if (!defined $node) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $node) { } 14 5 19
elsif (ref $node eq 'HASH') { } 5 0 5
143  
144 14 my $key = pop @path;
145 14 $section = $self->_build_section_name_from_path( @path );
146 14 $node = $self->_config()->{$section}->{$key};
147  
148 14 ▶ if (!defined $node) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $node) 6 8 14
149 6 return $self->_node_not_exists( \@path );
150 }
151 8 ▶ if (ref $node eq '') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $node eq '') { } 7 1 8
elsif (ref $node eq 'SCALAR') { } 0 1 1
elsif (ref $node eq 'ARRAY') { } 1 0 1
152 7 $meta = {TYPE => "scalar", VALUE => $node };
153 } elsif (ref $node eq "SCALAR") {
154 # I guess thats not supported
155 0 $meta = {TYPE => "reference", VALUE => $$node };
156 } elsif (ref $node eq "ARRAY") {
157 1 $meta = {TYPE => "list", VALUE => $node };
158 } else {
159 0 die "Unsupported node type";
160 }
161 } elsif (ref $node eq "HASH") {
162 5 $meta = {TYPE => "hash" };
163 } else {
164 0 die "Unsupported node type";
165 }
166 13 return $meta;
167 }
168  
169 4 ▶sub exists {
Subroutine exists: called CC=4 SCAR=13.9
Pod documented
170  
171 4 my $self = shift;
172  
173 4 my @path = $self->_build_path_with_prefix( shift );
174  
175 # No path always exists
176 4 ▶ if (!@path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@path) 0 4 4
177 0 return 1;
178 }
179  
180 # Test if it is a section
181 4 my $section = $self->_build_section_name_from_path( @path );
182 4 ▶ if ($self->_config()->{$section}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_config->{$section}) 1 3 4
183 1 return 1;
184 }
185  
186 # Test if it is a node
187 3 my $key = pop @path;
188 3 $section = $self->_build_section_name_from_path( @path );
189 3 ▶ if (defined $self->_config()->{$section}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->_config->{$section}{$key}) 2 1 3
190 2 return 1;
191 }
192  
193 1 return 0;
194  
195 }
196  
197 # might be refined to use a section delimiter different from connector
198 66 ▶sub _build_section_name_from_path {
Subroutine _build_section_name_from_path: called CC=1 SCAR=0.0
199  
200 66 my $self = shift;
201 66 return join( $self->DELIMITER() , @_ );
202 }
203  
204 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
205 2 __PACKAGE__->meta->make_immutable;
206  
207 2 1;
208 __END__
« Connector/Proxy/JSON.pm ↑ Summary Connector/Iterator.pm »