lib/super_settings.rb in super_settings-0.0.0.rc1 vs lib/super_settings.rb in super_settings-0.0.1.rc1
- old
+ new
@@ -1,16 +1,13 @@
# frozen_string_literal: true
-require "secret_keys"
-
require_relative "super_settings/application"
require_relative "super_settings/coerce"
require_relative "super_settings/configuration"
require_relative "super_settings/local_cache"
-require_relative "super_settings/encryption"
require_relative "super_settings/rest_api"
-require_relative "super_settings/rack_middleware"
+require_relative "super_settings/rack_application"
require_relative "super_settings/controller_actions"
require_relative "super_settings/attributes"
require_relative "super_settings/setting"
require_relative "super_settings/history_item"
require_relative "super_settings/storage"
@@ -63,10 +60,19 @@
def enabled?(key, default = false)
val = local_cache[key]
Coerce.boolean(val.nil? ? default : val)
end
+ # Return true if a setting cast as a boolean evaluates to false.
+ #
+ # @param key [String, Symbol]
+ # @param default [Boolean] value to return if the setting value is nil
+ # @return [Boolean]
+ def disabled?(key, default = true)
+ !enabled?(key, !default)
+ end
+
# Get a setting value cast to a Time.
#
# @param key [String, Symbol]
# @param default [Time] value to return if the setting value is nil
# @return [Time]
@@ -89,18 +95,20 @@
# Get setting values cast to a hash. This method can be used to cast the flat setting key/value
# store into a structured data store. It uses a delimiter to define how keys are nested which
# defaults to a dot.
#
- # If, for example, you have three keys in you settings "A.B1.C1 = 1", "A.B1.C2 = 2", and "A.B2.C3 = 3", the
+ # If, for example, you have three keys in you settings +A.B1.C1 = 1+, +A.B1.C2 = 2+, and +A.B2.C3 = 3+, the
# nested structure will be:
#
- # `{"A" => {"B1" => {"C1" => 1, "C2" => 2}, "B2" => {"C3" => 3}}}`
+ # +{"A" => {"B1" => {"C1" => 1, "C2" => 2}, "B2" => {"C3" => 3}}}+
#
- # This whole hash would be returned if you called `hash` without any key. If you called it with the
- # key "A.B1", it would return `{"C1" => 1, "C2" => 2}`.
+ # This whole hash would be returned if you called +hash+ without any key. If you called it with the
+ # key "A.B1", it would return
#
+ # +{"C1" => 1, "C2" => 2}+
+ #
# @param key [String, Symbol] the prefix patter to fetch keys for; default to returning all settings
# @param default [Hash] value to return if the setting value is nil
# @param delimiter [String] the delimiter to use to define nested keys in the hash; defaults to "."
# @return [Hash]
def structured(key = nil, default = nil, delimiter: ".", max_depth: nil)
@@ -144,24 +152,30 @@
end
end
end
# Load the settings from the database into the in memory cache.
+ #
+ # @return [void]
def load_settings
local_cache.load_settings
local_cache.wait_for_load
nil
end
# Force refresh the settings in the in memory cache to be in sync with the database.
+ #
+ # @return [void]
def refresh_settings
local_cache.refresh
nil
end
# Reset the in memory cache. The cache will be automatically reloaded the next time
# you access a setting.
+ #
+ # @return [void]
def clear_cache
local_cache.reset
nil
end
@@ -174,11 +188,12 @@
# Configure various aspects of the gem. The block will be yielded to with a configuration
# object. You should use this method to configure the gem from an Rails initializer since
# it will handle ensuring all the appropriate frameworks are loaded first.
#
- # yieldparam config [SuperSettings::Configuration]
+ # @yieldparam config [SuperSettings::Configuration]
+ # @return [void]
def configure(&block)
Configuration.instance.defer(&block)
unless defined?(Rails::Engine)
Configuration.instance.call
end
@@ -186,24 +201,22 @@
# Set the number of seconds between checks to synchronize the in memory cache from the database.
# This setting aids in performance since it throttles the number of times the database is queried
# for changes. However, changes made to the settings in the databae will take up to the number of
# seconds in the refresh interval to be updated in the cache.
+ #
+ # @return [void]
def refresh_interval=(value)
local_cache.refresh_interval = value
end
- # Set the secret used to encrypt secret settings in the database.
- #
- # If you need to roll your secret, you can pass in an array of values. The first one
- # specified will be used to encrypt values, but all of the keys will be tried when
- # decrypting a value already stored in the database.
- #
- # @param value [String, Array]
- def secret=(value)
- Encryption.secret = value
- load_settings if loaded?
- end
+ # URL for authenticating access to the application. This would normally be some kind of
+ # login page. Browsers will be redirected here if they are denied access to the web UI.
+ attr_accessor :authentication_url
+
+ # Javascript to inject into the settings application HTML page. This can be used, for example,
+ # to set authorization credentials stored client side to access the settings API.
+ attr_accessor :web_ui_javascript
private
def local_cache
@local_cache ||= LocalCache.new(refresh_interval: DEFAULT_REFRESH_INTERVAL)