Module: SuperSettings

Defined in:
lib/super_settings.rb,
lib/super_settings/coerce.rb,
lib/super_settings/engine.rb,
lib/super_settings/context.rb,
lib/super_settings/setting.rb,
lib/super_settings/storage.rb,
lib/super_settings/version.rb,
lib/super_settings/rest_api.rb,
lib/super_settings/attributes.rb,
lib/super_settings/application.rb,
lib/super_settings/http_client.rb,
lib/super_settings/local_cache.rb,
lib/super_settings/history_item.rb,
lib/super_settings/configuration.rb,
lib/super_settings/time_precision.rb,
lib/super_settings/context/current.rb,
lib/super_settings/rack_application.rb,
lib/super_settings/application/helper.rb,
lib/super_settings/controller_actions.rb,
lib/super_settings/storage/s3_storage.rb,
lib/super_settings/storage/transaction.rb,
lib/super_settings/storage/http_storage.rb,
lib/super_settings/storage/json_storage.rb,
lib/super_settings/storage/null_storage.rb,
lib/super_settings/storage/test_storage.rb,
lib/super_settings/storage/redis_storage.rb,
app/helpers/super_settings/settings_helper.rb,
lib/super_settings/context/rack_middleware.rb,
lib/super_settings/storage/mongodb_storage.rb,
lib/super_settings/context/sidekiq_middleware.rb,
lib/super_settings/storage/history_attributes.rb,
lib/super_settings/storage/storage_attributes.rb,
lib/super_settings/storage/active_record_storage.rb,
lib/super_settings/storage/active_record_storage/models.rb

Overview

This is the main interface to the access settings.

Defined Under Namespace

Modules: Attributes, Context, ControllerActions, Helper, SettingsHelper, Storage Classes: Application, Coerce, Configuration, Engine, HistoryItem, HttpClient, LocalCache, RackApplication, RestAPI, Setting, TimePrecision

Constant Summary collapse

DEFAULT_REFRESH_INTERVAL =

Default number of seconds between cache refresh checks.

5.0
VERSION =
File.read(File.expand_path("../../VERSION", __dir__)).chomp

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.authentication_urlObject

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.



252
253
254
# File 'lib/super_settings.rb', line 252

def authentication_url
  @authentication_url
end

.web_ui_javascriptObject

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.



256
257
258
# File 'lib/super_settings.rb', line 256

def web_ui_javascript
  @web_ui_javascript
end

Class Method Details

.[](key) ⇒ String

Alias for get to allow using the [] operator to get a setting value.

Parameters:

  • key (String, Symbol)

Returns:

  • (String)


49
50
51
# File 'lib/super_settings.rb', line 49

def [](key)
  get(key)
end

.array(key, default = nil) ⇒ Array

Get a setting value cast to an array of strings.

Parameters:

  • key (String, Symbol)
  • default (Array) (defaults to: nil)

    value to return if the setting value is nil

Returns:

  • (Array)


107
108
109
110
111
112
113
# File 'lib/super_settings.rb', line 107

def array(key, default = nil)
  val = context_setting(key)
  val = default if val.nil?
  return nil if val.nil?

  Array(val).collect { |v| v&.to_s }
end

.clear_cachevoid

This method returns an undefined value.

Reset the in memory cache. The cache will be automatically reloaded the next time you access a setting.



208
209
210
211
# File 'lib/super_settings.rb', line 208

def clear_cache
  local_cache.reset
  nil
end

.configurationSuperSettings::Configuration

Return the configuration object.



236
237
238
# File 'lib/super_settings.rb', line 236

def configuration
  Configuration.instance
end

.configure {|config| ... } ⇒ void

This method returns an undefined value.

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.

Yield Parameters:



226
227
228
229
230
231
# File 'lib/super_settings.rb', line 226

def configure(&block)
  Configuration.instance.defer(&block)
  unless defined?(Rails::Engine)
    Configuration.instance.call
  end
end

.context(&block) ⇒ Object

Define a block where settings will remain unchanged. This is useful to prevent settings from changing while you are in the middle of a block of code that depends on the settings.



177
178
179
180
181
182
183
184
185
# File 'lib/super_settings.rb', line 177

def context(&block)
  reset_context = Thread.current[:super_settings_context].nil?
  begin
    Thread.current[:super_settings_context] ||= Context::Current.new
    yield
  ensure
    Thread.current[:super_settings_context] = nil if reset_context
  end
end

.datetime(key, default = nil) ⇒ Time

Get a setting value cast to a Time.

Parameters:

  • key (String, Symbol)
  • default (Time) (defaults to: nil)

    value to return if the setting value is nil

Returns:

  • (Time)


97
98
99
100
# File 'lib/super_settings.rb', line 97

def datetime(key, default = nil)
  val = context_setting(key)
  Coerce.time(val.nil? ? default : val)
end

.disabled?(key, default = true) ⇒ Boolean

Return true if a setting cast as a boolean evaluates to false.

