File Coverage

blib/lib/Package/Role/ini.pm
Criterion Covered Total %
statement 39 58 67.2
branch 17 30 56.6
condition 1 3 33.3
subroutine 9 10 90.0
pod 5 5 100.0
total 71 106 66.9


line stmt bran cond sub pod time code
1             package Package::Role::ini;
2 1     1   71663 use strict;
  1         3  
  1         32  
3 1     1   5 use warnings;
  1         2  
  1         26  
4 1     1   471 use Path::Class qw{};
  1         43767  
  1         32  
5 1     1   780 use Config::IniFiles qw{};
  1         12193  
  1         575  
6              
7             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 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     0 1 0 my $self = shift;
48 0 0       0 unless ($self->{'ini'}) {
49 0         0 my $file = $self->ini_file;
50 0         0 $self->{'ini'} = Config::IniFiles->new(-file=>"$file");
51             }
52 0         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 6     6 1 14 my $self = shift;
78 6 50       17 if (@_) {
79 0         0 $self->{'ini_file'} = shift;
80 0         0 delete($self->{'ini'}); #delete cached Config::IniFiles
81             }
82 6 100       18 $self->{'ini_file'} = Path::Class::file($self->ini_path, $self->ini_file_default) unless defined $self->{'ini_file'};
83             #Config::IniFiles will catch the error if ini file is not valid.
84 6         455 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 9     9 1 17 my $self = shift;
107 9 50       27 if (@_) {
108 0         0 $self->{'ini_path'} = shift;
109 0         0 delete($self->{'ini'}); #delete cached Config::IniFiles
110             }
111 9 100       21 unless (defined $self->{'ini_path'}) { #both "" and "0" are valid paths
112 3         13 my $etc = $self->_ini_path_etc;
113 3 50 33     100 if ($^O eq 'MSWin32') {
    50          
114 0         0 local $@;
115 0         0 eval('use Win32');
116 0 0       0 $self->{'ini_path'} = eval('Win32::GetFolderPath(Win32::CSIDL_WINDOWS)') unless $@;
117             } elsif (-d $etc and -r $etc) {
118 3         13 $self->{'ini_path'} = $etc;
119             } else {
120 0         0 local $@;
121 0         0 eval('use Sys::Path');
122 0 0       0 $self->{'ini_path'} = eval('Sys::Path->sysconfdir') unless $@;
123             }
124 3 50       11 $self->{'ini_path'} = '.' unless defined($self->{'ini_path'}); #fallback is current directory
125             }
126 9         47 return $self->{'ini_path'};
127             }
128              
129 3     3   6 sub _ini_path_etc {'/etc'};
130              
131             =head2 ini_file_default
132              
133             Default: lc(__PACKAGE__)=~s/::/-/g
134              
135             =cut
136              
137             sub ini_file_default {
138 6     6 1 13 my $self = shift;
139 6 50       15 if (@_) {
140 0         0 $self->{'ini_file_default'} = shift;
141 0         0 delete($self->{'ini'}); #delete cached Config::IniFiles
142             }
143 6 100       15 unless ($self->{'ini_file_default'}) {
144 3         7 my $ext = $self->ini_file_default_extension;
145 3         10 my $file = lc(ref($self));
146 3         16 $file =~ s/::/-/g;
147 3 100       12 $file = $file . '.' . $ext if defined($ext);
148 3         7 $self->{'ini_file_default'} = $file;
149             }
150 6         22 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 6     6 1 3815 my $self = shift;
161 6 50       17 if (@_) {
162 0         0 $self->{'ini_file_default_extension'} = shift;
163 0         0 delete($self->{'ini'}); #delete cached Config::IniFiles
164             }
165 6 100       16 unless (exists $self->{'ini_file_default_extension'}) {
166 1         3 $self->{'ini_file_default_extension'} = 'ini';
167             }
168 6         19 return $self->{'ini_file_default_extension'};
169             }
170              
171             =head1 SEE ALSO
172              
173             L, L
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             1;