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.

Methods
add_setting load method_missing parse settings setup
Classes and Modules
Class Settings::Setting
Class Settings::SettingCollection
Public Class methods
add_setting(owner, name, options)

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

# 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
load(filename)

Load and parse an external yaml Settings file.

# File lib/more/facets/settings.rb, line 161
    def load(filename)
      parse(File.read(filename))
    end
method_missing(sym)
# 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
parse(options)

Parse Settings parameters in yaml format.

# File lib/more/facets/settings.rb, line 145
    def parse(options)
      temp = YAML::load(options)
      options = Dictionary.new
      temp.each do |k, v|
        begin
          options[k.gsub(/\./, '::').to_sym] = v
        rescue Object
          options[k] = v
        end
      end

      setup(options)
    end
settings(owner = nil)

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
setup(options)

Inject the Settings parameters provided as a hash (dictionary, ordered) to classes to be configured.

Warning: Pass an ordered hash (dictionary)

# 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