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.



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.full_messages.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

.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.



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



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

.typecast_for(key) ⇒ Object

Returns the typecast for a setting (if any).



56
57
58
59
# File 'lib/settler.rb', line 56

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.



49
50
51
52
53
# File 'lib/settler.rb', line 49

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