rails-settings-cached 2.0.4 → 2.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -1
- data/lib/rails-settings/base.rb +8 -8
- 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: 3be04f65e8d47a4b4065143abf3ecad21a4534db3fd681dbb47ab2579206101c
|
4
|
+
data.tar.gz: bb37fc8eb054b0f785b9e74ef41d81fc77f5f52b6418cbf5624ffd6381dd54c7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f9d670ebd193b04b55c6a70d7ba63e59a9fb29ae98b3ed022ede02c857ba48c9f12069f875984ad6508f57f570c7950fb36c5e0967f70bd034d771a82009bf6
|
7
|
+
data.tar.gz: b011723eb1b3e92180a7bf4ab408370d77108ee749a3d034078bfcd334bd190cb1f27b71ed2af2c639227e4533c5c94f9766540be4b9ffd7b484c43fb2c1532e
|
data/README.md
CHANGED
@@ -51,6 +51,8 @@ class Setting < RailsSettings::Base
|
|
51
51
|
field :readonly_item, type: :integer, default: 100, readonly: true
|
52
52
|
field :user_limits, type: :integer, default: 20
|
53
53
|
field :admin_emails, type: :array, default: %w[admin@rubyonrails.org]
|
54
|
+
# Override array separator, default: /[\n,]/ split with \n or comma.
|
55
|
+
field :tips, type: :array, separator: /[\n]+/
|
54
56
|
field :captcha_enable, type: :boolean, default: 1
|
55
57
|
field :notification_options, type: :hash, default: {
|
56
58
|
send_all: true,
|
@@ -163,7 +165,7 @@ Setting.host -> Check Cache -> Exist - Get value of key for cache -> Return
|
|
163
165
|
Return default value or nil
|
164
166
|
```
|
165
167
|
|
166
|
-
In each Setting keys call, we will load the cache/db and save in
|
168
|
+
In each Setting keys call, we will load the cache/db and save in [RequestStore](https://github.com/steveklabnik/request_store) to avoid hit cache/db.
|
167
169
|
|
168
170
|
Each key update will expire the cache, so do not add some frequent update key.
|
169
171
|
|
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 = /[\
|
9
|
+
SEPARATOR_REGEXP = /[\n,]+/
|
10
10
|
self.table_name = table_name_prefix + "settings"
|
11
11
|
|
12
12
|
# get the value field, YAML decoded
|
@@ -30,7 +30,7 @@ module RailsSettings
|
|
30
30
|
end
|
31
31
|
|
32
32
|
def field(key, **opts)
|
33
|
-
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly])
|
33
|
+
_define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator])
|
34
34
|
end
|
35
35
|
|
36
36
|
def cache_prefix(&block)
|
@@ -44,10 +44,10 @@ module RailsSettings
|
|
44
44
|
end
|
45
45
|
|
46
46
|
private
|
47
|
-
def _define_field(key, default: nil, type: :string, readonly: false)
|
47
|
+
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
|
48
48
|
if readonly
|
49
49
|
define_singleton_method(key) do
|
50
|
-
self.send(:_covert_string_to_typeof_value, type, default)
|
50
|
+
self.send(:_covert_string_to_typeof_value, type, default, separator: separator)
|
51
51
|
end
|
52
52
|
else
|
53
53
|
define_singleton_method(key) do
|
@@ -60,7 +60,7 @@ module RailsSettings
|
|
60
60
|
result = default.call if default.is_a?(Proc)
|
61
61
|
end
|
62
62
|
|
63
|
-
result = self.send(:_covert_string_to_typeof_value, type, result)
|
63
|
+
result = self.send(:_covert_string_to_typeof_value, type, result, separator: separator)
|
64
64
|
|
65
65
|
result
|
66
66
|
end
|
@@ -69,7 +69,7 @@ module RailsSettings
|
|
69
69
|
var_name = key.to_s
|
70
70
|
|
71
71
|
record = find_by(var: var_name) || new(var: var_name)
|
72
|
-
value = self.send(:_covert_string_to_typeof_value, type, value)
|
72
|
+
value = self.send(:_covert_string_to_typeof_value, type, value, separator: separator)
|
73
73
|
|
74
74
|
record.value = value
|
75
75
|
record.save!
|
@@ -85,14 +85,14 @@ module RailsSettings
|
|
85
85
|
end
|
86
86
|
end
|
87
87
|
|
88
|
-
def _covert_string_to_typeof_value(type, value)
|
88
|
+
def _covert_string_to_typeof_value(type, value, separator: nil)
|
89
89
|
return value unless value.is_a?(String) || value.is_a?(Integer)
|
90
90
|
|
91
91
|
case type
|
92
92
|
when :boolean
|
93
93
|
return value == "true" || value == "1" || value == 1 || value == true
|
94
94
|
when :array
|
95
|
-
return value.split(SEPARATOR_REGEXP).reject { |str| str.empty? }
|
95
|
+
return value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }
|
96
96
|
when :hash
|
97
97
|
value = YAML.load(value).to_hash rescue {}
|
98
98
|
value.deep_stringify_keys!
|
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.0
|
4
|
+
version: 2.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Lee
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-07-
|
11
|
+
date: 2019-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|