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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Namespaced

Returns a new instance of Namespaced.



8
9
10
11
12
13
14
# File 'lib/rails_admin_settings/namespaced.rb', line 8

def initialize(name)
  self.settings = {}
  @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) ⇒ Object

returns processed setting value



122
123
124
125
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
# File 'lib/rails_admin_settings/namespaced.rb', line 122

def method_missing(key, *args)
  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 ::Mongoid::VERSION >= "4.0.0"
      v.set(enabled: args.first)
    else
      v.set("enabled", args.first)
    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 || {})
    if v.nil?
      ''
    else
      v.val
    end
  end
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



5
6
7
# File 'lib/rails_admin_settings/namespaced.rb', line 5

def fallback
  @fallback
end

#loadedObject (readonly)

Returns the value of attribute loaded.



6
7
8
# File 'lib/rails_admin_settings/namespaced.rb', line 6

def loaded
  @loaded
end

#mutexObject (readonly)

Returns the value of attribute mutex.



6
7
8
# File 'lib/rails_admin_settings/namespaced.rb', line 6

def mutex
  @mutex
end

#settingsObject

Returns the value of attribute settings.



5
6
7
# File 'lib/rails_admin_settings/namespaced.rb', line 5

def settings
  @settings
end

Instance Method Details

#[](key) ⇒ Object



100
101
102
# File 'lib/rails_admin_settings/namespaced.rb', line 100

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



97
98
99
# File 'lib/rails_admin_settings/namespaced.rb', line 97

def []=(key, value)
  set(key, value)
end

#destroy!(key) ⇒ Object



104
105
106
107
108
109
110
111
# File 'lib/rails_admin_settings/namespaced.rb', line 104

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



113
114
115
116
117
118
119
# File 'lib/rails_admin_settings/namespaced.rb', line 113

def destroy_all!
  mutex.synchronize do
    ::RailsAdminSettings::Setting.where(ns: @name).destroy_all
    @loaded = false
    @settings = {}
  end
end

#enabled?(key, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/rails_admin_settings/namespaced.rb', line 93

def enabled?(key, options = {})
  get(key, options).enabled?
end

#get(key, options = {}) ⇒ Object

returns setting object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rails_admin_settings/namespaced.rb', line 45

def get(key, options = {})
  load!
  key = key.to_s
  mutex.synchronize do
    @locked = true
    v = @settings[key]
    if v.nil?
      unless @fallback.nil?
        v = ::Settings.ns(@fallback).getnc(key)
      end
      if v.nil?
        v = set(key, options[:default], options)
      end
    end
    @locked = false
    v
  end
end

#getnc(key) ⇒ Object

returns setting object



65
66
67
68
69
70
# File 'lib/rails_admin_settings/namespaced.rb', line 65

def getnc(key)
  load!
  mutex.synchronize do
    self.settings[key]
  end
end

#inspectObject



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

def inspect
  "#<RailsAdminSettings::Namespaced name: #{@name.inspect}, fallback: #{@fallback.inspect}, loaded: #{@loaded}>"
end

#load!Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/rails_admin_settings/namespaced.rb', line 26

def load!
  mutex.synchronize do
    return if loaded
    @loaded = true
    @settings = {}
    ::RailsAdminSettings::Setting.ns(@name).each do |setting|
      @settings[setting.key] = setting
    end
  end
end

#nil?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/rails_admin_settings/namespaced.rb', line 16

def nil?
  false
end

#pretty_inspectObject



22
23
24
# File 'lib/rails_admin_settings/namespaced.rb', line 22

def pretty_inspect
  inspect
end

#set(key, value = nil, options = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rails_admin_settings/namespaced.rb', line 72

def set(key, value = nil, options = {})
  load! unless @locked
  key = key.to_s
  options.symbolize_keys!

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

  options.merge!(value: value)
  if @locked
    write_to_database(key, options)
  else
    mutex.synchronize do
      write_to_database(key, options)
    end
  end
end

#unload!Object



37
38
39
40
41
42
# File 'lib/rails_admin_settings/namespaced.rb', line 37

def unload!
  mutex.synchronize do
    @loaded = false
    @settings = {}
  end
end

#write_to_database(key, options) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
# File 'lib/rails_admin_settings/namespaced.rb', line 156

def write_to_database(key, options)
  is_file = !options[:type].nil? && (options[:type] == 'image' || options[:type] == 'file')
  if is_file
    options[:raw] = ''
    file = options[:value]
  else
    options[:raw] = options[:value]
  end

  options.delete(:value)
  options.delete(:default)
  options[:ns] = @name

  if @settings[key].nil?
    options.delete(:overwrite)
    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
    @settings[key] = v
  else
    opts = options.dup
    if options[:overwrite] == false
      opts.delete(:raw)
      opts.delete(:value)
      opts.delete(:enabled)
    end
    opts.delete(:overwrite)
    @settings[key].update_attributes!(opts)
  end
  if is_file
    if options[:overwrite] != false || !@settings[key].file?
      @settings[key].file = file
      @settings[key].save!
    end
  end
  @settings[key]
end