Class: RailsAdminSettings::Namespaced
- Inherits:
- BasicObject
- Defined in:
- lib/rails_admin_settings/namespaced.rb
Overview
we are inheriting from BasicObject so we don’t get a bunch of methods from Kernel or Object
Constant Summary collapse
- DELEGATE =
[:puts, :p, :block_given?].freeze
Instance Attribute Summary collapse
-
#fallback ⇒ Object
Returns the value of attribute fallback.
-
#loaded ⇒ Object
readonly
Returns the value of attribute loaded.
-
#mutex ⇒ Object
readonly
Returns the value of attribute mutex.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#ns_mutex ⇒ Object
readonly
Returns the value of attribute ns_mutex.
-
#settings ⇒ Object
Returns the value of attribute settings.
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #destroy!(key) ⇒ Object
- #destroy_all! ⇒ Object
- #enabled?(key, options = {}) ⇒ Boolean
-
#get(key, options = {}, &block) ⇒ Object
returns setting object.
-
#getnc(key) ⇒ Object
returns setting object.
-
#initialize(name) ⇒ Namespaced
constructor
A new instance of Namespaced.
- #inspect ⇒ Object
- #load! ⇒ Object
-
#method_missing(key, *args, &block) ⇒ Object
returns processed setting value.
- #nil? ⇒ Boolean
- #pretty_inspect ⇒ Object
- #set(key, value = nil, options = {}) ⇒ Object
- #unload! ⇒ Object
- #write_to_database(key, options) ⇒ Object
Constructor Details
#initialize(name) ⇒ Namespaced
Returns a new instance of Namespaced.
11 12 13 14 15 16 17 18 |
# File 'lib/rails_admin_settings/namespaced.rb', line 11 def initialize(name) self.settings = {} @mutex = ::Mutex.new @ns_mutex = ::Mutex.new @loaded = false @locked = false @name = name end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(key, *args, &block) ⇒ Object
returns processed setting value
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/rails_admin_settings/namespaced.rb', line 201 def method_missing(key, *args, &block) return ::Kernel.send(key, *args, &block) if DELEGATE.include?(key) key = key.to_s if key.end_with?('_enabled?') key = key[0..-10] v = get(key) if v.nil? set(key, '').enabled else v.enabled end elsif key.end_with?('_enabled=') key = key[0..-10] v = get(key) if ::RailsAdminSettings.mongoid? if ::Mongoid::VERSION >= "4.0.0" v.set(enabled: args.first) else v.set("enabled", args.first) end else v.enabled = args.first v.save! end v.enabled elsif key.end_with?('=') key = key[0..-2] = args[1] || {} value = args.first set(key, value, ).val else v = get(key, args.first || {}, &block) if v.nil? '' else v.val end end end |
Instance Attribute Details
#fallback ⇒ Object
Returns the value of attribute fallback.
8 9 10 |
# File 'lib/rails_admin_settings/namespaced.rb', line 8 def fallback @fallback end |
#loaded ⇒ Object (readonly)
Returns the value of attribute loaded.
9 10 11 |
# File 'lib/rails_admin_settings/namespaced.rb', line 9 def loaded @loaded end |
#mutex ⇒ Object (readonly)
Returns the value of attribute mutex.
9 10 11 |
# File 'lib/rails_admin_settings/namespaced.rb', line 9 def mutex @mutex end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/rails_admin_settings/namespaced.rb', line 9 def name @name end |
#ns_mutex ⇒ Object (readonly)
Returns the value of attribute ns_mutex.
9 10 11 |
# File 'lib/rails_admin_settings/namespaced.rb', line 9 def ns_mutex @ns_mutex end |
#settings ⇒ Object
Returns the value of attribute settings.
8 9 10 |
# File 'lib/rails_admin_settings/namespaced.rb', line 8 def settings @settings end |
Instance Method Details
#[](key) ⇒ Object
179 180 181 |
# File 'lib/rails_admin_settings/namespaced.rb', line 179 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
176 177 178 |
# File 'lib/rails_admin_settings/namespaced.rb', line 176 def []=(key, value) set(key, value) end |
#destroy!(key) ⇒ Object
183 184 185 186 187 188 189 190 |
# File 'lib/rails_admin_settings/namespaced.rb', line 183 def destroy!(key) load! key = key.to_s mutex.synchronize do ::RailsAdminSettings::Setting.where(ns: @name, key: key).destroy_all @settings.delete(key) end end |
#destroy_all! ⇒ Object
192 193 194 195 196 197 198 |
# File 'lib/rails_admin_settings/namespaced.rb', line 192 def destroy_all! mutex.synchronize do ::RailsAdminSettings::Setting.where(ns: @name).destroy_all @loaded = false @settings = {} end end |
#enabled?(key, options = {}) ⇒ Boolean
172 173 174 |
# File 'lib/rails_admin_settings/namespaced.rb', line 172 def enabled?(key, = {}) get(key, ).enabled? end |
#get(key, options = {}, &block) ⇒ Object
returns setting object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rails_admin_settings/namespaced.rb', line 49 def get(key, = {}, &block) [:loadable] = true unless [:loadable] == false load! if [:loadable] key = key.to_s mutex.synchronize do @locked = true _loadable = [:loadable] unless _loadable v = ::RailsAdminSettings::Setting.ns(name).where(key: key).first if v unless [:cache_keys_str].present? _cache_keys = .delete(:cache_keys) _cache_keys ||= .delete(:cache_key) if _cache_keys.nil? [:cache_keys_str] = "" # if _cache # options[:cache_keys_str] = name.underscore # end else if _cache_keys.is_a?(::Array) [:cache_keys_str] = _cache_keys.map { |k| k.to_s.strip }.join(" ") else [:cache_keys_str] = _cache_keys.to_s.strip end end end .delete(:cache_keys) .delete(:cache_key) _old_cache_keys = [:cache_keys_str].strip.split(" ") # options[:cache_keys_str] = ("#{v.cache_keys_str} #{(options[:cache_keys_str] || "")}".strip.split(" ")).uniq # options[:cache_keys_str] = ("#{v.cache_keys_str.join(" ")} #{(options[:cache_keys_str] || "")}".strip.split(" ")).uniq [:cache_keys_str] = (_old_cache_keys + v.cache_keys).uniq [:overwrite] = true unless ([:cache_keys_str] - _old_cache_keys).blank? [:cache_keys_str] = [:cache_keys_str].map { |k| k.to_s.strip }.join(" ") end else v = @settings[key] end _overwrite = [:overwrite] if v.nil? or _overwrite if v.nil? unless @fallback.nil? || @fallback == @name v = ::Settings.ns(@fallback).getnc(key) end end if v.nil? or _overwrite if block begin v = set(key, yield, ) rescue Exception => ex # puts "WTF" # puts ex.inspect end else v = set(key, [:default], ) end end end @locked = false v end end |
#getnc(key) ⇒ Object
returns setting object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/rails_admin_settings/namespaced.rb', line 115 def getnc(key) load! ret = mutex.synchronize do self.settings[key] end unless ret ret = ::RailsAdminSettings::Setting.ns(name).where(key: key).first end ret end |
#inspect ⇒ Object
23 24 25 |
# File 'lib/rails_admin_settings/namespaced.rb', line 23 def inspect "#<RailsAdminSettings::Namespaced name: #{@name.inspect}, fallback: #{@fallback.inspect}, loaded: #{@loaded}>".freeze end |
#load! ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rails_admin_settings/namespaced.rb', line 30 def load! mutex.synchronize do return if loaded @loaded = true @settings = {} ::RailsAdminSettings::Setting.ns(@name).loadable.each do |setting| @settings[setting.key] = setting end end end |
#nil? ⇒ Boolean
20 21 22 |
# File 'lib/rails_admin_settings/namespaced.rb', line 20 def nil? false end |
#pretty_inspect ⇒ Object
26 27 28 |
# File 'lib/rails_admin_settings/namespaced.rb', line 26 def pretty_inspect inspect end |
#set(key, value = nil, options = {}) ⇒ Object
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/rails_admin_settings/namespaced.rb', line 126 def set(key, value = nil, = {}) load! unless @locked if [:loadable] key = key.to_s .symbolize_keys! if .key?(:cache) _cache = .delete(:cache) else _cache = (name != ::Settings.ns_default) end if ![:type].nil? && [:type] == 'yaml' && !value.nil? if value.class.name != 'String' value = value.to_yaml end end unless [:cache_keys_str].present? _cache_keys = .delete(:cache_keys) _cache_keys ||= .delete(:cache_key) if _cache_keys.nil? # if _cache # options[:cache_keys_str] = name.underscore # end else if _cache_keys.is_a?(::Array) [:cache_keys_str] = _cache_keys.map { |k| k.to_s.strip }.join(" ") else [:cache_keys_str] = _cache_keys.to_s.strip end end end .delete(:cache_keys) .delete(:cache_key) .merge!(value: value) if @locked ret = write_to_database(key, ) else mutex.synchronize do ret = write_to_database(key, ) end end ret end |
#unload! ⇒ Object
41 42 43 44 45 46 |
# File 'lib/rails_admin_settings/namespaced.rb', line 41 def unload! mutex.synchronize do @loaded = false @settings = {} end end |
#write_to_database(key, options) ⇒ Object
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/rails_admin_settings/namespaced.rb', line 241 def write_to_database(key, ) [:kind] = [:kind].to_s if [:kind] is_file = ![:kind].nil? && ([:kind] == 'image' || [:kind] == 'file') is_array = ![:kind].nil? && ([:kind] == 'array') is_hash = ![:kind].nil? && ([:kind] == 'hash') if is_file [:raw] = '' file = [:value] elsif is_array [:raw] = '' [:raw_array] = [:value] if [:value] elsif is_hash [:raw] = '' [:raw_hash] = [:value] if [:value] else [:raw] = [:value] if [:value] end .delete(:value) .delete(:default) [:ns] = @name if @settings[key].nil? if .delete(:overwrite) v = ::RailsAdminSettings::Setting.ns([:ns]).where(key: key).first if v opts = .dup v.update_attributes!(opts) end end if v.nil? v = ::RailsAdminSettings::Setting.create(.merge(key: key)) if !v.persisted? if v.errors[:key].any? v = ::RailsAdminSettings::Setting.where(key: key).first if v.nil? ::Kernel.raise ::RailsAdminSettings::PersistenceException, 'Fatal: error in key and not in DB' end else ::Kernel.raise ::RailsAdminSettings::PersistenceException, v.errors..join(',') end end end if [:loadable] @settings[key] = v else return v end else opts = .dup if [:overwrite] == false && !@settings[key].value.blank? opts.delete(:raw) opts.delete(:raw_array) opts.delete(:raw_hash) opts.delete(:value) opts.delete(:enabled) end opts.delete(:overwrite) @settings[key].update_attributes!(opts) end if is_file and [:loadable] and @settings[key] if [:overwrite] != false || !@settings[key].file? @settings[key].file = file @settings[key].save! end end @settings[key] end |