class ICFS::Config
Configuration storage interface
@abstract
Constants
- SetupCss
default setup for 'css'
- SetupRelTime
default setup for 'rel_time'
- SetupTimezone
Default setup for 'tz'
Attributes
The configuration values hash
The configuration defaults
Public Class Methods
New instance
@param setup [Array<Array>] The setup array
Each item is a [key, hash], each hash should contain:
-
:name The name of the config setting
-
:default The default value
-
:validate A Validator
-
:label The HTML label
-
:input Array used by {Web::Client#_form_config}
-
:parse
-
:tip Text of the tup
-
:display Array to pass to {Web::Client#_div_config}
# File lib/icfs/config.rb, line 98 def initialize(setup) @data = {} @unam = nil @order = [] @setup = {} setup.each do |ary| @order << ary[0] @setup[ary[0]] = ary[1] end end
Public Instance Methods
Generate a JSON encoded string
# File lib/icfs/config.rb, line 236 def _generate() JSON.pretty_generate(@data) end
Clear data
# File lib/icfs/config.rb, line 112 def clear; @data = {}; end
Get the default value
@param key [String] The name of the config setting
# File lib/icfs/config.rb, line 166 def default(key) opt = _opt(key) opt[:default] end
Get a value
@param key [String] The name of the config setting
# File lib/icfs/config.rb, line 143 def get(key) opt = _opt(key) @data.key?(key) ? @data[key] : opt[:default] end
Load a user configuration
@param unam [String] the user name to load @return [Boolean] if any config data was found for the user
# File lib/icfs/config.rb, line 247 def load(unam); raise NotImplementedError; end
Save a user configuration
# File lib/icfs/config.rb, line 253 def save; raise NotImplementedError; end
Set a value
@param key [String] The name of the config setting @param val [Object] The value of the config setting
# File lib/icfs/config.rb, line 155 def set(key, val) opt = _opt(key) Items.validate(val, opt[:name], opt[:validate]) @data[key] = val end
Is the value set?
@param key [String] The name of the config setting
# File lib/icfs/config.rb, line 177 def set?(key); @data.key?(key); end
Get setup @param key [String] the specific key to get @return [Hash, Array] the setup for the key or
an array of \[key, setup\]
# File lib/icfs/config.rb, line 186 def setup(key=nil) return _opt(key) if key return @order.map do |key| [key, @setup[key]] end end
Private Instance Methods
Where to store objects
# File lib/icfs/config.rb, line 198 def _key(unam) @pre + unam end
Get the option for this key
# File lib/icfs/config.rb, line 130 def _opt(key) opt = @setup[key] raise(ArgumentError, 'Invalid config option') unless opt return opt end
Parse JSON encoded config settings
# File lib/icfs/config.rb, line 206 def _parse(json) if json.nil? raise(Error::NotFound, 'Config not found') end begin itm = JSON.parse(json) rescue raise(Error::Value, 'JSON parsing failed') end errs = {} itm.each do |key, val| opt = @setup[key] raise(Error::Value, 'Unsupported config option %s' % key) if !opt err = Validate.check(val, opt[:validate]) errs[key] = err if err end unless errs.empty? raise(Error::Value, 'Config has bad settings: %s' % errs.inspect) end @data = itm end