rails-settings-cached 2.2.1 → 2.3.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +32 -5
- data/lib/rails-settings/base.rb +100 -73
- data/lib/rails-settings/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4a7bc508cf14fd62288de455806b11c39f5894fc0830ea89523e9a4f9ca3d09b
|
4
|
+
data.tar.gz: 9d4affefb02823cf0a73344c7356f2e416d6cb6fb78dc0e25ad94a21cb4b1646
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c023a86676cd5f41c5479e8c671df1946076de4167f8fe40c437e69d69e27d1d46a5d5e5a97ad35f950f6b477a0c14ab6de0c4e4fbd3210df46d0c091ac6c312
|
7
|
+
data.tar.gz: 16ed2df3231c5379847c25c97f3950a8692ce51a36d5b1ffc7e1973a11786de820f8264e628f3f7df5e6689018a91e3849a4749c5757eb7f1b076a5b041f538b
|
data/README.md
CHANGED
@@ -30,8 +30,8 @@ You will get `app/models/setting.rb`
|
|
30
30
|
```rb
|
31
31
|
class Setting < RailsSettings::Base
|
32
32
|
# cache_prefix { "v1" }
|
33
|
-
field :app_name, default: "Rails Settings
|
34
|
-
field :host, default: "http://example.com"
|
33
|
+
field :app_name, default: "Rails Settings"
|
34
|
+
field :host, default: "http://example.com", readonly: true
|
35
35
|
field :default_locale, default: "zh-CN"
|
36
36
|
field :readonly_item, type: :integer, default: 100, readonly: true
|
37
37
|
field :user_limits, type: :integer, default: 20
|
@@ -68,9 +68,11 @@ The syntax is easy. First, let's create some settings to keep track of:
|
|
68
68
|
```ruby
|
69
69
|
irb > Setting.host
|
70
70
|
"http://example.com"
|
71
|
-
irb > Setting.
|
72
|
-
|
73
|
-
|
71
|
+
irb > Setting.app_name
|
72
|
+
"Rails Settings"
|
73
|
+
irb > Setting.app_name = "Rails Settings Cached"
|
74
|
+
irb > Setting.app_name
|
75
|
+
"Rails Settings Cached"
|
74
76
|
|
75
77
|
irb > Setting.user_limits
|
76
78
|
20
|
@@ -124,6 +126,31 @@ irb > Setting.notification_options
|
|
124
126
|
}
|
125
127
|
```
|
126
128
|
|
129
|
+
|
130
|
+
### Get defined fields
|
131
|
+
|
132
|
+
> version 2.3+
|
133
|
+
|
134
|
+
```rb
|
135
|
+
# Get all keys
|
136
|
+
Setting.keys
|
137
|
+
=> ["app_name", "host", "default_locale", "readonly_item"]
|
138
|
+
|
139
|
+
# Get editable keys
|
140
|
+
Settng.editable_keys
|
141
|
+
=> ["app_name", "default_locale"]
|
142
|
+
|
143
|
+
# Get readonly keys
|
144
|
+
Setting.readonly_keys
|
145
|
+
=> ["host", "readonly_item"]
|
146
|
+
|
147
|
+
# Get options of field
|
148
|
+
Setting.get_field("host")
|
149
|
+
=> { key: "host", type: :string, default: "http://example.com", readonly: true }
|
150
|
+
Setting.get_field("app_name")
|
151
|
+
=> { key: "app_name", type: :string, default: "Rails Settings", readonly: false }
|
152
|
+
```
|
153
|
+
|
127
154
|
## Readonly field
|
128
155
|
|
129
156
|
Sometimes you may need to use Setting before Rails is initialized, for example `config/devise.rb`
|
data/lib/rails-settings/base.rb
CHANGED
@@ -6,7 +6,7 @@ module RailsSettings
|
|
6
6
|
class Base < ActiveRecord::Base
|
7
7
|
class SettingNotFound < RuntimeError; end
|
8
8
|
|
9
|
-
SEPARATOR_REGEXP = /[\n,;]
|
9
|
+
SEPARATOR_REGEXP = /[\n,;]+/.freeze
|
10
10
|
self.table_name = table_name_prefix + "settings"
|
11
11
|
|
12
12
|
# get the value field, YAML decoded
|
@@ -26,15 +26,17 @@ module RailsSettings
|
|
26
26
|
class << self
|
27
27
|
def clear_cache
|
28
28
|
RequestStore.store[:rails_settings_all_settings] = nil
|
29
|
-
Rails.cache.delete(
|
29
|
+
Rails.cache.delete(cache_key)
|
30
30
|
end
|
31
31
|
|
32
32
|
def field(key, **opts)
|
33
|
-
@keys ||= []
|
34
|
-
@keys << key.to_s
|
35
33
|
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
|
36
34
|
end
|
37
35
|
|
36
|
+
def get_field(key)
|
37
|
+
@defined_fields.find { |field| field[:key] == key.to_s } || {}
|
38
|
+
end
|
39
|
+
|
38
40
|
def cache_prefix(&block)
|
39
41
|
@cache_prefix = block
|
40
42
|
end
|
@@ -46,96 +48,121 @@ module RailsSettings
|
|
46
48
|
end
|
47
49
|
|
48
50
|
def keys
|
49
|
-
@
|
51
|
+
@defined_fields.map { |field| field[:key] }
|
52
|
+
end
|
53
|
+
|
54
|
+
def editable_keys
|
55
|
+
@defined_fields.reject { |field| field[:readonly] }.map { |field| field[:key] }
|
56
|
+
end
|
57
|
+
|
58
|
+
def readonly_keys
|
59
|
+
@defined_fields.select { |field| field[:readonly] }.map { |field| field[:key] }
|
50
60
|
end
|
51
61
|
|
52
62
|
private
|
53
63
|
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
result
|
64
|
+
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
|
65
|
+
@defined_fields ||= []
|
66
|
+
@defined_fields << {
|
67
|
+
key: key.to_s,
|
68
|
+
default: default,
|
69
|
+
type: type || :string,
|
70
|
+
readonly: readonly.nil? ? false : readonly
|
71
|
+
}
|
72
|
+
|
73
|
+
if readonly
|
74
|
+
define_singleton_method(key) do
|
75
|
+
send(:_convert_string_to_typeof_value, type, default, separator: separator)
|
76
|
+
end
|
77
|
+
else
|
78
|
+
define_singleton_method(key) do
|
79
|
+
val = send(:_value_of, key)
|
80
|
+
result = nil
|
81
|
+
if !val.nil?
|
82
|
+
result = val
|
83
|
+
else
|
84
|
+
result = default
|
85
|
+
result = default.call if default.is_a?(Proc)
|
73
86
|
end
|
74
87
|
|
75
|
-
|
76
|
-
var_name = key.to_s
|
88
|
+
result = send(:_convert_string_to_typeof_value, type, result, separator: separator)
|
77
89
|
|
78
|
-
|
79
|
-
|
90
|
+
result
|
91
|
+
end
|
80
92
|
|
81
|
-
|
82
|
-
|
93
|
+
define_singleton_method("#{key}=") do |value|
|
94
|
+
var_name = key.to_s
|
83
95
|
|
84
|
-
|
85
|
-
|
86
|
-
end
|
96
|
+
record = find_by(var: var_name) || new(var: var_name)
|
97
|
+
value = send(:_convert_string_to_typeof_value, type, value, separator: separator)
|
87
98
|
|
88
|
-
|
89
|
-
|
90
|
-
self.send(key)
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
99
|
+
record.value = value
|
100
|
+
record.save!
|
94
101
|
|
95
|
-
def _convert_string_to_typeof_value(type, value, separator: nil)
|
96
|
-
return value unless [String, Integer, Float, BigDecimal].include?(value.class)
|
97
|
-
|
98
|
-
case type
|
99
|
-
when :boolean
|
100
|
-
value == "true" || value == "1" || value == 1 || value == true
|
101
|
-
when :array
|
102
|
-
value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
|
103
|
-
when :hash
|
104
|
-
value = YAML.load(value).to_h rescue eval(value).to_h rescue {}
|
105
|
-
value.deep_stringify_keys!
|
106
|
-
value
|
107
|
-
when :integer
|
108
|
-
value.to_i
|
109
|
-
when :float
|
110
|
-
value.to_f
|
111
|
-
when :big_decimal
|
112
|
-
value.to_d
|
113
|
-
else
|
114
102
|
value
|
115
103
|
end
|
116
104
|
end
|
117
105
|
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
106
|
+
if type == :boolean
|
107
|
+
define_singleton_method("#{key}?") do
|
108
|
+
send(key)
|
109
|
+
end
|
122
110
|
end
|
111
|
+
end
|
123
112
|
|
124
|
-
|
125
|
-
|
113
|
+
def _convert_string_to_typeof_value(type, value, separator: nil)
|
114
|
+
return value unless [String, Integer, Float, BigDecimal].include?(value.class)
|
115
|
+
|
116
|
+
case type
|
117
|
+
when :boolean
|
118
|
+
value == "true" || value == "1" || value == 1 || value == true
|
119
|
+
when :array
|
120
|
+
value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
|
121
|
+
when :hash
|
122
|
+
value = begin
|
123
|
+
begin
|
124
|
+
YAML.load(value).to_h
|
125
|
+
rescue StandardError
|
126
|
+
eval(value).to_h
|
127
|
+
end
|
128
|
+
rescue StandardError
|
129
|
+
{}
|
130
|
+
end
|
131
|
+
value.deep_stringify_keys!
|
132
|
+
value
|
133
|
+
when :integer
|
134
|
+
value.to_i
|
135
|
+
when :float
|
136
|
+
value.to_f
|
137
|
+
when :big_decimal
|
138
|
+
value.to_d
|
139
|
+
else
|
140
|
+
value
|
126
141
|
end
|
142
|
+
end
|
127
143
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
136
|
-
|
144
|
+
def _value_of(var_name)
|
145
|
+
raise "#{table_name} does not exist." unless table_exists?
|
146
|
+
|
147
|
+
_all_settings[var_name.to_s]
|
148
|
+
end
|
149
|
+
|
150
|
+
def rails_initialized?
|
151
|
+
Rails.application&.initialized?
|
152
|
+
end
|
153
|
+
|
154
|
+
def _all_settings
|
155
|
+
raise "You cannot use settings before Rails initialize." unless rails_initialized?
|
156
|
+
|
157
|
+
RequestStore.store[:rails_settings_all_settings] ||= begin
|
158
|
+
Rails.cache.fetch(cache_key, expires_in: 1.week) do
|
159
|
+
vars = unscoped.select("var, value")
|
160
|
+
result = {}
|
161
|
+
vars.each { |record| result[record.var] = record.value }
|
162
|
+
result.with_indifferent_access
|
137
163
|
end
|
138
164
|
end
|
165
|
+
end
|
139
166
|
end
|
140
167
|
end
|
141
168
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rails-settings-cached
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.
|
4
|
+
version: 2.3.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-10-
|
11
|
+
date: 2020-10-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|