Class: Settings
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Settings
- Defined in:
- lib/rails-settings/settings.rb
Direct Known Subclasses
Defined Under Namespace
Classes: SettingNotFound
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).
- .cache_key(var_name) ⇒ Object
- .delete_all(conditions = nil) ⇒ Object
-
.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
83 84 85 86 87 88 89 90 91 |
# File 'lib/rails-settings/settings.rb', line 83 def self.[](var_name) cache.fetch(cache_key(var_name), ) do if (var = target(var_name)).present? var.value else ENV[var_name.to_s.upcase] || defaults[var_name.to_s] end end end |
.[]=(var_name, value) ⇒ Object
set a setting value by [] notation
94 95 96 97 98 99 100 |
# File 'lib/rails-settings/settings.rb', line 94 def self.[]=(var_name, value) record = target_scoped.find_or_initialize_by_var(var_name.to_s) record.value = value record.save! cache.write(cache_key(var_name), value, ) value end |
.all(starting_with = nil) ⇒ Object
retrieve all settings as a hash (optionally starting with a given namespace)
71 72 73 74 75 76 77 78 79 80 |
# File 'lib/rails-settings/settings.rb', line 71 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 |
.cache_key(var_name) ⇒ Object
15 16 17 |
# File 'lib/rails-settings/settings.rb', line 15 def self.cache_key(var_name) [target_id, target_type, var_name].compact.join("::") end |
.delete_all(conditions = nil) ⇒ Object
65 66 67 68 |
# File 'lib/rails-settings/settings.rb', line 65 def self.delete_all(conditions = nil) cache.clear super end |
.destroy(var_name) ⇒ Object
destroy the specified settings record
54 55 56 57 58 59 60 61 62 63 |
# File 'lib/rails-settings/settings.rb', line 54 def self.destroy(var_name) var_name = var_name.to_s begin target(var_name).destroy cache.delete(cache_key(var_name)) true rescue NoMethodError raise SettingNotFound, "Setting variable \"#{var_name}\" not found" end end |
.merge!(var_name, hash_value) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rails-settings/settings.rb', line 102 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/rails-settings/settings.rb', line 25 def self.method_missing(method, *args) if self.respond_to?(method) super else method_name = method.to_s #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 #default behavior if self.connection.tables.include?(self.table_name) self[method_name] else #ENV is table not created warn "#{self.table_name} table does not exist, fallback to ENV[#{method_name.upcase}]" ENV[method_name.upcase] end end end end |
.reload ⇒ Object
Deprecated!
133 134 135 |
# File 'lib/rails-settings/settings.rb', line 133 def self.reload # :nodoc: self end |
.target(var_name) ⇒ Object
114 115 116 |
# File 'lib/rails-settings/settings.rb', line 114 def self.target(var_name) target_scoped.find_by_var(var_name.to_s) end |
.target_id ⇒ Object
137 138 139 |
# File 'lib/rails-settings/settings.rb', line 137 def self.target_id nil end |
.target_scoped ⇒ Object
128 129 130 |
# File 'lib/rails-settings/settings.rb', line 128 def self.target_scoped Settings.scoped_by_target_type_and_target_id(target_type, target_id) end |
.target_type ⇒ Object
141 142 143 |
# File 'lib/rails-settings/settings.rb', line 141 def self.target_type nil end |
Instance Method Details
#value ⇒ Object
get the value field, YAML decoded
119 120 121 |
# File 'lib/rails-settings/settings.rb', line 119 def value YAML::load(self[:value]) end |
#value=(new_value) ⇒ Object
set the value field, YAML encoded
124 125 126 |
# File 'lib/rails-settings/settings.rb', line 124 def value=(new_value) self[:value] = new_value.to_yaml end |