File Coverage

blib/lib/DBIx/FileStore/ConfigFile.pm
Criterion Covered Total %
statement 18 36 50.0
branch 0 10 0.0
condition n/a
subroutine 6 9 66.6
pod 2 2 100.0
total 26 57 45.6


line stmt bran cond sub pod time code
1             package DBIx::FileStore::ConfigFile;
2 3     3   13 use strict;
  3         4  
  3         100  
3              
4 3     3   13 use List::Util qw(first);
  3         3  
  3         251  
5 3     3   1486 use File::Spec::Functions qw(catfile); # to concat dirs & filenames portably
  3         2028  
  3         206  
6 3     3   1478 use Config::File;
  3         26346  
  3         150  
7              
8 3     3   1254 use DBIx::FileStore::UtilityFunctions qw(get_user_homedir);
  3         6  
  3         173  
9              
10 3     3   1468 use fields qw( vars_hash verbose ); # you can set $obj->{verbose} for debugging
  3         3519  
  3         13  
11              
12             ######################################
13             # my $conf = new DBIx::FileStore::ConfigFile()
14             sub new {
15 0     0 1   my DBIx::FileStore::ConfigFile $self = shift;
16 0 0         unless (ref $self) {
17 0           $self = fields::new($self);
18             }
19 0           return $self;
20             }
21              
22              
23             ############################################
24             # my $hashref = $conf->read_config_file()
25             # or
26             # my $hashref = $conf->read_config_file( "/etc/fdb-alternate.conf" )
27             sub read_config_file {
28 0     0 1   my ($self, $opt_filename) = @_;
29              
30             # choose which config file we're going to use
31 0           my $filename = $opt_filename;
32 0           my $user_dotfile = catfile( get_user_homedir(), ".fdbrc" ); # ~/.fdbrc
33 0           my $etc_conffile = "/etc/fdb.conf";
34 0 0         unless( $filename ) {
35             # explicit sub{} like this for perl 5.6.2
36 0     0     $filename = first( sub { -e }, $user_dotfile, $etc_conffile );
  0            
37             }
38              
39 0 0         unless($filename) {
40 0           die ("$0: Can't find config file to open " .
41             "(tried $user_dotfile and $etc_conffile)\n");
42             }
43 0 0         print "$0: reading $filename\n" if $self->{verbose};
44              
45 0           $self->{vars_hash} = Config::File::read_config_file( $filename );
46              
47             # sanity check that there's a dbuser passed.
48 0 0         unless( $self->{vars_hash}->{dbuser} ) {
49 0           warn "$0: Can't find a 'dbuser' setting in $filename\n";
50             }
51              
52 0           return $self->{vars_hash};
53             }
54              
55             1;
56              
57             =pod
58              
59             =head1 NAME
60              
61             DBIx::FileStore::ConfigFile -- Find and read filestore conf files.
62              
63             =head1 SYNOPSIS
64              
65             my $conf = new DBIx::FileStore::ConfigFile();
66             my $hashref = $conf->read_config_file();
67              
68             # these are the fields we use, along wth dbpasswd
69             print "db: $hashref->{dbname}, user: $hashref->{dbuser}\n";
70              
71             =head1 DESCRIPTION
72              
73             Provides interface to read DBIx::FileStore configuration files.
74              
75             The read_config_file() method reads from the optionally
76             passed configuration file, the file .fdbrc in the user's
77             home directory , or /etc/fdb.conf, whichever is found first.
78              
79             =head1 METHODS
80              
81             =over 4
82              
83             =item new DBIx::FileStore::ConfigFile();
84              
85             my $conf = new DBIx::FileStore::ConfigFile();
86              
87             Returns a new DBIx::FileStore::ConfigFile object.
88              
89             =item $conf->read_config_file()
90              
91             my $conf_hash = $conf->read_config_file();
92              
93             my $conf_hash = $conf->read_config_file( $filename )
94              
95             Returns a hashref with the name/value pairs parsed from
96             the configuration file. The settings expected by
97             DBIx-Filestore are: dbname, dbuser, and dbpasswd.
98              
99             If a $filename is passed by the caller, that file is used as
100             the configuration file. Otherwise the module uses
101             the file .fdbrc in the current user's home directory,
102             or /etc/fdb.conf, whichever is found first.
103              
104             If no configuration file can be found, the method dies
105             with an error message.
106              
107             =back
108              
109             =head1 COPYRIGHT
110              
111             Copyright (c) 2010-2015 Josh Rabinowitz, All Rights Reserved.
112              
113             =head1 AUTHORS
114              
115             Josh Rabinowitz
116              
117             =cut
118