Sha256: 835de283d054a39ac3244a88bf18d87c6222f0fea243e19f28a04a038b316a53

Contents?: true

Size: 1.37 KB

Versions: 2

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module RrxConfig
  class Environment < ActiveSupport::StringInquirer
    RRX_ENVIRONMENT_VARIABLE = 'RRX_ENVIRONMENT'
    RRX_ENVIRONMENT_DEFAULT  = 'development'

    SHORT_NAMES = {
      'development' => 'dev',
      'staging'     => 'stg',
      'production'  => 'prd'
    }.freeze

    LONG_NAMES = SHORT_NAMES.invert.freeze

    # @param [String, nil] name Deployment environment name
    def initialize(name = nil)
      value = normalize(name || from_env)
      super(value)
      validate!
      @force_suffix = "-#{short}"
      @suffix = production? ? '' : @force_suffix
    end

    def short
      SHORT_NAMES[to_s]
    end

    def suffix(force = false) # rubocop:disable Style/OptionalBooleanParameter
      force ? @force_suffix : @suffix
    end

    def validate!
      raise RrxConfig::EnvironmentError, self unless SHORT_NAMES.key?(self)
    end

    class << self
      def instance
        @env ||= get_env
      end

      def get_env
        Environment.new
      end

      ##
      # Spec helper for testing +env+
      def reset
        @env = nil
      end
    end

    private

    def normalize(name)
      LONG_NAMES[name] || name
    end

    def from_env
      ENV.fetch(RRX_ENVIRONMENT_VARIABLE, RRX_ENVIRONMENT_DEFAULT)
    end
  end

  ##
  # Primary accessor for getting the environment
  def self.env
    Environment.instance
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rrx_config-0.1.1 lib/rrx_config/environment.rb
rrx_config-0.1.0 lib/rrx_config/environment.rb