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.
37 38 39 40 |
# File 'lib/settler.rb', line 37 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.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/settler.rb', line 15 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 = File.exist?(source) ? YAML.load(ERB.new(File.read(source)).result).to_hash : {} self.config = config[namespace] || {} if namespace Setting.unscoped do self.config.each do |key, attributes| setting = Setting.where(:key => key).first || Setting.create(:key => key) do |s| s.label = attributes['label'] 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.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/settler.rb', line 63 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.
91 92 93 94 |
# File 'lib/settler.rb', line 91 def namespace @@namespace ||= Rails.env if defined?(Rails) @@namespace end |
.settings(options = {}) ⇒ Object
Returns an array of all setting keys
43 44 45 46 |
# File 'lib/settler.rb', line 43 def settings( = {}) Settler.load! if config.nil? Setting.order([:order]).select(:key).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.
84 85 86 87 |
# File 'lib/settler.rb', line 84 def source @@source ||= File.join(Rails.root, 'config', 'settler.yml') if defined?(Rails) @@source end |