Class: ICFS::Config Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/icfs/config.rb

Overview

This class is abstract.

Configuration storage interface

Direct Known Subclasses

ConfigRedis, ConfigS3

Constant Summary collapse

SetupTimezone =

Default setup for ‘tz’

{
  name: 'Timezone',
  default: '+00:00',
  validate: {
    method: :string,
    valid: /[+\-](0[0-9]|1[0-2]):[0-5][0-9]/.freeze,
    whitelist: true,
  }.freeze,
  label: 'cfg-tz',
  input: [:text, 'form-tz'].freeze,
  parse: :text,
  tip: 'Timezone to display date/times, format as +/-HH:MM.',
  display: 'list-text-m',
}.freeze
SetupRelTime =

default setup for ‘rel_time’

{
  name: 'Rel. Time',
  default: true,
  validate: Validate::IsBoolean,
  label: 'cfg-reltime',
  input: [:boolean].freeze,
  parse: :boolean,
  tip: 'Display relative times e.g. 3 days ago.',
  display: 'list-text-s',
}.freeze
SetupCss =

default setup for ‘css’

{
  name: 'Style',
  default: '/static/icfs.css',
  validate: {
    method: :string,
    allowed: Set[
      '/static/icfs.css',
      '/static/icfs-dark.css'
    ].freeze,
    whitelist: true,
  }.freeze,
  label: 'cfg-css',
  input: [
    :select,
    'form-css',
    'cfg-css',
    [
      ['/static/icfs.css', 'Light'].freeze,
      ['/static/icfs-dark.css', 'Dark'].freeze,
    ].freeze
  ].freeze,
  parse: :text,
  tip: 'Display settings for web interface.',
  display: 'list-text-l',
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(setup) ⇒ Config

New instance

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

Parameters:

  • setup (Array<Array>)

    The setup array



98
99
100
101
102
103
104
105
106
107
# 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

Instance Attribute Details

#dataObject

The configuration values hash



118
119
120
# File 'lib/icfs/config.rb', line 118

def data
  @data
end

#defaultsObject (readonly)

The configuration defaults



124
125
126
# File 'lib/icfs/config.rb', line 124

def defaults
  @defaults
end

Instance Method Details

#_generateObject

Generate a JSON encoded string



236
237
238
# File 'lib/icfs/config.rb', line 236

def _generate()
  JSON.pretty_generate(@data)
end

#clearObject

Clear data



112
# File 'lib/icfs/config.rb', line 112

def clear; @data = {}; end

#default(key) ⇒ Object

Get the default value

Parameters:

  • key (String)

    The name of the config setting



166
167
168
169
# File 'lib/icfs/config.rb', line 166

def default(key)
  opt = _opt(key)
  opt[:default]
end

#get(key) ⇒ Object

Get a value

Parameters:

  • key (String)

    The name of the config setting



143
144
145
146
# File 'lib/icfs/config.rb', line 143

def get(key)
  opt = _opt(key)
  @data.key?(key) ? @data[key] : opt[:default]
end

#load(unam) ⇒ Boolean

Load a user configuration

Parameters:

  • unam (String)

    the user name to load

Returns:

  • (Boolean)

    if any config data was found for the user

Raises:

  • (NotImplementedError)


247
# File 'lib/icfs/config.rb', line 247

def load(unam); raise NotImplementedError; end

#saveObject

Save a user configuration

Raises:

  • (NotImplementedError)


253
# File 'lib/icfs/config.rb', line 253

def save; raise NotImplementedError; end

#set(key, val) ⇒ Object

Set a value

Parameters:

  • key (String)

    The name of the config setting

  • val (Object)

    The value of the config setting



155
156
157
158
159
# 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) ⇒ Boolean

Is the value set?

Parameters:

  • key (String)

    The name of the config setting

Returns:

  • (Boolean)


177
# File 'lib/icfs/config.rb', line 177

def set?(key); @data.key?(key); end

#setup(key = nil) ⇒ Hash, Array

Get setup

Parameters:

  • key (String) (defaults to: nil)

    the specific key to get

Returns:

  • (Hash, Array)

    the setup for the key or an array of [key, setup]



186
187
188
189
190
191
192
# 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