module Settings::ClassLevel
Class-level settings can be either static (file/code-based) or dynamic (db-based) depending on your needs. Static settings will work well for gem configuration, command-line tools, etc. while dynamic settings might be useful for a CMS or other web-based tool that needs to support user editing of settings values on-the-fly.
Public Instance Methods
Define the class-level settings for a given class. Supported options include:
:store => :static | :dynamic - how to load and (potentially) save settings values, defaults to :static
Static mode options:
:file => '/path/to/file' - provides a single file to load when using the static settings store :files => ['/path1', '/path2'] - same as :file, but allows multiple files to be loaded in order
Reload timing (primarily intended for use with :db mode):
:reload => <when> - when and if to reload from file/db, with <when> as one of: true - on every access to #settings false - only do initial load, never reload '/path/to/file' - file to test for modified timestamp changes, reload if file timestamp is after latest load <num seconds> - after N seconds since last load lambda { <true to reload> } - custom lambda to execute to check for reload, reloads on returned true value
Any options passed on subsequent calls to class_settings
will be ignored.
A passed block will be evaluated in the context of a Settings::Builder
instance that can be used to define groups and entries.
Example:
class Site class_settings(:file => File.join(RAILS_ROOT, 'config/site-settings.rb')) do string('name') group('security') do bool('force-ssl', false) string('secret-key') end end end
The above would set up Site.settings.name, Site.settings.security.force_ssl, etc, with an optional settings file located at $RAILS_ROOT/config/site-settings.rb
# File lib/iron/settings/class_level.rb, line 82 def class_settings(options = {}, &block) unless @settings_class_root # Set up our root group and options @settings_class_root = Settings::Root.new() options = { :store => :static }.merge(options) # Set our default reload timing if options[:reload].nil? if options[:store] == :static # Static settings generally don't need reloading options[:reload] = false else # For dynamic, db-backed settings at the class level, we use # file modified reload timing by default options[:reload] = Settings.default_timestamp_file(self.name) end end # Save off our options @settings_class_options = options # Add this class to the settings registry Settings.classes << self # Add in support for settings for this class extend ClassMethods # Create our value store class_settings_reset! end # Create a builder and do the right thing based on passed args builder = Settings::Builder.new(@settings_class_root) builder.define(&block) if block builder end