lib/rails-settings/base.rb in rails-settings-cached-2.5.1 vs lib/rails-settings/base.rb in rails-settings-cached-2.5.2
- old
+ new
@@ -26,11 +26,12 @@
RequestCache.reset
Rails.cache.delete(cache_key)
end
def field(key, **opts)
- _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly], separator: opts[:separator], validates: opts[:validates])
+ _define_field(key, default: opts[:default], type: opts[:type], readonly: opts[:readonly],
+ separator: opts[:separator], validates: opts[:validates])
end
def get_field(key)
@defined_fields.find { |field| field[:key] == key.to_s } || {}
end
@@ -58,13 +59,15 @@
end
private
def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil)
+ key = key.to_s
+
@defined_fields ||= []
@defined_fields << {
- key: key.to_s,
+ key: key,
default: default,
type: type || :string,
readonly: readonly.nil? ? false : readonly
}
@@ -87,11 +90,11 @@
result
end
define_singleton_method("#{key}=") do |value|
- var_name = key.to_s
+ var_name = key
record = find_by(var: var_name) || new(var: var_name)
value = send(:_convert_string_to_typeof_value, type, value, separator: separator)
record.value = value
@@ -99,11 +102,11 @@
value
end
if validates
- validates[:if] = Proc.new { |item| item.var.to_s == key.to_s }
+ validates[:if] = proc { |item| item.var.to_s == key }
send(:validates, key, **validates)
define_method(:read_attribute_for_validation) do |_key|
self.value
end
@@ -120,23 +123,23 @@
def _convert_string_to_typeof_value(type, value, separator: nil)
return value unless [String, Integer, Float, BigDecimal].include?(value.class)
case type
when :boolean
- value == "true" || value == "1" || value == 1 || value == true
+ ["true", "1", 1, true].include?(value)
when :array
value.split(separator || SEPARATOR_REGEXP).reject { |str| str.empty? }.map(&:strip)
when :hash
value = begin
- begin
- YAML.load(value).to_h
- rescue StandardError
- eval(value).to_h
- end
- rescue StandardError
- {}
- end
+ begin
+ YAML.load(value).to_h
+ rescue StandardError
+ eval(value).to_h
+ end
+ rescue StandardError
+ {}
+ end
value.deep_stringify_keys!
ActiveSupport::HashWithIndifferentAccess.new(value)
when :integer
value.to_i
when :float
@@ -153,10 +156,10 @@
# Fallback to default value if table was not ready (before migrate)
puts "WARNING: table: \"#{table_name}\" does not exist or not database connection, `#{name}.#{var_name}` fallback to returns the default value."
return nil
end
- _all_settings[var_name.to_s]
+ _all_settings[var_name]
end
def _table_exists?
table_exists?
rescue => e