Class: Settings
Overview
Settings
Settings holds configuration information organized by Owners. An owner is a class that represents the system to be configured. An alias for this class is Settings.
You can pass strings, constants or symbols as keys for the classes to be configured. Passing symbols you can configure classes even before they are defined.
Defined Under Namespace
Classes: Setting, SettingCollection
Constant Summary collapse
- @@owners =
A hash of setting owners. Use double @‘s to allow for the Settings alias. – TODO: find a better name. ++
SyncHash.new
Class Method Summary collapse
-
.add_setting(owner, name, options) ⇒ Object
(also: setting)
Manually add a Settings setting.
-
.load(filename) ⇒ Object
Load and parse an external yaml Settings file.
-
.method_missing(sym) ⇒ Object
– FIXME: this does not work as expected.
-
.parse(options) ⇒ Object
Parse Settings parameters in yaml format.
-
.settings(owner = nil) ⇒ Object
(also: all, [])
Return the settings for the given owner.
-
.setup(options) ⇒ Object
Inject the Settings parameters provided as a hash (dictionary, ordered) to classes to be configured.
Class Method Details
.add_setting(owner, name, options) ⇒ Object Also known as: setting
Manually add a Settings setting. The class key can be the actual class name constant or a symbol. If the setting is already defined it updates it.
Examples
Settings.add_setting Compiler, :verification, :value => 12, :doc => ‘…’ Settings.setting :IdPart, :verify_registration_email, :value => false s = Settings.Compiler.verification.value
177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/more/facets/settings.rb', line 177 def add_setting(owner, name, ) owner = owner.to_s.to_sym @@owners[owner] ||= {} if s = @@owners[owner][name] # The setting already exists, update it. s.update() else # The setting does not exist, create it. @@owners[owner][name] = Setting.new(owner, name, ) end end |
.load(filename) ⇒ Object
Load and parse an external yaml Settings file.
163 164 165 |
# File 'lib/more/facets/settings.rb', line 163 def load(filename) parse(File.read(filename)) end |
.method_missing(sym) ⇒ Object
– FIXME: this does not work as expected. ++
210 211 212 213 214 215 216 217 218 219 |
# File 'lib/more/facets/settings.rb', line 210 def method_missing(sym) if sym.to_s.capitalized? bdl = SettingCollection.new bdl.owner = sym if hash = self[sym] bdl.update(hash) end return bdl end end |
.parse(options) ⇒ Object
Parse Settings parameters in yaml format.
147 148 149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/more/facets/settings.rb', line 147 def parse() temp = YAML::load() = Dictionary.new temp.each do |k, v| begin [k.gsub(/\./, '::').to_sym] = v rescue Object [k] = v end end setup() end |
.settings(owner = nil) ⇒ Object Also known as: all, []
Return the settings for the given owner. The owner is typically the Class that represents the system to be configured. If no class is provided, it returns all the registered settings.
195 196 197 198 199 200 201 202 |
# File 'lib/more/facets/settings.rb', line 195 def settings(owner = nil) if owner owner = owner.to_s.to_sym @@owners[owner] else @@owners.values.inject([]) { |memo, obj| memo.concat(obj.values) } end end |
.setup(options) ⇒ Object
Inject the Settings parameters provided as a hash (dictionary, ordered) to classes to be configured.
Warning: Pass an ordered hash (dictionary)
136 137 138 139 140 141 142 143 |
# File 'lib/more/facets/settings.rb', line 136 def setup() .each do |owner, ss| next unless ss ss.each do |name, s| add_setting(owner, name.to_sym, :value => s) end end end |