Class: Settings

Inherits:
BasicObject
Defined in:
lib/thecore_settings/settings.rb

Overview

we are inheriting from BasicObject so we don’t get a bunch of methods from Kernel or Object

Constant Summary collapse

@@file_uploads_supported =
false
@@file_uploads_engine =
false
@@namespaces =
{}
@@mutex =
::Mutex.new
@@ns_default =
'main'
@@ns_fallback =
nil

Class Method Summary collapse

Class Method Details

.apply_defaults!(file, verbose = false) ⇒ Object



68
69
70
# File 'lib/thecore_settings/settings.rb', line 68

def apply_defaults!(file, verbose = false)
  ThecoreSettings.apply_defaults!(file, verbose)
end

.create_setting(key, value, options = {}) ⇒ Object



86
87
88
# File 'lib/thecore_settings/settings.rb', line 86

def create_setting(key, value, options = {})
  set(key, nil, options.merge(overwrite: false))
end

.destroy_all!Object



55
56
57
58
# File 'lib/thecore_settings/settings.rb', line 55

def destroy_all!
  ThecoreSettings::Setting.destroy_all
  unload!
end

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



72
73
74
75
# File 'lib/thecore_settings/settings.rb', line 72

def get(key, options = {})
  options.symbolize_keys!
  ns(options[:ns], options).get(key, options)
end

.get_default_nsObject



36
37
38
# File 'lib/thecore_settings/settings.rb', line 36

def get_default_ns
  ns(nil, fallback: @@ns_fallback)
end

.method_missing(*args) ⇒ Object



90
91
92
# File 'lib/thecore_settings/settings.rb', line 90

def method_missing(*args)
  get_default_ns.__send__(*args)
end

.ns(name, options = {}) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/thecore_settings/settings.rb', line 19

def ns(name, options = {})
  options.symbolize_keys!
  if name.nil? || name == @@ns_default
    name = @@ns_default.to_s
  else
    name = name.to_s
  end
  if options.key?(:type)
    options[:kind] = options.delete(:type)
  end
  @@mutex.synchronize do
    @@namespaces[name] ||= ::ThecoreSettings::Namespaced.new(name.to_s)
  end
  fallback = options.key?(:fallback) ? options[:fallback] : @@ns_fallback
  ::ThecoreSettings::Fallback.new(@@namespaces[name], fallback)
end

.root_file_pathObject



60
61
62
63
64
65
66
# File 'lib/thecore_settings/settings.rb', line 60

def root_file_path
  if defined?(Rails)
    Rails.root
  else
    Pathname.new(File.dirname(__FILE__)).join('../..')
  end
end

.save_default(key, value, options = {}) ⇒ Object



82
83
84
# File 'lib/thecore_settings/settings.rb', line 82

def save_default(key, value, options = {})
  set(key, value, options.merge(overwrite: false))
end

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



77
78
79
80
# File 'lib/thecore_settings/settings.rb', line 77

def set(key, value = nil, options = {})
  options.symbolize_keys!
  ns(options[:ns], options).set(key, value, options)
end

.table_exists?Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/thecore_settings/settings.rb', line 40

def table_exists?
  ThecoreSettings.mongoid? || ThecoreSettings::Setting.table_exists?
rescue ActiveRecord::NoDatabaseError
  false
end

.unload!Object



46
47
48
49
50
51
52
53
# File 'lib/thecore_settings/settings.rb', line 46

def unload!
  @@mutex.synchronize do
    @@namespaces.values.map(&:unload!)
    @@namespaces = {}
    @@ns_default = 'main'
    @@ns_fallback = nil
  end
end