Banal-Config
view release on metacpan or search on metacpan
lib/Banal/Config.pm view on Meta::CPAN
package Banal::Config;
use 5.006;
use strict;
use warnings;
no warnings qw(uninitialized);
our $VERSION = '0.11';
use File::Spec;
use Banal::Config::General;
use Banal::Config::General::Extended;
use Banal::Utils::Data qw(banal_get_data);
use Moose;
has 'verbose' => (is => 'rw', lazy_build=>1);
has 'debug' => (is => 'rw', lazy_build=>1);
has 'switches' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # Typically contains command line switches as produced by Getopt::Long or Getopt::Descriptive, or something that resembles it.
has 'options' => (is => 'rw', isa => 'HashRef', default=>sub{{}}); # The hash that gets passed to "new" for the actual configuration object (xcfg). The 'ConfigFile' option will default to the value of 'source' property.
has 'source' => (is => 'rw', isa => 'Str', lazy_build=>1); # The path to the configuration file. If it's not set, it will be guessed based on the name of the running process ($0). Note that this may be overriden by the "-ConfigFile" option.
has 'xcfg_class' => (is => 'rw', isa => 'Str', default=>'Banal::Config::General::Extended' );
has 'xcfg' => (
is => 'rw',
isa => 'Banal::Config::General::Extended',
lazy_build => 1,
handles => [qw (obj value hash array is_hash is_array is_scalar exists keys delete configfile find)],
);
has 'cfg_hash' => (is => 'rw', lazy_build=>1);
has 'cfg_context' => (is => 'rw', lazy_build=>1); # If you do not set this, you can also provide it with the 'cfg_context' switch or the '-Banal_ConfigContext' option. Otherwise, it will take upon the value given by 'cfg_context_default'
has 'cfg_context_default' => (is => 'rw', lazy_build=>1); # You may wish to override this, if needed.
has 'default_options_for_banal_get_data' => (is => 'rw', default=>sub {
{
search_upwards_while_not_defined => 1,
use_path_semantics => 1,
path_separator => '/',
remove_extra_separators => 1,
remove_leading_separator => 0,
remove_trailing_separator => 1,
remove_empty_segments => 1,
try_avoiding_repeated_segments => 1,
lower_case => 1,
trim => 1,
}
}
);
#-----------------------------------------------
sub load {
my $self = shift;
return $self->reload();
}
#-----------------------------------------------
sub reload {
my $self = shift;
my $cc = $self->xcfg_class;
my $opts = $self->options;
eval {
require $cc;
};
my $c = $cc->new(-ConfigFile=>$self->source, %$opts); # source can be overriden with the options.
return $self->xcfg($c);
}
#
#-----------------------------------------------
sub get_cfg {
my $self = shift;
return $self->grab_cfg(key=>[@_]);
}
#-----------------------------------------------
sub grab_cfg {
my $self = shift;
my $args = {@_};
$args->{data} ||= $self->cfg_hash();
unless (defined($args->{context})) {
$args->{context} = $self->cfg_context();
}
unless (defined($args->{options})) {
# get a copy.
my $opts = $self->default_options_for_banal_get_data();
$args->{options} = {%$opts};
( run in 0.525 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )