blib/lib/Package/Role/ini.pm

filter: Statementstmt 68.3% 41 / 60 Branchbran 56.6% 17 / 30 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 90.0% 9 / 10 Podpod 100% 5 / 5 totaltotal 66.3% 73 / 110 CC 17 SCAR 22.7
CC
17
Cov
63%
ini_path
22.1
ini
6.0
ini_file_default
4.1
CRAP 9.7
↑ Summary
1 package Package::Role::ini;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Path::Class qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Config::IniFiles qw{};
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 2 our $VERSION = '0.07';
8  
9 =head1 NAME
10  
11 Package::Role::ini - Perl role for "ini" object the returns a Config::IniFiles object
12  
13 =head1 SYNOPSIS
14  
15 Configure INI file
16  
17 /etc/my-package.ini
18 [section]
19 entry=my_value
20  
21 Package
22  
23 package My::Package;
24 use base qw{Package::New Package::Role::ini};
25  
26 sub my_method {
27 my $self = shift;
28 my $value = $self->ini->val('section', 'entry', 'default');
29 return $value
30 }
31  
32 =head1 DESCRIPTION
33  
34 Perl role for "ini" object that returns a Config::IniFiles object against a default INI file name and location
35  
36 =head1 OBJECT ACCESSORS
37  
38 =head2 ini
39  
40 Returns a lazy loaded L<Config::IniFiles> object so that you can read settings from the INI file.
41  
42 my $ini = $object->ini; #isa Config::IniFiles
43  
44 =cut
45  
46 0 ▶sub ini {
Subroutine ini: not called CC=2 SCAR=17.9
Pod documented
47 0 my $self = shift;
48 0 ▶ unless ($self->{'ini'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ini"}) 0 0 0
49 0 my $file = $self->ini_file;
50 0 $self->{'ini'} = Config::IniFiles->new(-file=>"$file");
51 }
52 0 return $self->{'ini'};
53 }
54  
55 =head1 METHODS
56  
57 =head2 ini_file
58  
59 Sets or returns the profile INI filename
60  
61 my $file = $object->ini_file;
62 my $file = $object->ini_file("./my.ini");
63  
64 Set on construction
65  
66 my $object = My::Class->new(ini_file=>"./my.ini");
67  
68 Default is the object lower case class name replacing :: with - and adding ".ini" extension. In other words, for the package My::Package the default location on Linux would be /etc/my-package.ini.
69  
70 override in sub class
71  
72 sub ini_file {"/path/my.ini"};
73  
74 =cut
75  
76 6 ▶sub ini_file {
Subroutine ini_file: called CC=3 SCAR=11.8
Pod documented
77 6 my $self = shift;
78 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
79 0 $self->{'ini_file'} = shift;
80 0 delete($self->{'ini'}); #delete cached Config::IniFiles
81 }
82 6 ▶ $self->{'ini_file'} = Path::Class::file($self->ini_path, $self->ini_file_default) unless defined $self->{'ini_file'};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ini_file"} 3 3 6
83 #Config::IniFiles will catch the error if ini file is not valid.
84 6 return $self->{'ini_file'};
85 }
86  
87 =head2 ini_path
88  
89 Sets and returns the path for the INI file.
90  
91 my $path = $object->ini_path; #isa Str
92 my $path = $object->ini_path("../other/path"); #isa Str
93  
94 Default: C:\Windows on Windows-like systems that have Win32 installed
95 Default: /etc on systems that have /etc
96 Default: Sys::Path->sysconfdir on systems that Sys::Path installed
97 Default: . otherwise
98  
99 override in sub class
100  
101 sub ini_path {"/my/path"};
102  
103 =cut
104  
105 9 ▶sub ini_path {
Subroutine ini_path: called CC=9 SCAR=31.0
Pod documented
106 9 my $self = shift;
107 9 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 9 9
108 0 $self->{'ini_path'} = shift;
109 0 delete($self->{'ini'}); #delete cached Config::IniFiles
110 }
111 9 ▶ unless (defined $self->{'ini_path'}) { #both "" and "0" are valid paths
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ini_path"}) 3 6 9
112 3 my $etc = $self->_ini_path_etc;
113 3 ▶ if ($^O eq 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 3 3
elsif (-d $etc and -r $etc) { } 3 0 3
114 0 local $@;
115 0 eval('use Win32');
116 0 ▶ $self->{'ini_path'} = eval('Win32::GetFolderPath(Win32::CSIDL_WINDOWS)') unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
117 ▶ } elsif (-d $etc and -r $etc) {
Condition33%1/3
-d $etc and -r $etc
!ll&&!rl&&r
003
MC/DC0%0/2
-d $etc and -r $etc
-d $etc -r $etc
Truth table33%1/3
A and B
A= -d $etcB= -r $etc
ABresult
0X0
100
111
118 3 $self->{'ini_path'} = $etc;
119 } else {
120 0 local $@;
121 0 eval('use Sys::Path');
122 0 ▶ $self->{'ini_path'} = eval('Sys::Path->sysconfdir') unless $@;
Branch: 1 branch
BranchTrueFalseTotal
unless $@ 0 0 0
123 }
124 3 ▶ $self->{'ini_path'} = '.' unless defined($self->{'ini_path'}); #fallback is current directory
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ini_path"} 0 3 3
125 }
126 9 return $self->{'ini_path'};
127 }
128  
129 3 ▶sub _ini_path_etc {'/etc'};
Subroutine _ini_path_etc: called CC=1 SCAR=0.0
130  
131 =head2 ini_file_default
132  
133 Default: lc(__PACKAGE__)=~s/::/-/g
134  
135 =cut
136  
137 6 ▶sub ini_file_default {
Subroutine ini_file_default: called CC=4 SCAR=14.1
Pod documented
138 6 my $self = shift;
139 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
140 0 $self->{'ini_file_default'} = shift;
141 0 delete($self->{'ini'}); #delete cached Config::IniFiles
142 }
143 6 ▶ unless ($self->{'ini_file_default'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ini_file_default"}) 3 3 6
144 3 my $ext = $self->ini_file_default_extension;
145 3 my $file = lc(ref($self));
146 3 $file =~ s/::/-/g;
147 3 ▶ $file = $file . '.' . $ext if defined($ext);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 2 1 3
148 3 $self->{'ini_file_default'} = $file;
149 }
150 6 return $self->{'ini_file_default'};
151 }
152  
153 =head2 ini_file_default_extension
154  
155 Default: ini
156  
157 =cut
158  
159 6 ▶sub ini_file_default_extension {
Subroutine ini_file_default_extension: called CC=3 SCAR=11.6
Pod documented
160 6 my $self = shift;
161 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
162 0 $self->{'ini_file_default_extension'} = shift;
163 0 delete($self->{'ini'}); #delete cached Config::IniFiles
164 }
165 6 ▶ unless (exists $self->{'ini_file_default_extension'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ini_file_default_extension"}) 1 5 6
166 1 $self->{'ini_file_default_extension'} = 'ini';
167 }
168 6 return $self->{'ini_file_default_extension'};
169 }
170  
171 =head1 SEE ALSO
172  
173 L<Config::IniFiles>, L<Package::New>
174  
175 =head1 AUTHOR
176  
177 Michael R. Davis, mrdvt92
178  
179 =head1 COPYRIGHT AND LICENSE
180  
181 Copyright (C) 2020 by Michael R. Davis
182  
183 This library is free software; you can redistribute it and/or modify
184 it under the same terms as Perl itself, either Perl version 5.16.3 or,
185 at your option, any later version of Perl 5 you may have available.
186  
187 =cut
188  
189 2 1;
↑ Summary