Class: Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Settings
- Defined in:
- lib/settings.rb
Direct Known Subclasses
Defined Under Namespace
Classes: SettingNotFound
Constant Summary collapse
- @@defaults =
SettingsDefaults::DEFAULTS.with_indifferent_access
Class Method Summary collapse
-
.[](var_name) ⇒ Object
get a setting value by [] notation.
-
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation.
-
.all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace).
-
.destroy(var_name) ⇒ Object
destroy the specified settings record.
- .merge!(var_name, hash_value) ⇒ Object
-
.method_missing(method, *args) ⇒ Object
get or set a variable with the variable as the called method.
-
.reload ⇒ Object
Deprecated!.
- .target(var_name) ⇒ Object
- .target_id ⇒ Object
- .target_scoped ⇒ Object
- .target_type ⇒ Object
Instance Method Summary collapse
-
#value ⇒ Object
get the value field, YAML decoded.
-
#value=(new_value) ⇒ Object
set the value field, YAML encoded.
Class Method Details
.[](var_name) ⇒ Object
get a setting value by [] notation
55 56 57 58 59 60 61 |
# File 'lib/settings.rb', line 55 def self.[](var_name) if var = target(var_name) var.value else @@defaults[var_name.to_s] end end |
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation
64 65 66 67 68 69 70 71 72 |
# File 'lib/settings.rb', line 64 def self.[]=(var_name, value) var_name = var_name.to_s record = target(var_name) || target_scoped.new(:var => var_name) record.value = value record.save! value end |
.all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
43 44 45 46 47 48 49 50 51 52 |
# File 'lib/settings.rb', line 43 def self.all(starting_with=nil) = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {} vars = target_scoped.find(:all, {:select => 'var, value'}.merge()) result = {} vars.each do |record| result[record.var] = record.value end result.with_indifferent_access end |
.destroy(var_name) ⇒ Object
destroy the specified settings record
32 33 34 35 36 37 38 39 40 |
# File 'lib/settings.rb', line 32 def self.destroy(var_name) var_name = var_name.to_s if self[var_name] target(var_name).destroy true else raise SettingNotFound, "Setting variable \"#{var_name}\" not found" end end |
.merge!(var_name, hash_value) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/settings.rb', line 74 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
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/settings.rb', line 13 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 |
.reload ⇒ Object
Deprecated!
105 106 107 |
# File 'lib/settings.rb', line 105 def self.reload # :nodoc: self end |
.target(var_name) ⇒ Object
86 87 88 |
# File 'lib/settings.rb', line 86 def self.target(var_name) target_scoped.find_by_var(var_name.to_s) end |
.target_id ⇒ Object
109 110 111 |
# File 'lib/settings.rb', line 109 def self.target_id nil end |
.target_scoped ⇒ Object
100 101 102 |
# File 'lib/settings.rb', line 100 def self.target_scoped Settings.scoped_by_target_type_and_target_id(target_type, target_id) end |
.target_type ⇒ Object
113 114 115 |
# File 'lib/settings.rb', line 113 def self.target_type nil end |
Instance Method Details
#value ⇒ Object
get the value field, YAML decoded
91 92 93 |
# File 'lib/settings.rb', line 91 def value YAML::load(self[:value]) end |
#value=(new_value) ⇒ Object
set the value field, YAML encoded
96 97 98 |
# File 'lib/settings.rb', line 96 def value=(new_value) self[:value] = new_value.to_yaml end |