Class: ICFS::Config Abstract
- Inherits:
-
Object
- Object
- ICFS::Config
- Defined in:
- lib/icfs/config.rb
Overview
Configuration storage interface
Direct Known Subclasses
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
-
#data ⇒ Object
The configuration values hash.
-
#defaults ⇒ Object
readonly
The configuration defaults.
Instance Method Summary collapse
-
#_generate ⇒ Object
Generate a JSON encoded string.
-
#clear ⇒ Object
Clear data.
-
#default(key) ⇒ Object
Get the default value.
-
#get(key) ⇒ Object
Get a value.
-
#initialize(setup) ⇒ Config
constructor
New instance.
-
#load(unam) ⇒ Boolean
Load a user configuration.
-
#save ⇒ Object
Save a user configuration.
-
#set(key, val) ⇒ Object
Set a value.
-
#set?(key) ⇒ Boolean
Is the value set?.
-
#setup(key = nil) ⇒ Hash, Array
Get setup.
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
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
#data ⇒ Object
The configuration values hash
118 119 120 |
# File 'lib/icfs/config.rb', line 118 def data @data end |
#defaults ⇒ Object (readonly)
The configuration defaults
124 125 126 |
# File 'lib/icfs/config.rb', line 124 def defaults @defaults end |
Instance Method Details
#_generate ⇒ Object
Generate a JSON encoded string
236 237 238 |
# File 'lib/icfs/config.rb', line 236 def _generate() JSON.pretty_generate(@data) end |
#clear ⇒ Object
Clear data
112 |
# File 'lib/icfs/config.rb', line 112 def clear; @data = {}; end |
#default(key) ⇒ Object
Get the default value
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
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
247 |
# File 'lib/icfs/config.rb', line 247 def load(unam); raise NotImplementedError; end |
#save ⇒ Object
Save a user configuration
253 |
# File 'lib/icfs/config.rb', line 253 def save; raise NotImplementedError; end |
#set(key, val) ⇒ Object
Set a value
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?
177 |
# File 'lib/icfs/config.rb', line 177 def set?(key); @data.key?(key); end |
#setup(key = nil) ⇒ Hash, Array
Get 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 |