Class: ThecoreSettings::Namespaced

Inherits:
BasicObject
Defined in:
lib/thecore_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
15
# File 'lib/thecore_settings/namespaced.rb', line 8

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) ⇒ Object

returns processed setting value



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
155
156
157
158
159
160
# File 'lib/thecore_settings/namespaced.rb', line 123

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 ::ThecoreSettings.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 || {})
    if v.nil?
      ''
    else
      v.val
    end
  end
end

Instance Attribute Details

#fallbackObject

Returns the value of attribute fallback.



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

def fallback
  @fallback
end

#loadedObject (readonly)

Returns the value of attribute loaded.



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

def loaded
  @loaded
end

#mutexObject (readonly)

Returns the value of attribute mutex.



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

def mutex
  @mutex
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#ns_mutexObject (readonly)

Returns the value of attribute ns_mutex.



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

def ns_mutex
  @ns_mutex
end

#settingsObject

Returns the value of attribute settings.



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

def settings
  @settings
end

Instance Method Details

#[](key) ⇒ Object



101
102
103
# File 'lib/thecore_settings/namespaced.rb', line 101

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



98
99
100
# File 'lib/thecore_settings/namespaced.rb', line 98

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

#destroy!(key) ⇒ Object



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

def destroy!(key)
  load!
  key = key.to_s
  mutex.synchronize do
    ::ThecoreSettings::Setting.where(ns: @name, key: key).destroy_all
    @settings.delete(key)
  end
end

#destroy_all!Object



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

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

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

Returns:

  • (Boolean)


94
95
96
# File 'lib/thecore_settings/namespaced.rb', line 94

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

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

returns setting object



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

def get(key, options = {})
  load!
  key = key.to_s
  mutex.synchronize do
    @locked = true
    v = @settings[key]
    if v.nil?
      unless @fallback.nil? || @fallback == @name
        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



66
67
68
69
70
71
# File 'lib/thecore_settings/namespaced.rb', line 66

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

#inspectObject



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

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

#load!Object



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

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

#nil?Boolean

Returns:

  • (Boolean)


17
18
19
# File 'lib/thecore_settings/namespaced.rb', line 17

def nil?
  false
end

#pretty_inspectObject



23
24
25
# File 'lib/thecore_settings/namespaced.rb', line 23

def pretty_inspect
  inspect
end

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



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

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



38
39
40
41
42
43
# File 'lib/thecore_settings/namespaced.rb', line 38

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

#write_to_database(key, options) ⇒ Object



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
201
202
203
204
205
206
207
208
# File 'lib/thecore_settings/namespaced.rb', line 162

def write_to_database(key, options)
  is_file = !options[:kind].nil? && (options[:kind] == 'image' || options[:kind] == '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 = ::ThecoreSettings::Setting.create(options.merge(key: key))
    if !v.persisted?
      if v.errors[:key].any?
        v = ::ThecoreSettings::Setting.where(key: key).first
        if v.nil?
          ::Kernel.raise ::ThecoreSettings::PersistenceException, 'Fatal: error in key and not in DB'
        end
      else
        ::Kernel.raise ::ThecoreSettings::PersistenceException, v.errors.full_messages.join(',')
      end
    end
    @settings[key] = v
  else
    opts = options.dup
    if options[:overwrite] == false && !@settings[key].value.blank?
      opts.delete(:raw)
      opts.delete(:value)
      opts.delete(:enabled)
    end
    opts.delete(:overwrite)
    # Pre Rails 6.1 vs Post Rails 6.1
    # In Rails 6.1 update_attributes has been deprecated
    @settings[key].respond_to?('update_attributes!') ? @settings[key].update_attributes!(opts) : @settings[key].update!(opts)
  end
  if is_file
    if options[:overwrite] != false || !@settings[key].file?
      @settings[key].file = file
      @settings[key].save!
    end
  end
  @settings[key]
end