Class: Settler

Inherits:
Object
  • Object
show all
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

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

.namespaceObject

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(options = {})
  Settler.load! if config.nil?
  Setting.all(:order => options[:order]).map(&:key)
end

.sourceObject

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

.typecast_for(key) ⇒ Object

Returns the typecast for a setting (if any).



52
53
54
55
# File 'lib/settler.rb', line 52

def typecast_for(key)
  Settler.load! if config.nil?
  (setting = config[key.to_s]) ? setting['typecast'] || nil : nil
end

.validations_for(key) ⇒ Object

Returns a list of validations to perform on a setting.



45
46
47
48
49
# File 'lib/settler.rb', line 45

def validations_for(key)
  setting_validations = {}
  Settler.load! if config.nil?
  (setting = config[key.to_s]) ? setting['validations'] || {} : {}
end