# HG changeset patch # User jittat # Date 2008-10-19 00:46:29 # Node ID 1d03356b1f432954548b7b51b3464814cefa417c # Parent 96c29b90fed23472a9b3e42422a1daf92af28462 removed caching on Configuration git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@285 6386c4cd-e34a-4fa8-8920-d93eb39b512e diff --git a/app/models/configuration.rb b/app/models/configuration.rb --- a/app/models/configuration.rb +++ b/app/models/configuration.rb @@ -7,14 +7,21 @@ SYSTEM_MODE_CONF_KEY = 'system.mode' + # set @@cache = true to only reload once. + @@cache = false + @@configurations = nil @@task_grading_info = nil def self.get(key) - if @@configurations == nil - self.read_config + if @@cache + if @@configurations == nil + self.read_config + end + return @@configurations[key] + else + return Configuration.read_one_key(key) end - return @@configurations[key] end def self.[](key) @@ -71,22 +78,32 @@ end protected + + def self.convert_type(val,type) + case type + when 'string' + return val + + when 'integer' + return val.to_i + + when 'boolean' + return (val=='true') + end + end + def self.read_config @@configurations = {} Configuration.find(:all).each do |conf| key = conf.key val = conf.value - case conf.value_type - when 'string' - @@configurations[key] = val + @@configurations[key] = Configuration.convert_type(val,conf.value_type) + end + end - when 'integer' - @@configurations[key] = val.to_i - - when 'boolean' - @@configurations[key] = (val=='true') - end - end + def self.read_one_key(key) + conf = Configuration.find_by_key(key) + return Configuration.convert_type(conf.value,conf.value_type) end def self.read_grading_info