Module: UltraSettings

Defined in:
lib/ultra_settings.rb,
lib/ultra_settings/field.rb,
lib/ultra_settings/coerce.rb,
lib/ultra_settings/railtie.rb,
lib/ultra_settings/version.rb,
lib/ultra_settings/rack_app.rb,
lib/ultra_settings/web_view.rb,
lib/ultra_settings/view_helper.rb,
lib/ultra_settings/yaml_config.rb,
lib/ultra_settings/config_helper.rb,
lib/ultra_settings/configuration.rb,
lib/ultra_settings/render_helper.rb,
lib/ultra_settings/application_view.rb,
lib/ultra_settings/configuration_view.rb,
lib/ultra_settings/uninitialized_runtime_settings.rb

Overview

This is the root namespace for UltraSettings. You can add configurations to this namespace using the add method.

Examples:

UltraSettings.add(:test)
UltraSettings.test # => TestConfiguration.instance

Defined Under Namespace

Modules: ConfigHelper, RenderHelper, ViewHelper Classes: ApplicationView, Coerce, Configuration, ConfigurationView, Field, RackApp, Railtie, UninitializedRuntimeSettings, WebView, YamlConfig

Constant Summary collapse

VALID_NAME__PATTERN =
/\A[a-z_][a-zA-Z0-9_]*\z/
VERSION =
File.read(File.join(__dir__, "..", "..", "VERSION")).strip

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.runtime_settings=(value) ⇒ void (writeonly)

This method returns an undefined value.

