Class: Settler
- Inherits:
-
Object
- Object
- Settler
- Defined in:
- lib/settler.rb
Overview
Settler loads and manages application wide settings and provides an interface for retrieving settings. The Settler object cannot be instantiated; all functionality is available on class level.
Class Method Summary collapse
-
.[](key) ⇒ Object
Shortcut method for quickly retrieving settings.
-
.load! ⇒ Object
Loads the settler configuration from settler.yml and defines methods for retrieving the found settings.
-
.method_missing(name, *args, &block) ⇒ Object
Overrides the normal method_missing to return nil for non-existant settings.
-
.namespace ⇒ Object
Returns the namespace of the configuration to look for settings.
-
.settings(options = {}) ⇒ Object
Returns an array of all setting keys.
-
.source ⇒ Object
Returns the file location of the settler.yml configuration file.
-
.typecast_for(key) ⇒ Object
Returns the typecast for a setting (if any).
-
.validations_for(key) ⇒ Object
Returns a list of validations to perform on a setting.
Class Method Details
.[](key) ⇒ Object
Shortcut method for quickly retrieving settings. This method directly returns the setting’s value instead of the Setting instance.
36 37 38 39 |
# File 'lib/settler.rb', line 36 def [](key) Settler.load! if config.nil? Setting.find_by_key(key.to_s).try(:value) end |
.load! ⇒ Object
Loads the settler configuration from settler.yml and defines methods for retrieving the found settings.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/settler.rb', line 16 def load! raise "Source settler.yml not set. Please create one and set it by using Settler.source = <file>. When using Rails, please create a settler.yml file in the config directory." unless source self.config = YAML.load(ERB.new(File.read(source)).result).to_hash self.config = config[namespace] if namespace self.config.each do |key, attributes| Setting.without_default_scope do setting = Setting.find_or_create_by_key(:key => key) do |s| s.alt = attributes['alt'] s.value = attributes['value'] s.editable = attributes['editable'] s.deletable = attributes['deletable'] end p "[Settler] Validation failed for setting '#{setting.key}': #{setting.errors..to_sentence}" if !setting.valid? end end Setting.all.each{ |s| key = s.key; Settler.class.send(:define_method, key){ Setting.find_by_key(key) } } end |
.method_missing(name, *args, &block) ⇒ Object
Overrides the normal method_missing to return nil for non-existant settings. The behaviour of this method depends on the boolean attributes raise_missing and report_missing.
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/settler.rb', line 62 def method_missing(name, *args, &block) method_name = name.to_s return super if Settler.private_methods(false).include?(method_name) if config.nil? Settler.load! return send(method_name) end if report_missing puts "[Settler] Warning: setting missing: #{method_name}" Rails.logger.warn("[Settler] setting missing: #{method_name}") if defined?(Rails) end raise "[Settler] setting missing: #{method_name}" if raise_missing nil end |
.namespace ⇒ Object
Returns the namespace of the configuration to look for settings. Defaults to the current Rails environment if the library is included in a Rails app.
90 91 92 93 |
# File 'lib/settler.rb', line 90 def namespace @@namespace ||= Rails.env if defined?(Rails) @@namespace end |
.settings(options = {}) ⇒ Object
Returns an array of all setting keys
42 43 44 45 |
# File 'lib/settler.rb', line 42 def settings( = {}) Settler.load! if config.nil? Setting.all(:order => [:order]).map(&:key) end |
.source ⇒ Object
Returns the file location of the settler.yml configuration file. Defaults to the ‘config/settler.yml’ if the library is included in a Rails app.
83 84 85 86 |
# File 'lib/settler.rb', line 83 def source @@source ||= File.join(Rails.root, 'config', 'settler.yml') if defined?(Rails) @@source end |