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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 85279c41b179aa65d668a48d4d18e73b5fe886be9cd8235dc2acdb54c3beb5da
4
- data.tar.gz: 516991953aa7698cf86511512d863b7903d1ec5ad648a69c386501413a75ac86
3
+ metadata.gz: 4a7bc508cf14fd62288de455806b11c39f5894fc0830ea89523e9a4f9ca3d09b
4
+ data.tar.gz: 9d4affefb02823cf0a73344c7356f2e416d6cb6fb78dc0e25ad94a21cb4b1646
5
5
  SHA512:
6
- metadata.gz: 742596dc7e47c3d31ea64ecf54d96cae5885938076c2704aebe5f3e94701627631d19a8480f24ef97dc1861a8e1a1cab33c6f3aefd56ff755f88ef47c1380401
7
- data.tar.gz: aa7b9a3ba2443a7ac6cb12150c196247bfd543e086b4eea1199f83eca1035ddefc1bcb32ae5d01cf9c3448dcd2f5c651dd9a73c8dab22600d28f202ef7d1f4a6
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 Cache Demo"
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.host = "https://your-host.com"
72
- irb > Setting.host
73
- "https://your-host.com"
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`
@@ -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(self.cache_key)
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
- @keys
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
- def _define_field(key, default: nil, type: :string, readonly: false, separator: nil)
55
- if readonly
56
- define_singleton_method(key) do
57
- self.send(:_convert_string_to_typeof_value, type, default, separator: separator)
58
- end
59
- else
60
- define_singleton_method(key) do
61
- val = self.send(:_value_of, key)
62
- result = nil
63
- if !val.nil?
64
- result = val
65
- else
66
- result = default
67
- result = default.call if default.is_a?(Proc)
68
- end
69
-
70
- result = self.send(:_convert_string_to_typeof_value, type, result, separator: separator)
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
- define_singleton_method("#{key}=") do |value|
76
- var_name = key.to_s
88
+ result = send(:_convert_string_to_typeof_value, type, result, separator: separator)
77
89
 
78
- record = find_by(var: var_name) || new(var: var_name)
79
- value = self.send(:_convert_string_to_typeof_value, type, value, separator: separator)
90
+ result
91
+ end
80
92
 
81
- record.value = value
82
- record.save!
93
+ define_singleton_method("#{key}=") do |value|
94
+ var_name = key.to_s
83
95
 
84
- value
85
- end
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
- if type == :boolean
89
- define_singleton_method("#{key}?") do
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
- def _value_of(var_name)
119
- raise "#{self.table_name} does not exist." unless table_exists?
120
-
121
- _all_settings[var_name.to_s]
106
+ if type == :boolean
107
+ define_singleton_method("#{key}?") do
108
+ send(key)
109
+ end
122
110
  end
111
+ end
123
112
 
124
- def rails_initialized?
125
- Rails.application && Rails.application.initialized?
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
- def _all_settings
129
- raise "You cannot use settings before Rails initialize." unless rails_initialized?
130
- RequestStore.store[:rails_settings_all_settings] ||= begin
131
- Rails.cache.fetch(self.cache_key, expires_in: 1.week) do
132
- vars = unscoped.select("var, value")
133
- result = {}
134
- vars.each { |record| result[record.var] = record.value }
135
- result.with_indifferent_access
136
- end
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
@@ -3,7 +3,7 @@
3
3
  module RailsSettings
4
4
  class << self
5
5
  def version
6
- "2.2.1"
6
+ "2.3.1"
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.2.1
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-14 00:00:00.000000000 Z
11
+ date: 2020-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails