lib/runger/settings.rb in runger_config-4.0.0 vs lib/runger/settings.rb in runger_config-5.0.0

- old
+ new

@@ -1,111 +1,109 @@ # frozen_string_literal: true -require "pathname" +require 'pathname' -module Runger - # Use Settings name to not confuse with Config. - # - # Settings contain the library-wide configuration. - class Settings - # Future encapsulates settings that will be introduced in the upcoming version - # with the default values, which could break compatibility - class Future - class << self - def setting(name, default_value) - settings[name] = default_value +# Use Settings name to not confuse with Config. +# +# Settings contain the library-wide configuration. +class Runger::Settings + # Future encapsulates settings that will be introduced in the upcoming version + # with the default values, which could break compatibility + class Future + class << self + def setting(name, default_value) + settings[name] = default_value - define_method(name) do - store[name] - end - - define_method(:"#{name}=") do |val| - store[name] = val - end + define_method(name) do + store[name] end - def settings - @settings ||= {} + define_method(:"#{name}=") do |val| + store[name] = val end end - def initialize - @store = {} + def settings + @settings ||= {} end + end - def use(*names) - store.clear - names.each { store[_1] = self.class.settings[_1] } - end + def initialize + @store = {} + end - setting :unwrap_known_environments, true - - private - - attr_reader :store + def use(*names) + store.clear + names.each { store[_1] = self.class.settings[_1] } end - class << self - # Define whether to load data from - # *.yml.local (or credentials/local.yml.enc) - attr_accessor :use_local_files, - :current_environment, - :default_environmental_key, - :known_environments + setting :unwrap_known_environments, true - # A proc returning a path to YML config file given the config name - attr_reader :default_config_path + private - def default_config_path=(val) - if val.is_a?(Proc) - @default_config_path = val - return - end + attr_reader :store + end - val = val.to_s + class << self + # Define whether to load data from + # *.yml.local (or credentials/local.yml.enc) + attr_accessor :use_local_files, + :current_environment, + :default_environmental_key, + :known_environments - @default_config_path = ->(name) { File.join(val, "#{name}.yml") } + # A proc returning a path to YML config file given the config name + attr_reader :default_config_path + + def default_config_path=(val) + if val.is_a?(Proc) + @default_config_path = val + return end - # Enable source tracing - attr_accessor :tracing_enabled + val = val.to_s - def future - @future ||= Future.new - end + @default_config_path = ->(name) { File.join(val, "#{name}.yml") } + end - def app_root - Pathname.new(Dir.pwd) - end + # Enable source tracing + attr_accessor :tracing_enabled - def default_environmental_key? - !default_environmental_key.nil? - end + def future + @future ||= Future.new + end - def matching_env?(env) - return true if env.nil? || env.to_s == current_environment + def app_root + Pathname.new(Dir.pwd) + end - 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 + def default_environmental_key? + !default_environmental_key.nil? + end + + 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 + end - # By default, use RUNGER_ENV - self.current_environment = ENV["RUNGER_ENV"] + # By default, use RUNGER_ENV + self.current_environment = ENV.fetch('RUNGER_ENV', nil) - # By default, use local files only in development (that's the purpose if the local files) - self.use_local_files = (ENV["RUNGER_ENV"] == "development" || ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development" || (defined?(Rails) && Rails.env.development?)) + # By default, use local files only in development (that's the purpose if the local files) + self.use_local_files = (ENV['RUNGER_ENV'] == 'development' || ENV['RACK_ENV'] == 'development' || ENV['RAILS_ENV'] == 'development' || (defined?(Rails) && Rails.env.development?)) - # By default, consider configs are stored in the ./config folder - self.default_config_path = ->(name) { "./config/#{name}.yml" } + # By default, consider configs are stored in the ./config folder + self.default_config_path = ->(name) { "./config/#{name}.yml" } - # Tracing is enabled by default - self.tracing_enabled = true - end + # Tracing is enabled by default + self.tracing_enabled = true end