Class: Lorj::Defaults
- Defined in:
- lib/lorj_defaults.rb
Overview
This class is the Application configuration class used by Lorj::Config
It loads a defaults.yaml file (path defined by PrcLib::app_defaults)
The defaults.yaml data is accessible through Lorj.defaults.data Use this capability with caution, as it contents is R/W.
For getting Application defaults, use Lorj::Config[] or Lorj::Account[], Lorj::Account.get(key, nil, :names => [‘default’]) For setup meta data, use Lorj.defaults.get_meta, Lorj.defaults.get_meta_auto or Lorj.defaults.get_meta_section
defaults.yaml is divided in 3 sections. But only defaults section is loaded in Defaults instance. Defaults implements a AppConfig class, which provides meta data access Accessible through PrcLib.appdata
-
:default: Contains a list of key = value representing the application default configuration.
Data stored in this section are accessible through Lorj::Config[] or Lorj::Account[], Lorj::Account.get(key, nil, :names => [‘default’])
Ex:
# Use Lorj.defaults.data exceptionnaly Lorj.defaults.data.merge(:default => { data: 'test'}}) config = Lorj::Account.new puts config.get(:data, nil, :names => ['default']) # => test puts Lorj.defaults[:data] # => test
-
:setup: Contains :ask_step array For details, see Lorj::MetaAppConfig
-
:section: Contains a list of sections with several keys and attributes and eventually :default: For details, see Lorj::MetaAppConfig
Instance Method Summary collapse
-
#[](*keys) ⇒ Object
Get function It implements the following: 1.
-
#[]=(*_prop) ⇒ Object
Remove inherited method []=.
-
#exist?(*keys) ⇒ Boolean
Check if data found in this default layer has a value.
-
#get_meta(section, data, *options) ⇒ Object
Get model section/data options.
-
#get_meta_auto(data, *options) ⇒ Object
Get model data options.
-
#get_meta_section(key) ⇒ Object
-
Args : - ++ -> * Returns : - * Raises : - ++ ->.
-
-
#load ⇒ Object
-
Args : - ++ -> * Returns : - * Raises : - ++ ->.
-
-
#meta_each ⇒ Object
Loop on Config metadata This function is obsolete.
-
#meta_exist?(key) ⇒ Boolean
Check existence of the key in metadata.
Instance Method Details
#[](*keys) ⇒ Object
Get function It implements the following:
-
Search in data_options section or :default section if missing
-
otherwise, provide the metadata default value, erb like.
It uses [:metadata_section]
-
Args
-
keys
: Array of key path to found
-
-
Returns
-
value
-
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/lorj_defaults.rb', line 76 def [](*keys) return nil if keys.length == 0 if @data_options[:section].nil? section = :default else section = @data_options[:section] end return p_get(section, *keys) if p_exist?(section, *keys) = @data_options[:metadata_section] return nil if .nil? default = Lorj.data.section_data(, keys[0], :default_value) return nil if default.nil? default end |
#[]=(*_prop) ⇒ Object
Remove inherited method []=
128 129 |
# File 'lib/lorj_defaults.rb', line 128 def []=(*_prop) end |
#exist?(*keys) ⇒ Boolean
Check if data found in this default layer has a value.
It implements the following:
-
Search in data_options section or :default section if missing
-
otherwise, provide the metadata default value, erb like.
It uses [:metadata_section]
-
Args
-
keys
: Array of key path to found
-
-
Returns
-
boolean : true if the key path was found
-
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/lorj_defaults.rb', line 109 def exist?(*keys) return nil if keys.length == 0 if @data_options[:section].nil? section = :default else section = @data_options[:section] end return true if p_exist?(section, *keys) = @data_options[:metadata_section] return false if .nil? Lorj.data.exist?(:sections, , keys[0], :default_value) end |
#get_meta(section, data, *options) ⇒ Object
Get model section/data options.
-
Args :
-
section
: section name -
data
: data name -
options
: options tree.
-
-
Returns :
-
data options values
OR
-
nil if:
-
missing section and data name as parameter.
-
data was not found. defined in /:sections/<section>/<data
-
-
-
Raises :
-
++ ->
-
212 213 214 215 216 217 218 |
# File 'lib/lorj_defaults.rb', line 212 def (section, data, *) PrcLib.debug("'Lorj.defaults.%s' is obsolete and will be removed "\ 'in Lorj 2.0. Please update your code to call '\ "'Lorj.data.%s' instead.\n%s", __method__, 'section_data', caller[0]) Lorj.data.section_data(section, data, *) end |
#get_meta_auto(data, *options) ⇒ Object
Get model data options. Section name is determined by the associated data name
-
Args :
-
data
: data name -
options+ : options tree.
-
-
Returns :
-
data options values
OR
-
nil if:
-
missing data name as parameter.
-
data was not found. defined in /:sections/<section>/<data
-
-
-
Raises :
-
++ ->
-
184 185 186 187 188 189 190 |
# File 'lib/lorj_defaults.rb', line 184 def (data, *) PrcLib.debug("'Lorj.defaults.%s' is obsolete and will be removed "\ 'in Lorj 2.0. Please update your code to call '\ "'Lorj.data.%s' instead.\n%s", __method__, 'auto_section_data', caller[0]) Lorj.data.auto_section_data(data, *) end |
#get_meta_section(key) ⇒ Object
-
Args :
-
++ ->
-
-
Returns : -
-
Raises :
-
++ ->
-
228 229 230 231 232 233 234 235 236 237 |
# File 'lib/lorj_defaults.rb', line 228 def (key) PrcLib.debug("'Lorj.defaults.%s' is obsolete and will be removed "\ 'in Lorj 2.0. Please update your code to call '\ "'Lorj.data.%s' instead.\n%s. Warning! This function call "\ "return is different than 'Lorj.defaults.%s'. Please read "\ 'the documentation', __method__, 'first_section', caller[0], __method__) section, = Lorj.data.first_section(key) section end |
#load ⇒ Object
-
Args :
-
++ ->
-
-
Returns : -
-
Raises :
-
++ ->
-
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/lorj_defaults.rb', line 247 def load if !PrcLib.app_defaults PrcLib.debug('PrcLib.app_defaults is not set. Application defaults'\ " won't be loaded.") else @filename = File.join(PrcLib.app_defaults, 'defaults.yaml') PrcLib.info("Reading default configuration '%s'...", @filename) if File.exist?(@filename) p_load(@filename) else PrcLib.warning("PrcLib.app_defaults is set to '%s'. Trying to load"\ " '%s' but not found. Application defaults won't "\ 'be loaded.', PrcLib.app_defaults, @filename) end end end |
#meta_each ⇒ Object
Loop on Config metadata This function is obsolete. Use Lorj.metadata.meta_each instead
-
Args :
-
code
: Block of code on ‘section`, `key`, `value`
-
-
Returns :
-
nothing
-
142 143 144 145 146 147 148 149 150 |
# File 'lib/lorj_defaults.rb', line 142 def PrcLib.debug("'Lorj.defaults.%s' is obsolete and will be removed "\ 'in Lorj 2.0. Please update your code to call '\ "'Lorj.data.%s' instead.\n%s", __method__, 'meta_each', caller[0]) Lorj.data. do |section, key, value| yield section, key, value end end |
#meta_exist?(key) ⇒ Boolean
Check existence of the key in metadata. This function is obsolete. Use Lorj.metadata.auto_meta_exist? instead Consider also the Lorj.metadata.auto_meta_exist? which check from a section name, a well.
-
Args :
-
key
: Key name to check.
-
-
Returns :
-
true if found, false otherwise.
-
162 163 164 165 166 167 168 |
# File 'lib/lorj_defaults.rb', line 162 def (key) PrcLib.debug("'Lorj.defaults.%s' is obsolete and will be removed "\ 'in Lorj 2.0. Please update your code to call '\ "'Lorj.data.%s' instead.\n%s", __method__, 'auto_meta_exist?', caller[0]) Lorj.data.(key) end |