class Iqvoc::Configuration::InstanceConfiguration
Attributes
defaults[R]
Public Class Methods
new()
click to toggle source
# File lib/iqvoc/configuration/instance_configuration.rb, line 33 def initialize @defaults = {} # default settings @records = {} # customized (non-default) settings @settings = {} # current settings, using defaults as fallback # NB: cannot cache immediately because defaults need to be registered first end
validate_value(value)
click to toggle source
checks whether value type is supported
# File lib/iqvoc/configuration/instance_configuration.rb, line 115 def self.validate_value(value) # TODO: compare type to default's? (cf. controller) if value == nil raise TypeError, 'nil values not supported' end # 0.class enables support for Ruby 2.4 Fixnum/Bignum into Integer class merge unless [TrueClass, FalseClass, String, 0.class, Float, Array].include?(value.class) raise TypeError, 'complex values not supported' end end
Public Instance Methods
[](key)
click to toggle source
retrieve individual setting, using default value as fallback
# File lib/iqvoc/configuration/instance_configuration.rb, line 71 def [](key) initialize_cache unless @initialized return @settings[key] end
[]=(key, value)
click to toggle source
store individual customized setting
# File lib/iqvoc/configuration/instance_configuration.rb, line 77 def []=(key, value) raise UnregisteredSetting unless @defaults.include?(key) self.class.validate_value(value) json = JSON.dump([value])[1..-2] # temporary array wrapper ensures valid JSON text if setting = ConfigurationSetting.find_by_key(key) setting.update(value: json) else ConfigurationSetting.create(key: key, value: json) end # update cache @records[key] = value @settings[key] = value return value end
deregister_setting(key)
click to toggle source
remove a default setting returns nil if setting does not exist NB: does not delete configuration settings from the database
# File lib/iqvoc/configuration/instance_configuration.rb, line 61 def deregister_setting(key) res = @defaults.delete(key) # update cache @settings.delete(key) return res end
initialize_cache()
click to toggle source
populate settings caches (subsequent updates will happen automatically via the respective setters)
# File lib/iqvoc/configuration/instance_configuration.rb, line 97 def initialize_cache return false unless ConfigurationSetting.table_exists? # pre-migration # cache customized settings ConfigurationSetting.all.each do |setting| @records[setting.key] = JSON.load("[#{setting.value}]")[0] # temporary array wrapper ensures valid JSON text end # cache current settings @defaults.each do |key, default_value| value = @records[key] @settings[key] = value.nil? ? default_value : value end @initialized = true end
register_setting(key, default_value)
click to toggle source
create or update a default setting
# File lib/iqvoc/configuration/instance_configuration.rb, line 49 def register_setting(key, default_value) self.class.validate_value(default_value) @defaults[key] = default_value # update cache @settings[key] = @records[key] || default_value end
register_settings(settings)
click to toggle source
convenience wrapper for ‘register_setting` batch operations accepts a hash of key / default value pairs
# File lib/iqvoc/configuration/instance_configuration.rb, line 42 def register_settings(settings) settings.each do |key, default_value| register_setting(key, default_value) end end