Add a has method to settings, with tests.
has checks to see if the setting exists in the object's cache. It's safer than poking inside the object's hash. It should mainly be used for developers who run the same script over and over again.
This commit is contained in:
parent
09c5b4cb9e
commit
0695e4f4fc
2 changed files with 29 additions and 1 deletions
|
|
@ -100,6 +100,30 @@ sub get {
|
|||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 has ( $param )
|
||||
|
||||
Returns true if the requested setting exists in this object's cache of the settings.
|
||||
This works better than using ->get, since it doesn't care about the truthiness of
|
||||
the value of the setting.
|
||||
|
||||
This method will have little use outside of upgrade and install scripts, to prevent
|
||||
them from creating and/or overwriting existing settings.
|
||||
|
||||
=head3 $param
|
||||
|
||||
The setting to check.
|
||||
|
||||
=cut
|
||||
|
||||
sub has {
|
||||
my $self = shift;
|
||||
my $param = shift;
|
||||
return exists $self->{_settings}{$param};
|
||||
}
|
||||
|
||||
|
||||
#-------------------------------------------------------------------
|
||||
|
||||
=head2 new ( session )
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ use lib "$FindBin::Bin/../lib";
|
|||
use WebGUI::Test;
|
||||
use WebGUI::Session;
|
||||
|
||||
use Test::More tests => 6; # increment this value for each test you create
|
||||
use Test::More tests => 9; # increment this value for each test you create
|
||||
|
||||
my $session = WebGUI::Test->session;
|
||||
|
||||
|
|
@ -27,6 +27,10 @@ $session->setting->set("inmate","37927");
|
|||
my ($value) = $session->db->quickArray("select value from settings where name='inmate'");
|
||||
is($value, '37927', "set()");
|
||||
is($session->setting->get("inmate"), '37927', 'set() also updates object cache');
|
||||
is($session->setting->has('inmate'), 1, 'has checks for existance');
|
||||
$session->setting->set('inmate', 0);
|
||||
is($session->setting->get('inmate'), 0, 'get will get 0 values');
|
||||
is($session->setting->has('inmate'), 1, 'has checks for existance, regardless of value');
|
||||
$session->setting->remove("inmate");
|
||||
my ($value) = $session->db->quickArray("select value from settings where name='inmate'");
|
||||
is($value, undef, "delete()");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue