lib/rails-settings/base.rb in rails-settings-cached-2.6.0 vs lib/rails-settings/base.rb in rails-settings-cached-2.7.0
- old
+ new
@@ -32,26 +32,31 @@
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, **opts)
end
+ def scope(name)
+ @scope = name.to_sym
+ yield
+ @scope = nil
+ end
+
def get_field(key)
@defined_fields.find { |field| field[:key] == key.to_s } || {}
end
def cache_prefix(&block)
@cache_prefix = block
end
def cache_key
- scope = ["rails-settings-cached"]
- scope << @cache_prefix.call if @cache_prefix
- scope.join("/")
+ key_parts = ["rails-settings-cached"]
+ key_parts << @cache_prefix.call if @cache_prefix
+ key_parts.join("/")
end
def keys
@defined_fields.map { |field| field[:key] }
end
@@ -62,22 +67,26 @@
def readonly_keys
@defined_fields.select { |field| field[:readonly] }.map { |field| field[:key] }
end
+ attr_reader :defined_fields
+
private
- def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil)
+ def _define_field(key, default: nil, type: :string, readonly: false, separator: nil, validates: nil, **opts)
key = key.to_s
raise ProcetedKeyError.new(key) if PROTECTED_KEYS.include?(key)
@defined_fields ||= []
@defined_fields << {
+ scope: @scope,
key: key,
default: default,
type: type || :string,
- readonly: readonly.nil? ? false : readonly
+ readonly: readonly.nil? ? false : readonly,
+ options: opts
}
if readonly
define_singleton_method(key) do
result = default.is_a?(Proc) ? default.call : default