lib/File/Corresponding/Config/Find.pm

filter: Statementstmt 100% 25 / 25 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod 100% 1 / 1 totaltotal 100% 35 / 35 CC 3 SCAR 5.1
CC
3
Cov
100%
user_config
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.7
« lib/File/Corresponding/File/Profile.pm ↑ Summary lib/File/Corresponding/Group.pm »
1  
2 =head1 NAME
3  
4 File::Corresponding::Config::Find -- Locate config files (e.g. per user)
5  
6  
7 =head1 SYNOPSIS
8  
9 use File::Corresponding::Config::Find;
10 my $my_config = ".myapp";
11  
12 #Find .myapp in any of the user's home directories
13 my $myapp_config_in_home
14 = File::Corresponding::Config::Find->new()->user_config($my_config) or die;
15  
16 #Find .myapp in the current working directory, or in the user's home directory
17 use Path::Class qw/ dir /;
18 my $myapp_config_in_cwd_or_home
19 = File::Corresponding::Config::Find->new(preferred_dirs => [ dir(".") ])->user_config($my_config)
20 or die;
21  
22  
23 =head1 DESCRIPTION
24  
25 Locate named config files in the usual places, e.g. the current dir,
26 the user's home directory (cross platform).
27  
28 First the preferred_dirs are searched, then the user's document
29 directory, data directory, and home directory.
30  
31 =head1 COMMENT
32  
33 I searched for something like this, couldn't find anything.
34  
35 So I wrote this module, and named it Config::Find. Which is a name
36 already taken by a CPAN module.
37  
38 D'oh!
39  
40 But now it's written, and it works, so it stays.
41  
42 =cut
43  
44 package File::Corresponding::Config::Find;
45 1 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
46  
47  
48  
49 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use Moose::Autobox;
Subroutine BEGIN: called CC=1 SCAR=0.0
52  
53 1 ▶use File::HomeDir;
Subroutine BEGIN: called CC=1 SCAR=0.0
54  
55  
56  
57 =head1 ATTRIBUTES
58  
59 =head2 preferred_dirs : ArrayRef[Path::Class]
60  
61 =cut
62 has preferred_dirs => (
63 is => "rw",
64 isa => "ArrayRef[Path::Class::Dir]",
65 1 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 auto_deref => 1,
67 );
68  
69  
70  
71 =head1 METHODS
72  
73 =head2 user_config($config_file_name, $preferred_dirs = []) : Path::Class::File $found_file_name | undef
74  
75 Find an existing readable file called $config_file_name
76 (e.g. ".myapp") in a) $preferred_dirs, or b) the usual user
77 directories ($HOME etc).
78  
79 Return the complete file name to the config file, or undef if none was
80 found.
81  
82 =cut
83 sub user_config {
84 4 ▶ my $self = shift;
Subroutine user_config: called CC=3 SCAR=11.0
Pod documented
85 4 my ($config_file_name) = @_;
86  
87 4 my @potential_dirs = (
88 $self->preferred_dirs,
89 File::HomeDir->my_documents,
90 File::HomeDir->my_data,
91 File::HomeDir->my_home,
92 );
93 4 for my $dir (@potential_dirs) {
94 9 my $file = file($dir, $config_file_name);
95 9 ▶ -r $file and return $file;
Branch: 1 branch
BranchTrueFalseTotal
-r $file and return $file 2 7 9
96 }
97  
98 2 return undef;
99 }
100  
101  
102  
103 1 1;
104  
105  
106  
107 __END__
« lib/File/Corresponding/File/Profile.pm ↑ Summary lib/File/Corresponding/Group.pm »