File Coverage

lib/File/Corresponding/Config/Find.pm
Criterion Covered Total %
statement 22 22 100.0
branch 2 2 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 31 31 100.0


line stmt bran cond sub pod time code
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             $File::Corresponding::Config::Find::VERSION = '0.004';
46 1     1   884 use Moose;
  1         355290  
  1         7  
47              
48              
49              
50 1     1   6580 use Data::Dumper;
  1         4956  
  1         54  
51 1     1   15 use Path::Class;
  1         1  
  1         44  
52 1     1   396 use Moose::Autobox;
  1         150658  
  1         4  
53              
54 1     1   1860 use File::HomeDir;
  1         4593  
  1         239  
55              
56              
57              
58             =head1 ATTRIBUTES
59              
60             =head2 preferred_dirs : ArrayRef[Path::Class]
61              
62             =cut
63             has preferred_dirs => (
64             is => "rw",
65             isa => "ArrayRef[Path::Class::Dir]",
66             default => sub { [] },
67             auto_deref => 1,
68             );
69              
70              
71              
72             =head1 METHODS
73              
74             =head2 user_config($config_file_name, $preferred_dirs = []) : Path::Class::File $found_file_name | undef
75              
76             Find an existing readable file called $config_file_name
77             (e.g. ".myapp") in a) $preferred_dirs, or b) the usual user
78             directories ($HOME etc).
79              
80             Return the complete file name to the config file, or undef if none was
81             found.
82              
83             =cut
84             sub user_config {
85 4     4 1 3204 my $self = shift;
86 4         6 my ($config_file_name) = @_;
87              
88 4         98 my @potential_dirs = (
89             $self->preferred_dirs,
90             File::HomeDir->my_documents,
91             File::HomeDir->my_data,
92             File::HomeDir->my_home,
93             );
94 4         233 for my $dir (@potential_dirs) {
95 9         172 my $file = file($dir, $config_file_name);
96 9 100       633 -r $file and return $file;
97             }
98              
99 2         61 return undef;
100             }
101              
102              
103              
104             1;
105              
106              
107              
108             __END__