Sha256: 00036afb9cfdd6051798c70bcff61ebe17b51533db5291450b86f2ce55bce5f8

Contents?: true

Size: 1.89 KB

Versions: 5

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Anyway
  # 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

          define_method(name) do
            store[name]
          end

          define_method(:"#{name}=") do |val|
            store[name] = val
          end
        end

        def settings
          @settings ||= {}
        end
      end

      def initialize
        @store = {}
      end

      def use(*names)
        store.clear
        names.each { store[_1] = self.class.settings[_1] }
      end

      private

      attr_reader :store
    end

    class << self
      # Define whether to load data from
      # *.yml.local (or credentials/local.yml.enc)
      attr_accessor :use_local_files

      # 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

        val = val.to_s

        @default_config_path = ->(name) { File.join(val, "#{name}.yml") }
      end

      # Enable source tracing
      attr_accessor :tracing_enabled

      def future
        @future ||= Future.new
      end
    end

    # By default, use local files only in development (that's the purpose if the local files)
    self.use_local_files = (ENV["RACK_ENV"] == "development" || ENV["RAILS_ENV"] == "development")

    # 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
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
anyway_config-2.2.3 lib/anyway/settings.rb
anyway_config-2.2.2 lib/anyway/settings.rb
anyway_config-2.2.1 lib/anyway/settings.rb
anyway_config-2.2.0 lib/anyway/settings.rb
anyway_config-2.1.0 lib/anyway/settings.rb