Parameters:

  • key (String, Symbol)
  • default (Boolean) (defaults to: true)

    value to return if the setting value is nil

Returns:

  • (Boolean)


88
89
90
# File 'lib/super_settings.rb', line 88

def disabled?(key, default = true)
  !enabled?(key, !default)
end

.enabled?(key, default = false) ⇒ Boolean

Get a setting value cast to a boolean.

Parameters:

  • key (String, Symbol)
  • default (Boolean) (defaults to: false)

    value to return if the setting value is nil

Returns:

  • (Boolean)


78
79
80
81
# File 'lib/super_settings.rb', line 78

def enabled?(key, default = false)
  val = context_setting(key)
  Coerce.boolean(val.nil? ? default : val)
end

.float(key, default = nil) ⇒ Float

Get a setting value cast to a float.

Parameters:

  • key (String, Symbol)
  • default (Numeric) (defaults to: nil)

    value to return if the setting value is nil

Returns:

  • (Float)


68
69
70
71
# File 'lib/super_settings.rb', line 68

def float(key, default = nil)
  val = context_setting(key)
  (val.nil? ? default : val)&.to_f
end

.get(key, default = nil) ⇒ String

Get a setting value cast to a string.

Parameters:

  • key (String, Symbol)
  • default (String) (defaults to: nil)

    value to return if the setting value is nil

Returns:

  • (String)


37
38
39
40
41
42
43
# File 'lib/super_settings.rb', line 37

def get(key, default = nil)
  val = context_setting(key)
  return val if val.is_a?(String)
  return default if val.nil?

  Coerce.string(val)
end

.integer(key, default = nil) ⇒ Integer

Get a setting value cast to an integer.

Parameters:

  • key (String, Symbol)
  • default (Integer) (defaults to: nil)

    value to return if the setting value is nil

Returns:

  • (Integer)


58
59
60
61
# File 'lib/super_settings.rb', line 58

def integer(key, default = nil)
  val = context_setting(key)
  (val.nil? ? default : val)&.to_i
end

.load_settingsvoid

This method returns an undefined value.

Load the settings from the database into the in memory cache.



190
191
192
193
194
# File 'lib/super_settings.rb', line 190

def load_settings
  local_cache.load_settings
  local_cache.wait_for_load
  nil
end

.loaded?Boolean

Return true if the in memory cache has been loaded from the database.

Returns:

  • (Boolean)


216
217
218
# File 'lib/super_settings.rb', line 216

def loaded?
  local_cache.loaded?
end

.rand(max = nil) ⇒ Integer, Float

Get a pseudo random number. This method works the same as Kernel.rand. However, if you are inside a context block, then the random number will be the same each time you call this method. This is useful when you need to generate a random number for a setting that you want to remain constant for the duration of the block.

So, for instance, if you are generating a random number to determine if a feature is enabled, you can use this method to ensure that the feature is either always enabled or always disabled for the duration of the block.

Parameters:

  • max (Integer, Float, Range) (defaults to: nil)

    the maximum value or range to use for the random number

Returns:

  • (Integer, Float)

    the random number. It will be an integer if max is an integer, otherwise it will be a float.



127
128
129
130
131
132
133
134
135
# File 'lib/super_settings.rb', line 127

def rand(max = nil)
  max ||= 1.0
  context = current_context
  if context
    context.rand(max)
  else
    Random.rand(max)
  end
end

.refresh_interval=(value) ⇒ void

This method returns an undefined value.

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.



246
247
248
# File 'lib/super_settings.rb', line 246

def refresh_interval=(value)
  local_cache.refresh_interval = value
end

.refresh_settingsvoid

This method returns an undefined value.

Force refresh the settings in the in memory cache to be in sync with the database.



199
200
201
202
# File 'lib/super_settings.rb', line 199

def refresh_settings
  local_cache.refresh
  nil
end

.set(key, value, value_type: nil) ⇒ void

This method returns an undefined value.

Create settings and update the local cache with the values. If a block is given, then the value will be reverted at the end of the block. This method can be used in tests when you need to inject a specific value into your settings.

Parameters:

  • key (String, Symbol)

    the key to set

  • value (Object)

    the value to set

  • value_type (String, Symbol) (defaults to: nil)

    the value type to set; if the setting does not already exist, this will be inferred from the value.



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/super_settings.rb', line 146

def set(key, value, value_type: nil)
  setting = Setting.find_by_key(key)
  if setting
    setting.value_type = value_type if value_type
  else
    setting = Setting.new(key: key)
    setting.value_type = (value_type || Setting.value_type(value) || Setting::STRING)
  end
  previous_value = setting.value
  setting.value = value
  begin
    setting.save!
    local_cache.load_settings unless local_cache.loaded?
    local_cache.update_setting(setting)

    if block_given?
      yield
    end
  ensure
    if block_given?
      setting.value = previous_value
      setting.save!
      local_cache.load_settings unless local_cache.loaded?
      local_cache.update_setting(setting)
    end
  end
end