Class: RailsSettings::Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RailsSettings::Settings
- Defined in:
- lib/rails-settings/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.
- .object(var_name) ⇒ Object
- .thing_scoped ⇒ 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
59 60 61 62 63 64 65 66 67 |
# File 'lib/rails-settings/settings.rb', line 59 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 |
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation
70 71 72 73 74 75 76 77 78 |
# File 'lib/rails-settings/settings.rb', line 70 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 |
.all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rails-settings/settings.rb', line 47 def self.all(starting_with=nil) = starting_with ? { :conditions => "var LIKE '#{starting_with}%'"} : {} vars = thing_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
36 37 38 39 40 41 42 43 44 |
# File 'lib/rails-settings/settings.rb', line 36 def self.destroy(var_name) var_name = var_name.to_s if self[var_name] object(var_name).destroy true else raise SettingNotFound, "Setting variable \"#{var_name}\" not found" end end |
.merge!(var_name, hash_value) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rails-settings/settings.rb', line 80 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
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# 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(var_name) ⇒ Object
92 93 94 |
# File 'lib/rails-settings/settings.rb', line 92 def self.object(var_name) thing_scoped.find_by_var(var_name.to_s) end |
.thing_scoped ⇒ Object
106 107 108 |
# File 'lib/rails-settings/settings.rb', line 106 def self.thing_scoped self.scoped_by_thing_type_and_thing_id(nil, nil) end |
Instance Method Details
#value ⇒ Object
get the value field, YAML decoded
97 98 99 |
# File 'lib/rails-settings/settings.rb', line 97 def value YAML::load(self[:value]) end |
#value=(new_value) ⇒ Object
set the value field, YAML encoded
102 103 104 |
# File 'lib/rails-settings/settings.rb', line 102 def value=(new_value) self[:value] = new_value.to_yaml end |