--- trunk/lib/WebPAC/Config.pm 2006/09/24 15:28:49 681 +++ trunk/lib/WebPAC/Config.pm 2007/11/02 13:11:35 967 @@ -14,11 +14,11 @@ =head1 VERSION -Version 0.01 +Version 0.02 =cut -our $VERSION = '0.01'; +our $VERSION = '0.02'; =head1 SYNOPSIS @@ -30,6 +30,10 @@ Create new configuration object. + my $config = new WebPAC::Config( + path => '/optional/path/to/config.yml' + ); + =cut sub new { @@ -51,6 +55,8 @@ $self->{path} ||= 'conf/config.yml'; + $log->logdie("can't open ", $self->{path}, ": $!") if ! -r $self->{path}; + $self->{config} = LoadFile($self->{path}) || $log->logdie("can't open ",$self->{path}, ": $!"); @@ -61,6 +67,8 @@ =head2 databases +Return all databases in config + my $config_databases_hash = $config->databases; my @config_databases_names = $config->databases; @@ -78,17 +86,96 @@ =head2 use_indexer +Which indexer are we using? + $config->use_indexer; =cut sub use_indexer { my $self = shift; - my $default = 'hyperestraier'; return unless ($self->{config}); - return $self->{config}->{use_indexer} || $default; + return $self->{config}->{use_indexer} || undef; +} + +=head2 get + + $config->get('top-level_key'); + +=cut + +sub get { + my $self = shift; + my $what = shift || return; + return $self->{config}->{$what}; } +=head2 webpac + +Return C<< config -> webpac >> parts + + my @supported_inputs = $config->webpac('inputs'); + my $inputs_hash = $config->webpac('inputs'); + +=cut + +sub webpac { + my $self = shift; + + $self->_get_logger()->logdie("can't find config->webpac") unless defined( $self->{config}->{webpac} ); + + my $what = shift || return $self->{config}->{webpac}; + + if (wantarray && ref( $self->{config}->{webpac}->{$what} ) eq 'HASH') { + return keys %{ $self->{config}->{webpac}->{$what} }; + } else { + return $self->{config}->{webpac}->{$what}; + } + +} + +=head2 iterate_inputs + + $config->iterate_inputs( sub { + my ($input, $database, $database_config_hash) = @_; + # ... do something with input config hash + } ); + +This function will also modify C<< $input->{normalize} >> to +be C, even with just one element. + +=cut + +sub iterate_inputs { + my $self = shift; + + my $log = $self->_get_logger(); + + my $code_ref = shift; + $log->logdie("called with CODE") unless ( ref($code_ref) eq 'CODE' ); + + while (my ($database, $db_config) = each %{ $self->{config}->{databases} }) { + my @inputs; + if (ref($db_config->{input}) eq 'ARRAY') { + @inputs = @{ $db_config->{input} }; + } elsif ($db_config->{input}) { + push @inputs, $db_config->{input}; + } else { + $log->info("database $database doesn't have inputs defined"); + } + + foreach my $input (@inputs) { + $log->debug("iterating over input ", dump($input)); + if ( defined( $input->{normalize} ) && ref($input->{normalize}) ne 'ARRAY' ) { + $input->{normalize} = [ $input->{normalize} ]; + } + $code_ref->($input, $database, $db_config); + } + } + +} + + =head1 AUTHOR Dobrica Pavlinusic, C<< >>