Class: RailsSettings::Settings

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

Direct Known Subclasses

CachedSettings, ScopedSettings

Defined Under Namespace

Classes: SettingNotFound

Constant Summary

@@defaults =
SettingsDefaults::DEFAULTS.with_indifferent_access

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) [](var_name)

get a setting value by [] notation



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

def self.[](var_name)
  if var = object(var_name)
    var.value
  elsif @@defaults[var_name.to_s]
    @@defaults[var_name.to_s]
  else
    nil
  end
end

+ (Object) []=(var_name, value)

set a setting value by [] notation



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

def self.[]=(var_name, value)
  var_name = var_name.to_s

  record = object(var_name) || thing_scoped.new(:var => var_name)
  record.value = value
  record.save!

  value
end

+ (Object) destroy(var_name)

destroy the specified settings record



33
34
35
36
37
38
39
40
41
42
# File 'lib/rails-settings/settings.rb', line 33

def self.destroy(var_name)
  var_name = var_name.to_s
  obj = object(var_name)
  unless obj.nil?
    obj.destroy
    true
  else
    raise SettingNotFound, "Setting variable \"#{var_name}\" not found"
  end
end

+ (Object) get_all(starting_with = nil)

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



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

def self.get_all(starting_with = nil)
  vars = thing_scoped.select("var,value")
  if starting_with
    vars = vars.where("var LIKE '#{starting_with}%'")
  end

  result = {}
  vars.each do |record|
    result[record.var] = record.value
  end
  result.with_indifferent_access
end

+ (Object) merge!(var_name, hash_value)

Raises:

  • (ArgumentError)


87
88
89
90
91
92
93
94
95
96
97
# File 'lib/rails-settings/settings.rb', line 87

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

+ (Object) method_missing(method, *args)

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



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rails-settings/settings.rb', line 17

def self.method_missing(method, *args)
  method_name = method.to_s
  super(method, *args)
rescue NoMethodError
  #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

+ (Object) object(var_name)



99
100
101
# File 'lib/rails-settings/settings.rb', line 99

def self.object(var_name)
  thing_scoped.where(:var => var_name.to_s).first
end

+ (Object) thing_scoped



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

def self.thing_scoped
  unscoped.where("thing_type is NULL and thing_id is NULL")
end

+ (Object) where(sql = nil)



58
59
60
61
62
63
# File 'lib/rails-settings/settings.rb', line 58

def self.where(sql = nil)
  if sql
    vars = thing_scoped.where(sql)
  end
  vars
end

Instance Method Details

- (Object) value

get the value field, YAML decoded



104
105
106
# File 'lib/rails-settings/settings.rb', line 104

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

- (Object) value=(new_value)

set the value field, YAML encoded



109
110
111
# File 'lib/rails-settings/settings.rb', line 109

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