Class: Settings

Inherits:
BasicObject
Defined in:
lib/rails_admin_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

Class Method Summary collapse

Class Method Details

.apply_defaults!(file) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/rails_admin_settings/settings.rb', line 50

def apply_defaults!(file)
  if File.file?(file)
    yaml = YAML.load(File.read(file), safe: true)
    yaml.each_pair do |namespace, vals|
      vals.symbolize_keys!
      n = ns(namespace)
      vals.each_pair do |key, val|
        val.symbolize_keys!
        if !val[:type].nil? && (val[:type] == 'file' || val[:type] == 'image')
          unless @@file_uploads_supported
            ::Kernel.raise ::RailsAdminSettings::PersistenceException, "Fatal: setting #{key} is #{val[:type]} but file upload engine is not detected"
          end
          value = File.open(root_file_path.join(val.delete(:value)))
        else
          value = val.delete(:value)
        end
        n.set(key, value, val.merge(overwrite: false))
      end
      n.unload!
    end
  end
end

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



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

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

.destroy_all!Object



37
38
39
40
# File 'lib/rails_admin_settings/settings.rb', line 37

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

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



73
74
75
76
77
78
79
80
81
# File 'lib/rails_admin_settings/settings.rb', line 73

def get(key, options = {})
  options.symbolize_keys!

  if options[:ns].nil? || options[:ns].to_s == 'main'
    ns('main').get(key, options)
  else
    ns(options[:ns]).get(key, options)
  end
end

.method_missing(*args) ⇒ Object



101
102
103
# File 'lib/rails_admin_settings/settings.rb', line 101

def method_missing(*args)
  ns('main').__send__(*args)
end

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



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rails_admin_settings/settings.rb', line 13

def ns(name, options = {})
  options.symbolize_keys!
  if name.nil?
    name = 'main'
  else
    name = name.to_s
  end
  @@mutex.synchronize do
    @@namespaces[name] ||= ::RailsAdminSettings::Namespaced.new(name.to_s)
  end
  if options[:fallback].nil?
    @@namespaces[name]
  else
    ::RailsAdminSettings::Fallback.new(@@namespaces[name], options[:fallback])
  end
end

.root_file_pathObject



42
43
44
45
46
47
48
# File 'lib/rails_admin_settings/settings.rb', line 42

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

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



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

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

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



83
84
85
86
87
88
89
90
91
# File 'lib/rails_admin_settings/settings.rb', line 83

def set(key, value = nil, options = {})
  options.symbolize_keys!

  if options[:ns].nil? || options[:ns].to_s == 'main'
    ns('main').set(key, value, options)
  else
    ns(options[:ns]).set(key, value, options)
  end
end

.unload!Object



30
31
32
33
34
35
# File 'lib/rails_admin_settings/settings.rb', line 30

def unload!
  @@mutex.synchronize do
    @@namespaces.values.map(&:unload!)
    @@namespaces = {}
  end
end