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

Instance Method Summary collapse

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]
    options = args[1] || {}
    value = args.first
    set(key, value, options).val
  else
    v = get(key, args.first || {}, &block)
    if v.nil?
      ''
    else
      v.val
    end
  end
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



8
9
10
# File 'lib/rails_admin_settings/namespaced.rb', line 8

def fallback
  @fallback
end

#loadedObject (readonly)

Returns the value of attribute loaded.



9
10
11
# File 'lib/rails_admin_settings/namespaced.rb', line 9

def loaded
  @loaded
end

#mutexObject (readonly)

Returns the value of attribute mutex.



9
10
11
# File 'lib/rails_admin_settings/namespaced.rb', line 9

def mutex
  @mutex
end

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/rails_admin_settings/namespaced.rb', line 9

def name
  @name
end

#ns_mutexObject (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

#settingsObject

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

Returns:

  • (Boolean)


172
173
174
# File 'lib/rails_admin_settings/namespaced.rb', line 172

def enabled?(key, options = {})
  get(key, options).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, options = {}, &block)
  options[:loadable] = true unless options[:loadable] == false
  load! if options[:loadable]
  key = key.to_s
  mutex.synchronize do
    @locked = true
    _loadable = options[:loadable]
    unless _loadable
      v = ::RailsAdminSettings::Setting.ns(name).where(key: key).first
      if v
        unless options[:cache_keys_str].present?
          _cache_keys = options.delete(:cache_keys)
          _cache_keys ||= options.delete(:cache_key)

          if _cache_keys.nil?
            options[:cache_keys_str] = ""
            # if _cache
            #   options[:cache_keys_str] = name.underscore
            # end
          else
            if _cache_keys.is_a?(::Array)
              options[:cache_keys_str] = _cache_keys.map { |k| k.to_s.strip }.join(" ")
            else
              options[:cache_keys_str] = _cache_keys.to_s.strip
            end
          end
        end
        options.delete(:cache_keys)
        options.delete(:cache_key)

        _old_cache_keys = options[: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
        options[:cache_keys_str] = (_old_cache_keys + v.cache_keys).uniq
        options[:overwrite] = true unless (options[:cache_keys_str] - _old_cache_keys).blank?
        options[:cache_keys_str] = options[:cache_keys_str].map { |k| k.to_s.strip }.join(" ")
      end
    else
      v = @settings[key]
    end
    _overwrite = options[: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, options)
          rescue Exception => ex
            # puts "WTF"
            # puts ex.inspect
          end
        else
          v = set(key, options[:default], options)
        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

#inspectObject



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

Returns:

  • (Boolean)


20
21
22
# File 'lib/rails_admin_settings/namespaced.rb', line 20

def nil?
  false
end

#pretty_inspectObject



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, options = {})
  load! unless @locked if options[:loadable]
  key = key.to_s
  options.symbolize_keys!
  if options.key?(:cache)
    _cache = options.delete(:cache)
  else
    _cache = (name != ::Settings.ns_default)
  end

  if !options[:type].nil? && options[:type] == 'yaml' && !value.nil?
    if value.class.name != 'String'
      value = value.to_yaml
    end
  end

  unless options[:cache_keys_str].present?
    _cache_keys = options.delete(:cache_keys)
    _cache_keys ||= options.delete(:cache_key)

    if _cache_keys.nil?
      # if _cache
      #   options[:cache_keys_str] = name.underscore
      # end
    else
      if _cache_keys.is_a?(::Array)
        options[:cache_keys_str] = _cache_keys.map { |k| k.to_s.strip }.join(" ")
      else
        options[:cache_keys_str] = _cache_keys.to_s.strip
      end
    end
  end
  options.delete(:cache_keys)
  options.delete(:cache_key)

  options.merge!(value: value)
  if @locked
    ret = write_to_database(key, options)
  else
    mutex.synchronize do
      ret = write_to_database(key, options)
    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, options)
  options[:kind] = options[:kind].to_s if options[:kind]
  is_file = !options[:kind].nil? && (options[:kind] == 'image' || options[:kind] == 'file')
  is_array = !options[:kind].nil? && (options[:kind] == 'array')
  is_hash = !options[:kind].nil? && (options[:kind] == 'hash')
  if is_file
    options[:raw] = ''
    file = options[:value]
  elsif is_array
    options[:raw] = ''
    options[:raw_array] = options[:value] if options[:value]
  elsif is_hash
    options[:raw] = ''
    options[:raw_hash] = options[:value] if options[:value]
  else
    options[:raw] = options[:value] if options[:value]
  end

  options.delete(:value)
  options.delete(:default)
  options[:ns] = @name
  if @settings[key].nil?
    if options.delete(:overwrite)
      v = ::RailsAdminSettings::Setting.ns(options[:ns]).where(key: key).first
      if v
        opts = options.dup
        v.update_attributes!(opts)
      end
    end
    if v.nil?
      v = ::RailsAdminSettings::Setting.create(options.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.full_messages.join(',')
        end
      end
    end
    if options[:loadable]
      @settings[key] = v
    else
      return v
    end
  else
    opts = options.dup
    if options[: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 options[:loadable] and @settings[key]
    if options[:overwrite] != false || !@settings[key].file?
      @settings[key].file = file
      @settings[key].save!
    end
  end
  @settings[key]
end