Class: RubySettings::Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- RubySettings::Settings
- Defined in:
- lib/ruby-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.
-
.destroy(var_name) ⇒ Object
destroy the specified settings record.
-
.get_all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace).
- .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
- .where(sql = nil) ⇒ 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
74 75 76 |
# File 'lib/ruby-settings/settings.rb', line 74 def [](var_name) object(var_name).try(:value) || @@defaults[var_name.to_s] end |
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation
79 80 81 82 83 84 85 86 87 |
# File 'lib/ruby-settings/settings.rb', line 79 def []=(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 |
.destroy(var_name) ⇒ Object
destroy the specified settings record
45 46 47 48 49 50 51 52 |
# File 'lib/ruby-settings/settings.rb', line 45 def destroy(var_name) var_name = var_name.to_s obj = object(var_name) raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil? obj.destroy true end |
.get_all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/ruby-settings/settings.rb', line 55 def get_all(starting_with = nil) vars = thing_scoped.select('var, value') vars = vars.where("var LIKE '#{starting_with}%'") if starting_with result = {} vars.each do |record| result[record.var] = record.value end result.merge! @@defaults.slice(*(@@defaults.keys - result.keys)) result.with_indifferent_access end |
.merge!(var_name, hash_value) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/ruby-settings/settings.rb', line 89 def 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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ruby-settings/settings.rb', line 29 def method_missing(method, *args) method_name = method.to_s super(method, *args) rescue NoMethodError # set a value for a variable if method_name[-1] == '=' var_name = method_name.sub('=', '') value = args.first self[var_name] = value else # retrieve a value self[method_name] end end |
.object(var_name) ⇒ Object
101 102 103 |
# File 'lib/ruby-settings/settings.rb', line 101 def object(var_name) thing_scoped.where(var: var_name.to_s).first end |
.thing_scoped ⇒ Object
105 106 107 |
# File 'lib/ruby-settings/settings.rb', line 105 def thing_scoped unscoped.where('thing_type is NULL and thing_id is NULL') end |
.where(sql = nil) ⇒ Object
68 69 70 71 |
# File 'lib/ruby-settings/settings.rb', line 68 def where(sql = nil) vars = thing_scoped.where(sql) if sql vars end |
Instance Method Details
#value ⇒ Object
get the value field, YAML decoded
18 19 20 |
# File 'lib/ruby-settings/settings.rb', line 18 def value YAML.load(self[:value]) end |
#value=(new_value) ⇒ Object
set the value field, YAML encoded
23 24 25 |
# File 'lib/ruby-settings/settings.rb', line 23 def value=(new_value) self[:value] = new_value.to_yaml end |