Class: Settings

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rails-settings/settings.rb

Direct Known Subclasses

ScopedSettings

Defined Under Namespace

Classes: SettingNotFound

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](var_name) ⇒ Object

get a setting value by [] notation



77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/rails-settings/settings.rb', line 77

def self.[](var_name)
  cache.fetch(cache_key(var_name), cache_options) do
    if var = target(var_name)
      var.value
    else
      if target_id.nil?
        @@defaults[var_name.to_s]
      else
        target_type.constantize.settings[var_name.to_s]
      end
    end
  end
end

.[]=(var_name, value) ⇒ Object

set a setting value by [] notation



92
93
94
95
96
97
98
# File 'lib/rails-settings/settings.rb', line 92

def self.[]=(var_name, value)
  record = target_scoped.find_or_initialize_by_var(var_name.to_s)
  record.value = value
  record.save!
  cache.write(cache_key(var_name), value, cache_options)
  value
end

.all(starting_with = nil) ⇒ Object

retrieve all settings as a hash (optionally starting with a given namespace)



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rails-settings/settings.rb', line 63

def self.all(starting_with=nil)
  options = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {}
  vars = target_scoped.find(:all, {:select => 'var, value'}.merge(options))
  
  result = {}
  vars.each do |record|
    result[record.var] = record.value
  end
  defaults = @@defaults.select{ |k, v| k =~ /^#{starting_with}/ }
  defaults = Hash[defaults] if defaults.is_a?(Array)
  defaults.merge(result).with_indifferent_access
end

.cache_key(var_name) ⇒ Object



15
16
17
# File 'lib/rails-settings/settings.rb', line 15

def self.cache_key(var_name)
  [target_id, target_type, var_name].compact.join("::")
end

.delete_all(conditions = nil) ⇒ Object



57
58
59
60
# File 'lib/rails-settings/settings.rb', line 57

def self.delete_all(conditions = nil)
  cache.clear
  super
end

.destroy(var_name) ⇒ Object

destroy the specified settings record



46
47
48
49
50
51
52
53
54
55
# File 'lib/rails-settings/settings.rb', line 46

def self.destroy(var_name)
  var_name = var_name.to_s
  begin
    target(var_name).destroy
    cache.delete(cache_key(var_name))
    true
  rescue NoMethodError
    raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
  end
end

.merge!(var_name, hash_value) ⇒ Object

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
107
108
109
110
# File 'lib/rails-settings/settings.rb', line 100

def self.merge!(var_name, hash_value)
  raise ArgumentError unless hash_value.is_a?(Hash)
  
  old_value = self[var_name] || {}
  raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
  
  new_value = old_value.merge(hash_value)
  self[var_name] = new_value if new_value != old_value
  
  new_value
end

.method_missing(method, *args) ⇒ Object

get or set a variable with the variable as the called method



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/rails-settings/settings.rb', line 25

def self.method_missing(method, *args)
  if self.respond_to?(method)
    super
  else
    method_name = method.to_s
  
    #set a value for a variable
    if method_name =~ /=$/
      var_name = method_name.gsub('=', '')
      value = args.first
      self[var_name] = value
  
    #retrieve a value
    else
      self[method_name]
    
    end
  end
end

.reloadObject

Deprecated!



131
132
133
# File 'lib/rails-settings/settings.rb', line 131

def self.reload # :nodoc:
  self
end

.target(var_name) ⇒ Object



112
113
114
# File 'lib/rails-settings/settings.rb', line 112

def self.target(var_name)
  target_scoped.find_by_var(var_name.to_s)
end

.target_idObject



135
136
137
# File 'lib/rails-settings/settings.rb', line 135

def self.target_id
  nil
end

.target_scopedObject



126
127
128
# File 'lib/rails-settings/settings.rb', line 126

def self.target_scoped
  Settings.scoped_by_target_type_and_target_id(target_type, target_id)
end

.target_typeObject



139
140
141
# File 'lib/rails-settings/settings.rb', line 139

def self.target_type
  nil
end

Instance Method Details

#valueObject

get the value field, YAML decoded



117
118
119
# File 'lib/rails-settings/settings.rb', line 117

def value
  YAML::load(self[:value])
end

#value=(new_value) ⇒ Object

set the value field, YAML encoded



122
123
124
# File 'lib/rails-settings/settings.rb', line 122

def value=(new_value)
  self[:value] = new_value.to_yaml
end