Basis/Config.pm

filter: Statementstmt 88.4% 107 / 121 Branchbran 63.8% 23 / 36 Conditioncond 33.3% 3 / 9 MC/DCmcdc 16.6% 1 / 6 Subroutinesub 85.1% 23 / 27 Podpod 80.0% 4 / 5 totaltotal 78.9% 161 / 204 CC 22 SCAR 15.9
CC
22
Cov
80%
BUILD
13.4
store
7.6
__ANON__
5.0
CRAP 4.9
« Basis.pm ↑ Summary
1 # ABSTRACT: Manage config YAML files in a simple manner
2  
3  
4  
5 package App::Basis::Config;
6 1 $App::Basis::Config::VERSION = '1.2';
7 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use YAML::XS qw( Load Dump);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Path::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use App::Basis;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17  
18 # ----------------------------------------------------------------------------
19 # the raw config data hash
20 has raw => (
21 is => 'ro',
22 lazy => 1,
23  
24 # builder => '_load',
25 init_arg => undef, # dont allow setting in constructor
26 1 ▶ default => sub { {} },
Subroutine __ANON__: called CC=1 SCAR=0.0
27 1 writer => '_set_raw'
28 );
29  
30 1 has filename => (
31 is => 'ro',
32 required => 0,
33 writer => '_set_filename'
34 );
35  
36 has nostore => (
37 is => 'ro',
38 2 ▶ default => sub {0}
Subroutine __ANON__: called CC=1 SCAR=0.0
39 1 );
40  
41 has die_on_error => (
42 is => 'ro',
43 2 ▶ default => sub {0}
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 );
45  
46  
47 has has_data => (
48 is => 'ro',
49 3 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 init_arg => undef, # dont allow setting in constructor
51 writer => '_set_has_data'
52 );
53  
54  
55 has changed => (
56 is => 'rw',
57 3 ▶ default => sub {0},
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 init_arg => undef, # dont allow setting in constructor
59 # writer => '_set_changed'
60 );
61  
62  
63 has error => (
64 is => 'ro',
65 3 ▶ default => sub {undef},
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 init_arg => undef, # dont allow setting in constructor
67 writer => '_set_error'
68 );
69  
70 # ----------------------------------------------------------------------------
71  
72  
73 3 ▶sub BUILD {
Subroutine BUILD: called CC=9 SCAR=25.9
Pod undocumented
74 3 my $self = shift;
75  
76 3 $self->_set_error(undef);
77  
78 # make sure that the we expand home
79 3 my $fname = fix_filename( $self->filename );
80  
81 3 ▶ if ( !$fname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fname) 0 3 3
82 0 ▶ $fname = $ENV{APP_BASIS_CFG} || fix_filename( "~/." . get_program() . ".cfg" );
Condition0%0/3
$ENV{'APP_BASIS_CFG'} || fix_filename('~/.' . get_program() . '.cfg')
l!l&&r!l&&!r
000
MC/DC0%0/2
$ENV{'APP_BASIS_CFG'} || fix_filename('~/.' . get_program() . '.cfg')
$ENV{'APP_BASIS_CFG'} fix_filename('~/.' . get_program() . '.cfg')
Truth table0%0/3
A || B
A= $ENV{'APP_BASIS_CFG'}B= fix_filename('~/.' . get_program() . '.cfg')
ABresult
1X1
011
000
83 }
84 3 ▶ if ( $fname && -f $fname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fname and -f $fname) { } 2 1 3
Condition66%2/3
$fname and -f $fname
!ll&&!rl&&r
012
MC/DC50%1/2
$fname and -f $fname
$fname -f $fname
Truth table66%2/3
A and B
A= $fnameB= -f $fname
ABresult
0X0
100
111
85 2 $self->_set_filename($fname);
86  
87 2 my $config;
88 2 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
89 2 $config = Load( path($fname)->slurp_utf8 );
90 }
91 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
92 0 $self->_set_error(
93 "Could not read/processs config file $fname. $_");
94 2 };
95  
96 # if there was a file to read from and we had an issue then we should
97 # report it back to the caller somehow and make sure its seen.
98 2 ▶ if ( $self->error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->error) 0 2 2
99 0 ▶ die $self->error if ( $self->die_on_error );
Branch: 1 branch
BranchTrueFalseTotal
if $self->die_on_error 0 0 0
100 0 warn $self->error;
101 }
102  
103 # if we loaded some config
104 2 ▶ if ( keys %$config ) {
Branch: 1 branch
BranchTrueFalseTotal
if (keys %$config) 2 0 2
105 2 $self->_set_has_data(1);
106 2 $self->_set_raw($config);
107 }
108 }
109 else {
110 1 $self->_set_error("could not establish a config filename");
111 1 ▶ die $self->error if ( $self->die_on_error );
Branch: 1 branch
BranchTrueFalseTotal
if $self->die_on_error 0 1 1
112 }
113 }
114  
115 # ----------------------------------------------------------------------------
116  
117  
118 3 ▶sub store {
Subroutine store: called CC=7 SCAR=20.2
Pod documented
119 3 my $self = shift;
120 3 my $filename = shift;
121 3 my $need_save = 0;
122 3 my $status = 0;
123  
124 3 local $YAML::Indent = 4;
125  
126 3 $self->_set_error(undef);
127 3 ▶ if ( !$filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $filename) { } 2 1 3
128 2 $filename = $self->filename;
129 2 ▶ $need_save = 1 if ( $self->changed );
Branch: 1 branch
BranchTrueFalseTotal
if $self->changed 2 0 2
130 }
131 else {
132 1 $need_save = 1;
133 }
134  
135 # only save if we need to
136 3 ▶ if ($need_save) {
Branch: 1 branch
BranchTrueFalseTotal
if ($need_save) 3 0 3
137 3 ▶ if ( $self->nostore ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->nostore) 1 2 3
138 1 warn "Attempt to save config file "
139 . $self->filename
140 . " when nostore has been used";
141 1 return 0;
142 }
143  
144 # do the save
145 2 my $cfg = $self->raw;
146 2 ▶ try {
Subroutine __ANON__: called CC=2 SCAR=8.1
147 # do we need to create the directory to hold the file
148 2 ▶ if ( !-d path($filename)->dirname ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-d path($filename)->dirname) 0 2 2
149 0 path($filename)->dirname->mkpath;
150 }
151 2 path($filename)->spew_utf8( Dump($cfg) );
152 }
153 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
154 0 $self->_set_error(
155 "Could not save config file " . $self->filename() );
156 0 $status = 0;
157 2 };
158 2 ▶ die $self->error if ( $self->error && $self->die_on_error );
Branch: 1 branch
BranchTrueFalseTotal
if $self->error and $self->die_on_error 0 2 2
Condition33%1/3
$self->error and $self->die_on_error
!ll&&!rl&&r
200
MC/DC0%0/2
$self->error and $self->die_on_error
$self->error $self->die_on_error
Truth table33%1/3
A and B
A= $self->errorB= $self->die_on_error
ABresult
0X0
100
111
159 2 $self->changed(0);
160 2 $status = 1;
161 }
162  
163 2 return $status;
164 }
165  
166 # ----------------------------------------------------------------------------
167 # return a ref to a item in the config or undef
168 # if $value is true then a path will be established and the value stored as the
169 # final node
170  
171 15 ▶sub _split_path {
Subroutine _split_path: called CC=2 SCAR=6.9
172 15 my $self = shift;
173 15 my ( $path, $value ) = @_;
174 15 my $done = 0;
175 15 my $path_separators = '/:\.';
176  
177 # remove any leading/trailing path separators
178 15 $path =~ s|^[$path_separators]?(.*)[$path_separators]?$|$1|;
179  
180 15 my $ref = $self->raw;
181 15 my @items = split( /[$path_separators]/, $path );
182 15 ▶ try {
Subroutine __ANON__: called CC=5 SCAR=16.1
183 15 ▶ for ( my $i = 0; $i < scalar(@items); $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @items) 30 15 45
184 30 my $item = $items[$i];
185 30 ▶ if ( $ref->{$item} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ref->{$item}) { } 20 10 30
186 20 $ref = $ref->{$item};
187 20 $done = 1;
188 }
189 else {
190 10 ▶ if ($value) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value) { } 10 0 10
191  
192 # is this the last thing?
193 10 ▶ if ( ( $i + 1 ) == scalar(@items) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i + 1 == scalar @items) { } 5 5 10
194  
195 # save the value in the last node
196 5 $ref->{$item} = $value;
197 }
198 else {
199 5 $ref->{$item} = {};
200 }
201 10 $ref = $ref->{$item};
202 10 $done = 1;
203 }
204 else {
205  
206 # missed item
207 0 $done = 0;
208 }
209 }
210 }
211 }
212 15 ▶ catch {};
Subroutine __ANON__: not called CC=1 SCAR=0.0
213  
214 15 ▶ return $done ? $ref : undef;
Branch: 1 branch
BranchTrueFalseTotal
$done ? : 15 0 15
215 }
216  
217 # ----------------------------------------------------------------------------
218  
219  
220 10 ▶sub get {
Subroutine get: called CC=1 SCAR=0.0
Pod documented
221 10 my $self = shift;
222 10 my $path = shift;
223  
224 10 $self->_set_error(undef);
225  
226 10 my $ref = $self->_split_path($path);
227  
228 10 return $ref;
229 }
230  
231 # ----------------------------------------------------------------------------
232  
233  
234 5 ▶sub set {
Subroutine set: called CC=2 SCAR=7.0
Pod documented
235 5 my $self = shift;
236 5 my ( $path, $value ) = @_;
237  
238 5 $self->_set_error(undef);
239  
240 # create the path to the item
241 5 my $ref = $self->_split_path( $path, $value );
242  
243 # if we loaded some config
244 5 ▶ $self->_set_has_data(1) if ( keys %{ $self->raw() } );
Branch: 1 branch
BranchTrueFalseTotal
if keys %{$self->raw;} 5 0 5
245  
246 # something has changed so we may need to save it later
247 5 $self->changed(1);
248 }
249  
250 # ----------------------------------------------------------------------------
251  
252  
253 0 ▶sub clear {
Subroutine clear: not called CC=1 SCAR=6.9
Pod documented
254 0 my $self = shift;
255 0 my ( $path, $value ) = @_;
256  
257 0 $self->_set_error(undef);
258  
259 0 $self->raw = {};
260 0 $self->_set_has_data(0);
261  
262 # something has changed so we may need to save it later
263 0 $self->changed(1);
264 }
265  
266 # ----------------------------------------------------------------------------
267 # make sure we do any cleanup required
268  
269 1 ▶END {
Subroutine END: called CC=1 SCAR=0.0
270 }
271  
272  
273 # ----------------------------------------------------------------------------
274  
275 1 1;
276  
277 __END__
« Basis.pm ↑ Summary