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

data[RW]

The configuration values hash

defaults[R]

The configuration defaults

Public Class Methods

new(setup) click to toggle source

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() click to toggle source

Generate a JSON encoded string

# File lib/icfs/config.rb, line 236
def _generate()
  JSON.pretty_generate(@data)
end
clear() click to toggle source

Clear data

# File lib/icfs/config.rb, line 112
def clear; @data = {}; end
default(key) click to toggle source

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(key) click to toggle source

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(unam) click to toggle source

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() click to toggle source

Save a user configuration

# File lib/icfs/config.rb, line 253
def save; raise NotImplementedError; end
set(key, val) click to toggle source

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
set?(key) click to toggle source

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
setup(key=nil) click to toggle source

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

_key(unam) click to toggle source

Where to store objects

# File lib/icfs/config.rb, line 198
def _key(unam)
  @pre + unam
end
_opt(key) click to toggle source

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) click to toggle source

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