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.
33 34 35 36 |
# File 'lib/settler.rb', line 33 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 |
# 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.find_or_create_by_key(attributes.only(:alt, :value).merge(:key => key)) do |s| s.editable = attributes['editable'] s.deletable = attributes['deletable'] end 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.
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/settler.rb', line 59 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.
87 88 89 90 |
# File 'lib/settler.rb', line 87 def namespace @@namespace ||= Rails.env if defined?(Rails) @@namespace end |
.settings(options = {}) ⇒ Object
Returns an array of all setting keys
39 40 41 42 |
# File 'lib/settler.rb', line 39 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.
80 81 82 83 |
# File 'lib/settler.rb', line 80 def source @@source ||= File.join(Rails.root, 'config', 'settler.yml') if defined?(Rails) @@source end |