class Anyway::Settings

Use Settings name to not confuse with Config.

Settings contain the library-wide configuration.

Attributes

autoload_static_config_path[R]
autoload_via_zeitwerk[W]
autoloader[R]
current_environment[RW]

Define whether to load data from *.yml.local (or credentials/local.yml.enc)

default_config_path[R]

A proc returning a path to YML config file given the config name

default_environmental_key[RW]

Define whether to load data from *.yml.local (or credentials/local.yml.enc)

known_environments[RW]

Define whether to load data from *.yml.local (or credentials/local.yml.enc)

tracing_enabled[RW]

Enable source tracing

use_local_files[RW]

Define whether to load data from *.yml.local (or credentials/local.yml.enc)

Public Class Methods

app_root() click to toggle source
# File lib/anyway/rails/settings.rb, line 72
def app_root
  ::Rails.root
end
autoload_static_config_path=(val) click to toggle source
# File lib/anyway/rails/settings.rb, line 16
def autoload_static_config_path=(val)
  if autoload_via_zeitwerk
    raise "Cannot setup autoloader after application has been initialized" if ::Rails.application.initialized?

    return unless ::Rails.root.join(val).exist?

    return if val == autoload_static_config_path

    autoloader&.unload

    @autoload_static_config_path = val

    # See Rails 6 https://github.com/rails/rails/blob/8ab4fd12f18203b83d0f252db96d10731485ff6a/railties/lib/rails/autoloaders.rb#L10
    # and Rails 7 https://github.com/rails/rails/blob/5462fbd5de1900c1b1ce1c9dc11c1a2d8cdcd809/railties/lib/rails/autoloaders.rb#L15
    @autoloader = Zeitwerk::Loader.new.tap do |loader|
      loader.tag = "anyway.config"

      if defined?(ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector)
        loader.inflector = ActiveSupport::Dependencies::ZeitwerkIntegration::Inflector
      elsif defined?(::Rails::Autoloaders::Inflector)
        loader.inflector = ::Rails::Autoloaders::Inflector
      end
      loader.push_dir(::Rails.root.join(val))
      loader.setup
    end
  else
    if autoload_static_config_path
      old_path = ::Rails.root.join(autoload_static_config_path).to_s
      ActiveSupport::Dependencies.autoload_paths.delete(old_path)
      ::Rails.application.config.eager_load_paths.delete(old_path)
    end

    @autoload_static_config_path = val
    new_path = ::Rails.root.join(val).to_s
    ActiveSupport::Dependencies.autoload_paths << new_path
    ::Rails.application.config.eager_load_paths << new_path
  end
end
autoload_via_zeitwerk() click to toggle source
# File lib/anyway/rails/settings.rb, line 62
def autoload_via_zeitwerk
  return @autoload_via_zeitwerk if instance_variable_defined?(:@autoload_via_zeitwerk)

  @autoload_via_zeitwerk = defined?(::Zeitwerk)
end
cleanup_autoload_paths() click to toggle source
# File lib/anyway/rails/settings.rb, line 55
def cleanup_autoload_paths
  return unless autoload_via_zeitwerk

  return unless autoload_static_config_path
  ActiveSupport::Dependencies.autoload_paths.delete(::Rails.root.join(autoload_static_config_path).to_s)
end
current_environment() click to toggle source

Define whether to load data from *.yml.local (or credentials/local.yml.enc)

# File lib/anyway/rails/settings.rb, line 68
def current_environment
  @current_environment || ::Rails.env.to_s
end
default_config_path=(val) click to toggle source
# File lib/anyway/settings.rb, line 58
def default_config_path=(val)
  if val.is_a?(Proc)
    @default_config_path = val
    return
  end

  val = val.to_s

  @default_config_path = ->(name) { File.join(val, "#{name}.yml") }
end
default_environmental_key?() click to toggle source
# File lib/anyway/settings.rb, line 80
def default_environmental_key?
  !default_environmental_key.nil?
end
future() click to toggle source
# File lib/anyway/settings.rb, line 72
def future
  @future ||= Future.new
end
matching_env?(env) click to toggle source
# File lib/anyway/settings.rb, line 84
def matching_env?(env)
  return true if env.nil? || env.to_s == current_environment

  if env.is_a?(::Hash)
    envs = env[:except]
    excluded_envs = [envs].flat_map(&:to_s)
    excluded_envs.none?(current_environment)
  elsif env.is_a?(::Array)
    env.flat_map(&:to_s).include?(current_environment)
  else
    false
  end
end