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
- @@defaults =
SettingsDefaults::DEFAULTS.with_indifferent_access
Class Method Summary (collapse)
-
+ (Object) [](var_name)
get a setting value by [] notation.
-
+ (Object) []=(var_name, value)
set a setting value by [] notation.
-
+ (Object) all(starting_with = nil)
retrieve all settings as a hash (optionally starting with a given namespace).
-
+ (Object) destroy(var_name)
destroy the specified settings record.
- + (Object) merge!(var_name, hash_value)
-
+ (Object) method_missing(method, *args)
get or set a variable with the variable as the called method.
- + (Object) object(var_name)
- + (Object) thing_scoped
- + (Object) where(sql = nil)
Instance Method Summary (collapse)
-
- (Object) value
get the value field, YAML decoded.
-
- (Object) value=(new_value)
set the value field, YAML encoded.
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) 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.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) destroy(var_name)
destroy the specified settings record
34 35 36 37 38 39 40 41 42 |
# File 'lib/rails-settings/settings.rb', line 34 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 |
+ (Object) merge!(var_name, hash_value)
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
18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rails-settings/settings.rb', line 18 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 |