rails-settings-cached 0.7.2 → 2.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,120 +0,0 @@
1
- module RailsSettings
2
- class Settings < ActiveRecord::Base
3
- self.table_name = table_name_prefix + "settings"
4
-
5
- class SettingNotFound < RuntimeError; end
6
-
7
- belongs_to :thing, polymorphic: true
8
-
9
- # get the value field, YAML decoded
10
- def value
11
- YAML.load(self[:value]) if self[:value].present?
12
- end
13
-
14
- # set the value field, YAML encoded
15
- def value=(new_value)
16
- self[:value] = new_value.to_yaml
17
- end
18
-
19
- class << self
20
- # get or set a variable with the variable as the called method
21
- # rubocop:disable Style/MethodMissing
22
- def method_missing(method, *args)
23
- method_name = method.to_s
24
- super(method, *args)
25
- rescue NoMethodError
26
- # set a value for a variable
27
- if method_name[-1] == "="
28
- var_name = method_name.sub("=", "")
29
- value = args.first
30
- self[var_name] = value
31
- else
32
- # retrieve a value
33
- self[method_name]
34
- end
35
- end
36
-
37
- # destroy the specified settings record
38
- def destroy(var_name)
39
- var_name = var_name.to_s
40
- obj = object(var_name)
41
- raise SettingNotFound, "Setting variable \"#{var_name}\" not found" if obj.nil?
42
-
43
- obj.destroy
44
- true
45
- end
46
-
47
- # retrieve all settings as a hash (optionally starting with a given namespace)
48
- def get_all(starting_with = nil)
49
- vars = thing_scoped.select("var, value")
50
- vars = vars.where("var LIKE '#{starting_with}%'") if starting_with
51
- result = {}
52
- vars.each { |record| result[record.var] = record.value }
53
- result.reverse_merge!(default_settings(starting_with))
54
- result.with_indifferent_access
55
- end
56
-
57
- def where(sql = nil)
58
- vars = thing_scoped.where(sql) if sql
59
- vars
60
- end
61
-
62
- # get a setting value by [] notation
63
- def [](var_name)
64
- return Default[var_name] unless rails_initialized?
65
-
66
- val = object(var_name)
67
- return val.value if val
68
- return Default[var_name] if Default.enabled?
69
- end
70
-
71
- # set a setting value by [] notation
72
- def []=(var_name, value)
73
- var_name = var_name.to_s
74
-
75
- record = object(var_name) || thing_scoped.new(var: var_name)
76
- record.value = value
77
- record.save!
78
-
79
- value
80
- end
81
-
82
- def merge!(var_name, hash_value)
83
- raise ArgumentError unless hash_value.is_a?(Hash)
84
-
85
- old_value = self[var_name] || {}
86
- raise TypeError, "Existing value is not a hash, can't merge!" unless old_value.is_a?(Hash)
87
-
88
- new_value = old_value.merge(hash_value)
89
- self[var_name] = new_value if new_value != old_value
90
-
91
- new_value
92
- end
93
-
94
- def object(var_name)
95
- return nil unless table_exists?
96
- thing_scoped.where(var: var_name.to_s).first
97
- end
98
-
99
- def thing_scoped
100
- unscoped.where("thing_type is NULL and thing_id is NULL")
101
- end
102
-
103
- def source(filename)
104
- Default.source(filename)
105
- end
106
-
107
- def rails_initialized?
108
- Rails.application && Rails.application.initialized?
109
- end
110
-
111
- private
112
-
113
- def default_settings(starting_with = nil)
114
- return {} unless Default.enabled?
115
- return Default.instance if starting_with.nil?
116
- Default.instance.select { |key, _| key.to_s.start_with?(starting_with) }
117
- end
118
- end
119
- end
120
- end