Class: RailsSettings::Settings

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/rails-settings/settings.rb

Direct Known Subclasses

Base

Defined Under Namespace

Classes: SettingNotFound

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.[](var_name) ⇒ Object

get a setting value by [] notation



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

def [](var_name)
  if var = object(var_name)
    val = var.value
  elsif Default.enabled?
    val = Default[var_name]
  else
    val = nil
  end
  val
end

.[]=(var_name, value) ⇒ Object

set a setting value by [] notation



84
85
86
87
88
89
90
91
92
# File 'lib/rails-settings/settings.rb', line 84

def []=(var_name, value)
  var_name = var_name.to_s

  record = object(var_name) || thing_scoped.new(var: var_name)
  record.value = value
  record.save!

  value
end

.destroy(var_name) ⇒ Object

destroy the specified settings record

Raises:



37
38
39
40
41
42
43
44
# File 'lib/rails-settings/settings.rb', line 37

def destroy(var_name)
  var_name = var_name.to_s
  obj = object(var_name)
  raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?

  obj.destroy
  true
end

.get_all(starting_with = nil) ⇒ Object

retrieve all settings as a hash (optionally starting with a given namespace)



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rails-settings/settings.rb', line 47

def get_all(starting_with = nil)
  vars = thing_scoped.select('var, value')
  vars = vars.where("var LIKE '#{starting_with}%'") if starting_with

  result = {}
  vars.each do |record|
    result[record.var] = record.value
  end

  defaults = {}
  if Default.enabled?
    defaults = starting_with.nil? ? Default.instance : Default.instance.select { |key, _| key.to_s.start_with?(starting_with) }
  end

  result.reverse_merge! defaults

  result.with_indifferent_access
end

.merge!(var_name, hash_value) ⇒ Object

Raises:

  • (ArgumentError)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rails-settings/settings.rb', line 94

def merge!(var_name, hash_value)
  raise ArgumentError unless hash_value.is_a?(Hash)

  old_value = self[var_name] || {}
  raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)

  new_value = old_value.merge(hash_value)
  self[var_name] = new_value if new_value != old_value

  new_value
end

.method_missing(method, *args) ⇒ Object

get or set a variable with the variable as the called method



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

def method_missing(method, *args)
  method_name = method.to_s
  super(method, *args)
rescue NoMethodError
  # set a value for a variable
  if method_name[-1] == '='
    var_name = method_name.sub('=', '')
    value = args.first
    self[var_name] = value
  else
    # retrieve a value
    self[method_name]
  end
end

.object(var_name) ⇒ Object



106
107
108
109
110
# File 'lib/rails-settings/settings.rb', line 106

def object(var_name)
  return nil unless rails_initialized?
  return nil unless table_exists?
  thing_scoped.where(var: var_name.to_s).first
end

.rails_initialized?Boolean

Returns:

  • (Boolean)


120
121
122
# File 'lib/rails-settings/settings.rb', line 120

def rails_initialized?
  Rails.application && Rails.application.initialized?
end

.source(filename) ⇒ Object



116
117
118
# File 'lib/rails-settings/settings.rb', line 116

def source(filename)
  Default.source(filename)
end

.thing_scopedObject



112
113
114
# File 'lib/rails-settings/settings.rb', line 112

def thing_scoped
  unscoped.where('thing_type is NULL and thing_id is NULL')
end

.where(sql = nil) ⇒ Object



66
67
68
69
# File 'lib/rails-settings/settings.rb', line 66

def where(sql = nil)
  vars = thing_scoped.where(sql) if sql
  vars
end

Instance Method Details

#valueObject

get the value field, YAML decoded



10
11
12
# File 'lib/rails-settings/settings.rb', line 10

def value
  YAML.load(self[:value]) if self[:value].present?
end

#value=(new_value) ⇒ Object

set the value field, YAML encoded



15
16
17
# File 'lib/rails-settings/settings.rb', line 15

def value=(new_value)
  self[:value] = new_value.to_yaml
end