Changeset 23932 for trunk/Nebulous-Server/lib/Nebulous/Server/Config.pm
- Timestamp:
- Apr 20, 2009, 11:15:20 AM (17 years ago)
- Location:
- trunk/Nebulous-Server
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
lib/Nebulous/Server/Config.pm (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/Nebulous-Server
-
Property svn:mergeinfo
set to
/branches/neb_distrib_20081210/Nebulous-Server merged eligible /trunk/Nebulous-Server merged eligible
-
Property svn:mergeinfo
set to
-
trunk/Nebulous-Server/lib/Nebulous/Server/Config.pm
r20990 r23932 8 8 use warnings FATAL => qw( all ); 9 9 10 our $VERSION = 0.0 2;10 our $VERSION = 0.03; 11 11 12 12 use base qw( Class::Accessor::Fast ); 13 13 14 14 use Log::Log4perl qw( :levels ); 15 use Params::Validate qw( validate SCALAR );15 use Params::Validate qw( validate validate_pos SCALAR ); 16 16 17 17 our %LEVELS = ( … … 25 25 ); 26 26 27 my $new_validate = { 27 my $db_validate = { 28 dbindex => { 29 type => SCALAR, 30 regex => qr/^\d+$/, 31 }, 28 32 dsn => { type => SCALAR }, 29 33 dbuser => { type => SCALAR }, 30 34 dbpasswd => { type => SCALAR }, 31 log_level => { 35 }; 36 37 my $new_validate = { 38 trace => { 32 39 type => SCALAR, 33 40 optional => 1, 34 default => ' all',41 default => 'fatal', 35 42 callbacks => { 36 43 'is valid level' => sub { … … 39 46 }, 40 47 }, 48 dsn => { type => SCALAR, optional => 1 }, 49 dbuser => { type => SCALAR, optional => 1 }, 50 dbpasswd => { type => SCALAR, optional => 1 }, 41 51 }; 42 52 43 53 __PACKAGE__->mk_ro_accessors( keys %$new_validate ); 44 54 45 sub init { 55 56 sub new 57 { 46 58 my $class = shift; 47 59 … … 49 61 50 62 # 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})} }; 54 64 55 65 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 } 56 78 57 79 return $self; 58 80 } 59 81 82 83 sub 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 101 sub 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 114 sub n_db 115 { 116 my $self = shift; 117 118 return scalar @{ $self->{dbs} }; 119 } 120 121 122 package Nebulous::Server::Config::DB; 123 124 use strict; 125 use warnings FATAL => qw( all ); 126 127 our $VERSION = 0.01; 128 129 use base qw( Class::Accessor::Fast ); 130 131 __PACKAGE__->mk_ro_accessors(qw( dsn dbuser dbpasswd )); 132 60 133 1; 61 134
Note:
See TracChangeset
for help on using the changeset viewer.
