blib/lib/Package/Role/ini.pm

filter: Statementstmt 23.3% 14 / 60 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 40.0% 4 / 10 Podpod 100% 5 / 5 totaltotal 20.9% 23 / 110 CC 17 SCAR 36.3
CC
17
Cov
15%
ini_path
90.0
ini_file_default
20.0
ini_file
12.0
CRAP 37.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.02';
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 sub ini {
47 0 ▶ my $self = shift;
Subroutine ini: not called CC=2 SCAR=17.9
Pod documented
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 sub ini_file {
77 0 ▶ my $self = shift;
Subroutine ini_file: not called CC=3 SCAR=24.8
Pod documented
78 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
79 0 $self->{'ini_file'} = shift;
80 0 delete($self->{'ini'}); #delete cached Config::IniFiles
81 }
82 0 ▶ $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"} 0 0 0
83 #Config::IniFiles will catch the error if ini file is not valid.
84 0 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 sub ini_path {
106 0 ▶ my $self = shift;
Subroutine ini_path: not called CC=9 SCAR=45.0
Pod documented
107 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
108 0 $self->{'ini_path'} = shift;
109 0 delete($self->{'ini'}); #delete cached Config::IniFiles
110 }
111 0 ▶ unless (ref($self->{'ini_path'}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"ini_path"} eq "ARRAY") 0 0 0
112 0 my $etc = $self->_ini_path_etc;
113 0 ▶ if ($^O eq 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O eq 'MSWin32') { } 0 0 0
elsif (-d $etc and -r $etc) { } 0 0 0
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) {
Condition0%0/3
-d $etc and -r $etc
!ll&&!rl&&r
000
MC/DC0%0/2
-d $etc and -r $etc
-d $etc -r $etc
Truth table0%0/3
A and B
A= -d $etcB= -r $etc
ABresult
0X0
100
111
118 0 $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 0 ▶ $self->{'ini_path'} = '.' unless defined($self->{'ini_path'}); #fallback is current directory
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"ini_path"} 0 0 0
125 }
126 0 return $self->{'ini_path'};
127 }
128  
129 0 ▶sub _ini_path_etc {'/etc'};
Subroutine _ini_path_etc: not called CC=1 SCAR=6.9
130  
131 =head2 ini_file_default
132  
133 Default: lc(__PACKAGE__)=~s/::/-/g
134  
135 =cut
136  
137 sub ini_file_default {
138 0 ▶ my $self = shift;
Subroutine ini_file_default: not called CC=4 SCAR=30.0
Pod documented
139 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
140 0 $self->{'ini_file_default'} = shift;
141 0 delete($self->{'ini'}); #delete cached Config::IniFiles
142 }
143 0 ▶ unless ($self->{'ini_file_default'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"ini_file_default"}) 0 0 0
144 0 my $ext = $self->ini_file_default_extension;
145 0 my $file = lc(ref($self));
146 0 $file =~ s/::/-/g;
147 0 ▶ $file = $file . '.' . $ext if defined($ext);
Branch: 1 branch
BranchTrueFalseTotal
if defined $ext 0 0 0
148 0 $self->{'ini_file_default'} = $file;
149 }
150 0 return $self->{'ini_file_default'};
151 }
152  
153 =head2 ini_file_default_extension
154  
155 Default: ini
156  
157 =cut
158  
159 sub ini_file_default_extension {
160 0 ▶ my $self = shift;
Subroutine ini_file_default_extension: not called CC=3 SCAR=24.8
Pod documented
161 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
162 0 $self->{'ini_file_default_extension'} = shift;
163 0 delete($self->{'ini'}); #delete cached Config::IniFiles
164 }
165 0 ▶ unless (exists $self->{'ini_file_default_extension'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"ini_file_default_extension"}) 0 0 0
166 0 $self->{'ini_file_default_extension'} = 'ini';
167 }
168 0 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