From b19279a13d9342275d593c0a6d7e07c6ad679a9c Mon Sep 17 00:00:00 2001 From: Colin Kuskie Date: Wed, 13 May 2009 17:32:49 +0000 Subject: [PATCH] Add a has method to Session/Setting, so that it's easy to check if a setting already exists. Very handy for upgrade scripts. --- lib/WebGUI/Session/Setting.pm | 24 ++++++++++++++++++++++++ t/Session/Setting.t | 6 +++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/lib/WebGUI/Session/Setting.pm b/lib/WebGUI/Session/Setting.pm index 1daee428d..297a057e2 100644 --- a/lib/WebGUI/Session/Setting.pm +++ b/lib/WebGUI/Session/Setting.pm @@ -109,6 +109,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 ) diff --git a/t/Session/Setting.t b/t/Session/Setting.t index 934460ed3..ce90ea387 100644 --- a/t/Session/Setting.t +++ b/t/Session/Setting.t @@ -15,7 +15,7 @@ use lib "$FindBin::Bin/../lib"; use WebGUI::Test; use WebGUI::Session; -use Test::More tests => 7; # increment this value for each test you create +use Test::More tests => 10; # 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()");