IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 20, 2009, 11:15:20 AM (17 years ago)
Author:
jhoblitt
Message:

merge neb_distrib_20081210

Location:
trunk/Nebulous-Server
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Nebulous-Server

  • trunk/Nebulous-Server/lib/Nebulous/Server/Config.pm

    r20990 r23932  
    88use warnings FATAL => qw( all );
    99
    10 our $VERSION = 0.02;
     10our $VERSION = 0.03;
    1111
    1212use base qw( Class::Accessor::Fast );
    1313
    1414use Log::Log4perl qw( :levels );
    15 use Params::Validate qw( validate SCALAR );
     15use Params::Validate qw( validate validate_pos SCALAR );
    1616
    1717our %LEVELS = (
     
    2525);
    2626
    27 my $new_validate = {
     27my $db_validate = {
     28    dbindex       => {
     29        type => SCALAR,
     30        regex => qr/^\d+$/,
     31    },
    2832    dsn         => { type => SCALAR },
    2933    dbuser      => { type => SCALAR },
    3034    dbpasswd    => { type => SCALAR },
    31     log_level   => {
     35};
     36
     37my $new_validate = {
     38    trace       => {
    3239        type        => SCALAR,
    3340        optional    => 1,
    34         default     => 'all',
     41        default     => 'fatal',
    3542        callbacks   => {
    3643            'is valid level' => sub {
     
    3946        },
    4047    },
     48    dsn         => { type => SCALAR, optional => 1 },
     49    dbuser      => { type => SCALAR, optional => 1 },
     50    dbpasswd    => { type => SCALAR, optional => 1 },
    4151};
    4252
    4353__PACKAGE__->mk_ro_accessors( keys %$new_validate );
    4454
    45 sub init {
     55
     56sub new
     57{
    4658    my $class = shift;
    4759
     
    4961
    5062    # normalize log levels to lower-case
    51     $p{ log_level } = lc $p{ log_level };
    52 
    53     my $self = \%p;
     63    my $self = { trace => $LEVELS{lc($p{trace})} };
    5464
    5565    bless $self, $class || ref $class;
     66
     67    my @dbs;
     68    $self->{dbs} = \@dbs;
     69
     70    if (defined $p{dsn} or defined $p{dbuser} or defined $p{dbpasswd}) {
     71        $self->add_db(
     72            dbindex     => 0,
     73            dsn         => $p{dsn},
     74            dbuser      => $p{dbuser},
     75            dbpasswd    => $p{dbpasswd},
     76        );
     77    }
    5678
    5779    return $self;
    5880}
    5981
     82
     83sub add_db
     84{
     85    my $self = shift;
     86   
     87    my %p = validate( @_, $db_validate );
     88
     89    my $config_db = Nebulous::Server::Config::DB->new({
     90        dsn         => $p{dsn},
     91        dbuser      => $p{dbuser},
     92        dbpasswd    => $p{dbpasswd},
     93    });
     94
     95    $self->{dbs}->[$p{dbindex}] = $config_db;
     96
     97    return $self;
     98}
     99
     100
     101sub db
     102{
     103    my $self = shift;
     104
     105    my ($db_index) = validate_pos( @_, { type => SCALAR, optional => 1, });
     106
     107    # default to 0
     108    $db_index ||= 0;
     109
     110    return $self->{dbs}->[$db_index];
     111}
     112
     113
     114sub n_db
     115{
     116    my $self = shift;
     117
     118    return scalar @{ $self->{dbs} };
     119}
     120
     121
     122package Nebulous::Server::Config::DB;
     123
     124use strict;
     125use warnings FATAL => qw( all );
     126
     127our $VERSION = 0.01;
     128
     129use base qw( Class::Accessor::Fast );
     130
     131__PACKAGE__->mk_ro_accessors(qw( dsn dbuser dbpasswd ));
     132
    601331;
    61134
Note: See TracChangeset for help on using the changeset viewer.