Class | Settings |
In: |
lib/more/facets/settings.rb
|
Parent: | Object |
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.
add_setting | -> | setting |
settings | -> | all |
settings | -> | [] |
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.
Settings.add_setting Compiler, :verification, :value => 12, :doc => ’…’ Settings.setting :IdPart, :verify_registration_email, :value => false s = Settings.Compiler.verification.value
# File lib/more/facets/settings.rb, line 175 def add_setting(owner, name, options) owner = owner.to_s.to_sym @@owners[owner] ||= {} if s = @@owners[owner][name] # The setting already exists, update it. s.update(options) else # The setting does not exist, create it. @@owners[owner][name] = Setting.new(owner, name, options) end end
# File lib/more/facets/settings.rb, line 208 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
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.
# File lib/more/facets/settings.rb, line 193 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
Inject the Settings parameters provided as a hash (dictionary, ordered) to classes to be configured.
# File lib/more/facets/settings.rb, line 134 def setup(options) options.each do |owner, ss| next unless ss ss.each do |name, s| add_setting(owner, name.to_sym, :value => s) end end end