Set the object to use for runtime settings. This can be any object that responds to the [] method. If you are using the ‘super_settings` gem, you can set this to `SuperSettings`.

Parameters:

  • value (#[])

    The object to use for runtime settings.



172
173
174
# File 'lib/ultra_settings.rb', line 172

def runtime_settings=(value)
  @runtime_settings = value
end

.runtime_settings_secure=(value) ⇒ void (writeonly)

This method returns an undefined value.

Set whether or not the runtime settings engine is considered secure. If this is set to false, then runtime settings will be disabled for all fields marked as secret. The default value is true.

Parameters:

  • value (Boolean)

    Whether the runtime settings engine is secure.



213
214
215
# File 'lib/ultra_settings.rb', line 213

def runtime_settings_secure=(value)
  @runtime_settings_secure = value
end

.runtime_settings_url(name: nil, type: nil, description: nil) ⇒ String?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the URL for changing runtime settings.

Parameters:

  • name (String) (defaults to: nil)

    The name of the setting.

  • type (String) (defaults to: nil)

    The type of the setting.

  • description (String) (defaults to: nil)

    The description of the setting.

Returns:

  • (String, nil)


198
199
200
201
202
203
204
205
# File 'lib/ultra_settings.rb', line 198

def runtime_settings_url(name: nil, type: nil, description: nil)
  url = @runtime_settings_url.to_s
  return nil if url.empty?

  url.gsub("${name}", URI.encode_www_form_component(name.to_s))
    .gsub("${type}", URI.encode_www_form_component(type.to_s))
    .gsub("${description}", URI.encode_www_form_component(description.to_s))
end

Class Method Details

.__configuration_names__Array<String>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the names of all of the configurations that have been added.

Returns:

  • (Array<String>)

    The names of the configurations.



259
260
261
# File 'lib/ultra_settings.rb', line 259

def __configuration_names__
  @configurations.keys
end

.__configurations__Array<UltraSettings::Configuration>

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get an array of all of the configuration instances that have been loaded into memory.

Returns:



267
268
269
270
271
272
273
274
# File 'lib/ultra_settings.rb', line 267

def __configurations__
  @configurations.each do |name, class_name|
    __load_config__(name, class_name)
  end

  config_classes = ObjectSpace.each_object(Class).select { |klass| klass < Configuration }
  config_classes.collect(&:instance)
end

.__runtime_settings__Object?

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Get the object to use for runtime settings.

Returns:

  • (Object, nil)


178
179
180
# File 'lib/ultra_settings.rb', line 178

def __runtime_settings__
  @runtime_settings
end

.add(name, klass = nil) ⇒ void

This method returns an undefined value.

Adds a configuration to the root namespace. The configuration will be available as a method on the UltraSettings module with the provide name.

Parameters:

  • name (Symbol, String)

    The name of the configuration.

  • klass (Class, String) (defaults to: nil)

    The class of the configuration. If this is not provided then the class will be inferred from the name by camelizing the name and appending “Configuration” to get the class name.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ultra_settings.rb', line 53

def add(name, klass = nil)
  name = name.to_s
  unless name.match?(VALID_NAME__PATTERN)
    raise ArgumentError.new("Invalid configuration name: #{name.inspect}")
  end

  class_name = klass&.to_s
  class_name ||= "#{classify(name)}Configuration"

  @mutex.synchronize do
    @configurations[name] = class_name

    eval "      def \#{name}\n        __load_config__(\#{name.inspect}, \#{class_name.inspect})\n      end\n    RUBY\n  end\nend\n", binding, __FILE__, __LINE__ + 1 # rubocop:disable Security/Eval

.added?(class_name) ⇒ Boolean

Returns true if the provided class has been added as a configuration.

Parameters:

  • class_name (Class, String)

    The name of the configuration class.

Returns:

  • (Boolean)


77
78
79
# File 'lib/ultra_settings.rb', line 77

def added?(class_name)
  @configurations.values.collect(&:to_s).include?(class_name.to_s)
end

.env_var_delimiter=(value) ⇒ void

This method returns an undefined value.

Set the delimiter to use when determining environment variable names. By default this is an underscore.

Parameters:

  • value (String)

    The delimiter to use.



126
127
128
# File 'lib/ultra_settings.rb', line 126

def env_var_delimiter=(value)
  Configuration.env_var_delimiter = value.to_s
end

.env_var_upcase=(value) ⇒ void

This method returns an undefined value.

Control if environment variable names should be upcased. By default this is true.

Parameters:

  • value (Boolean)

    Whether or not to upcase environment variable names.



144
145
146
# File 'lib/ultra_settings.rb', line 144

def env_var_upcase=(value)
  Configuration.env_var_upcase = !!value
end

.environment_variables_disabled=(value) ⇒ void

This method returns an undefined value.

Control if settings can be loaded from environment variables. By default environment variables are enabled. This can also be disabled on individual Configuration classes.

Parameters:

  • value (Boolean)

    Whether or not to load settings from environment variables.



87
88
89
# File 'lib/ultra_settings.rb', line 87

def environment_variables_disabled=(value)
  Configuration.environment_variables_disabled = !!value
end

.fields_secret_by_default=(value) ⇒ void

This method returns an undefined value.

Set whether fields should be considered secret by default.

Parameters:

  • value (Boolean)

    Whether fields should be secret by default.



226
227
228
# File 'lib/ultra_settings.rb', line 226

def fields_secret_by_default=(value)
  Configuration.fields_secret_by_default = value
end

.override!(settings, &block) ⇒ Object

Explicitly set values for setting within a block. This is useful for testing or other situations where you want hard code a specific set of values.

Parameters:

  • settings (Hash)

    The settings to set.

Returns:

  • (Object)

    The result of the block.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/ultra_settings.rb', line 235

def override!(settings, &block)
  settings = settings.to_a
  config_name, values = settings.first
  config_name = config_name.to_s
  other_settings = settings[1..-1]

  unless @configurations.include?(config_name)
    raise ArgumentError.new("Unknown configuration: #{config_name.inspect}")
  end

  config = send(config_name)
  config.override!(values) do
    if other_settings.empty?
      yield
    else
      override!(other_settings, &block)
    end
  end
end

.runtime_setting_delimiter=(value) ⇒ void

This method returns an undefined value.

Set the delimiter to use when determining setting names. By default this is a period.

Parameters:

  • value (String)

    The delimiter to use.



135
136
137
# File 'lib/ultra_settings.rb', line 135

def runtime_setting_delimiter=(value)
  Configuration.runtime_setting_delimiter = value.to_s
end

.runtime_setting_upcase=(value) ⇒ void

This method returns an undefined value.

Control if setting names should be upcased. By default this is false.

Parameters:

  • value (Boolean)

    Whether or not to upcase setting names.



152
153
154
# File 'lib/ultra_settings.rb', line 152

def runtime_setting_upcase=(value)
  Configuration.runtime_setting_upcase = !!value
end

.runtime_settings_disabled=(value) ⇒ void

This method returns an undefined value.

Control if settings can be loaded from runtime settings. By default runtime settings are enabled. This can also be disabled on individual Configuration classes.

Parameters:

  • value (Boolean)

    Whether or not to load settings from runtime settings.



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

def runtime_settings_disabled=(value)
  Configuration.runtime_settings_disabled = !!value
end

.runtime_settings_secure?Boolean

Check if the runtime settings engine is considered secure.

Returns:

  • (Boolean)


218
219
220
# File 'lib/ultra_settings.rb', line 218

def runtime_settings_secure?
  @runtime_settings_secure
end

.yaml_config_disabled=(value) ⇒ void

This method returns an undefined value.

Control if settings can be loaded from YAML configuration files. By default YAML configuration is enabled. This can also be disabled on individual Configuration classes.

Parameters:

  • value (Boolean)

    Whether or not to load settings from YAML configuration.



107
108
109
# File 'lib/ultra_settings.rb', line 107

def yaml_config_disabled=(value)
  Configuration.yaml_config_disabled = !!value
end

.yaml_config_env=(value) ⇒ void

This method returns an undefined value.

Set the environment to use when loading YAML configuration files. In a Rails application this will be the current Rails environment. Defaults to “development”.

Parameters:

  • value (String)

    The environment name to use.



117
118
119
# File 'lib/ultra_settings.rb', line 117

def yaml_config_env=(value)
  Configuration.yaml_config_env = value
end

.yaml_config_path=(value) ⇒ void

This method returns an undefined value.

Set the directory to use when loading YAML configuration files. In a Rails application this will be the config directory. Otherwise it will be the current working directory.

Parameters:

  • value (String, Pathname)

    The directory to use.



162
163
164
# File 'lib/ultra_settings.rb', line 162

def yaml_config_path=(value)
  Configuration.yaml_config_path = value.to_s
end