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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '08c5252dd50adb752c5f4271a8e8c6f9a33ed99688d1df97de143430aa8a3010'
4
- data.tar.gz: 95cec20166fcf229ffe6f7b6bb70477e5139b297012b685a7ace320ff956639d
3
+ metadata.gz: 3be04f65e8d47a4b4065143abf3ecad21a4534db3fd681dbb47ab2579206101c
4
+ data.tar.gz: bb37fc8eb054b0f785b9e74ef41d81fc77f5f52b6418cbf5624ffd6381dd54c7
5
5
  SHA512:
6
- metadata.gz: 01d00c7d1681bdf3e3510a8397b5c7e3569672783260be3ef5fe835e5b51b7fb7fe15e9d328bcd92816100b23c4d8441e47602aa9ef3e56b2d77594df269375a
7
- data.tar.gz: eec0ac9dfe44f09abdf800c2c968a6c15bb29be0dfb98176fda26c277ec82b4a6753a024069064bbfe8845d5768b121f30b3f024b44e045229e375e43c1c0b27
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 `Thread.current` to avoid hit cache/db.
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
 
@@ -6,7 +6,7 @@ module RailsSettings
6
6
  class Base < ActiveRecord::Base
7
7
  class SettingNotFound < RuntimeError; end
8
8
 
9
- SEPARATOR_REGEXP = /[\s,]/
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!
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.0.4"
6
+ "2.1.0"
7
7
  end
8
8
  end
9
9
  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.0.4
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-02 00:00:00.000000000 Z
11
+ date: 2019-07-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails