count stocks instead of deprecated individuals
[cxgn-corelibs.git] / lib / Chado / Config.pm
blob551b05a53ba2850d1348a06c864d4eeb1455b06b
1 package Chado::Config;
3 =head1 NAME
5 Chado::Config
7 =head1 SYNOPSIS
9 my $config = Chado::Config->new;
10 my $db_name = $config->{'database'}{'db_name'};
12 =head1 DESCRIPTION
14 This module is a simple interface to the Chado installation
15 configuration information.
17 =head1 METHODS
19 =cut
21 use strict;
22 use Carp;
23 use FindBin '$Bin';
24 use File::Spec::Functions;
25 use XML::Simple;
27 use constant DEFAULT_FILENAME => catfile( $Bin, 'load', 'etc', 'load.conf' );
29 # ---------------------------------------------------------
30 =pod
32 =head2 new
34 Instantiates a new object. Takes an optional (but preferred)
35 argument of the configuration file's path. The filename should be
36 indicated as a key/value pair, but, if only one argument is passed
37 in, it is assumed to be the filename.
39 my $conf = Chado::Config->new;
40 my $conf = Chado::Config->new( <filename> );
41 my $conf = Chado::Config->new( filename => <filename> );
43 =cut
45 sub new {
46 my $class = shift;
47 my $args = defined $_[0] && UNIVERSAL::isa( $_[0], 'HASH' ) ? shift
48 : scalar @_ == 1 ? { filename => shift }
49 : { @_ };
50 my $self = bless {}, $class;
52 if ( $args->{'filename'} ) {
53 $self->filename( $args->{'filename'} );
56 return $self;
59 # ---------------------------------------------------------
60 =pod
62 =head2 filename
64 Gets/sets the location of the configuration file.
66 my $file = $conf->filename( <filename> );
68 =cut
70 sub filename {
71 my ( $self, $arg ) = @_;
73 if ( ! defined $self->{'filename'} && ! $arg ) {
74 $arg = DEFAULT_FILENAME if -e DEFAULT_FILENAME;
77 if ( $arg ) {
78 if ( -e $arg && -r _ ) {
79 $self->{'filename'} = $arg;
81 else {
82 croak("The file '$arg' does not exist or is not readable");
86 return $self->{'filename'};
89 # ---------------------------------------------------------
90 =pod
92 =head2 config
94 Returns the configuration information as parsed by XML::Simple.
96 my $options = $conf->config;
98 =cut
100 sub config {
101 my $self = shift;
102 unless ( defined $self->{'config'} ) {
103 my $file = $self->filename;
104 $self->{'config'} = XMLin(
105 $file,
106 # ForceArray => [ qw( template token path file) ],
107 # KeyAttr => [ qw( token name file) ],
108 ContentKey => '-value',
111 return $self->{'config'};
114 # ---------------------------------------------------------
115 =pod
117 =head1 SEE ALSO
119 XML::Simple.
121 =head1 AUTHOR
123 Ken Y. Clark E<lt>kclark@cshl.orgE<gt>.
125 =cut