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
81 82 83 84 85 86 87 88 89 |
# File 'lib/rails-settings/settings.rb', line 81 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
92 93 94 95 96 97 98 |
# File 'lib/rails-settings/settings.rb', line 92 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)
69 70 71 72 73 74 75 76 77 78 |
# File 'lib/rails-settings/settings.rb', line 69 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
63 64 65 66 |
# File 'lib/rails-settings/settings.rb', line 63 def self.delete_all(conditions = nil) cache.clear super end |
.destroy(var_name) ⇒ Object
destroy the specified settings record
52 53 54 55 56 57 58 59 60 61 |
# File 'lib/rails-settings/settings.rb', line 52 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
100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/rails-settings/settings.rb', line 100 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 |
# 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 ActiveRecord::Base.connected? and 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!
131 132 133 |
# File 'lib/rails-settings/settings.rb', line 131 def self.reload # :nodoc: self end |
.target(var_name) ⇒ Object
112 113 114 |
# File 'lib/rails-settings/settings.rb', line 112 def self.target(var_name) target_scoped.find_by_var(var_name.to_s) end |
.target_id ⇒ Object
135 136 137 |
# File 'lib/rails-settings/settings.rb', line 135 def self.target_id nil end |
.target_scoped ⇒ Object
126 127 128 |
# File 'lib/rails-settings/settings.rb', line 126 def self.target_scoped Settings.scoped_by_target_type_and_target_id(target_type, target_id) end |
.target_type ⇒ Object
139 140 141 |
# File 'lib/rails-settings/settings.rb', line 139 def self.target_type nil end |
Instance Method Details
#value ⇒ Object
get the value field, YAML decoded
117 118 119 |
# File 'lib/rails-settings/settings.rb', line 117 def value YAML::load(self[:value]) end |
#value=(new_value) ⇒ Object
set the value field, YAML encoded
122 123 124 |
# File 'lib/rails-settings/settings.rb', line 122 def value=(new_value) self[:value] = new_value.to_yaml end |