Allow new config store to be univerally available within the app

This commit is contained in:
Duncan Ferguson 2010-06-25 16:20:48 +01:00
parent 1410a8f807
commit 4b03d3f6ea
2 changed files with 125 additions and 11 deletions

View file

@ -7,17 +7,18 @@ use App::ClusterSSH::L10N;
# Dont use SVN revision as it can cause problems
use version;
our $VERSION = version->new('0.01');
our $VERSION = version->new('0.02');
my $debug_level = 0;
our $language = 'en';
our $language_handle;
our $app_configuration;
sub new {
my ( $class, %args) = @_;
my ( $class, %args ) = @_;
my $config = {
lang => 'en',
lang => 'en',
debug => 0,
%args,
};
@ -27,8 +28,9 @@ sub new {
$self->set_debug_level( $config->{debug} );
$self->set_lang( $config->{lang} );
$self->debug( 7,
$self->loc('Arguments to [_1]->new(): ', $class),
$self->debug(
7,
$self->loc( 'Arguments to [_1]->new(): ', $class ),
$self->_dump_args_hash(%args),
);
@ -69,11 +71,9 @@ sub loc {
sub set_lang {
my ( $self, $lang ) = @_;
$language=$lang;
$language = $lang;
if ($self) {
$self->debug( 6,
$self->loc('Setting language to "[_1]"', $lang ),
);
$self->debug( 6, $self->loc( 'Setting language to "[_1]"', $lang ), );
}
return $self;
}
@ -109,6 +109,34 @@ sub debug {
return $self;
}
sub config {
my ($self) = @_;
if ( !$app_configuration ) {
croak( _translate('config has not yet been set') );
}
return $app_configuration;
}
sub set_config {
my ( $self, $config ) = @_;
if ($app_configuration) {
croak( _translate('config has already been set') );
}
if(!$config) {
croak( _translate('passed config is empty'));
}
$self->debug( 3, _translate('Setting app configuration') );
$app_configuration = $config;
return $self;
}
1;
=pod
@ -177,6 +205,15 @@ a wrapper to maketext in Locale::Maketext
Output text on STDOUT.
=item $config = $obj->config;
Returns whatever configuration object has been set up. Croaks if set_config
hasnt been called
=item $obj->set_config($config);
Set the config to the given value - croaks if has already been called
=back
=head1 AUTHOR

View file

@ -7,7 +7,7 @@ use lib "$Bin/../lib";
use Test::More;
use Test::Trap;
BEGIN { use_ok( 'App::ClusterSSH::Base' ) }
BEGIN { use_ok('App::ClusterSSH::Base') }
# force default language for tests
App::ClusterSSH::Base->set_lang('en');
@ -36,7 +36,7 @@ for my $level ( 0 .. 9 ) {
trap {
for my $log_level ( 0 .. 9 ) {
$base->debug( $log_level, 'test');
$base->debug( $log_level, 'test' );
}
};
@ -121,4 +121,81 @@ like(
'got expected new() output'
);
# config tests
$base = undef;
my $get_config;
my $object;
trap {
$base = App::ClusterSSH::Base->new( debug => 3, );
};
isa_ok( $base, 'App::ClusterSSH::Base' );
is( $trap->leaveby, 'return', 'returned ok' );
is( $trap->die, undef, 'returned ok' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
trap {
$get_config = $base->config();
};
is( $trap->leaveby, 'die', 'died ok' );
like( $trap->die, qr/^config has not yet been set/,
'Got correct croak text' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Expecting not STDOUT' );
is( $get_config, undef, 'config left empty' );
trap {
$object = $base->set_config();
};
is( $trap->leaveby, 'die', 'died ok' );
like( $trap->die, qr/^passed config is empty/, 'Got correct croak text' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Expecting no STDOUT' );
trap {
$object = $base->set_config('set to scalar');
};
is( $trap->leaveby, 'return', 'returned ok' );
is( $trap->die, undef, 'config set ok' );
is( $trap->stderr, '', 'Expecting no STDERR' );
like(
$trap->stdout,
qr/^Setting\sapp\sconfiguration/xsm,
'Got expected STDOUT'
);
isa_ok( $object, 'App::ClusterSSH::Base' );
trap {
$get_config = $base->config();
};
is( $trap->leaveby, 'return', 'returned ok' );
is( $trap->die, undef, 'returned ok' );
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Expecting not STDOUT' );
is( $get_config, 'set to scalar', 'config set as expected' );
trap {
$object = $base->set_config('set to another scalar');
};
is( $trap->leaveby, 'die', 'died ok' );
like(
$trap->die,
qr/^config\shas\salready\sbeen\sset/,
'config cannot be reset'
);
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Got expected STDOUT' );
trap {
$object = $base->set_config();
};
is( $trap->leaveby, 'die', 'died ok' );
like(
$trap->die,
qr/^config\shas\salready\sbeen\sset/,
'config cannot be reset'
);
is( $trap->stderr, '', 'Expecting no STDERR' );
is( $trap->stdout, '', 'Got expected STDOUT' );
done_testing();