Sha256: 4978c73b50111d70bb8e0bbeb7539a25c6257720fa9ff7812557e9549fac5b00

Contents?: true

Size: 1.52 KB

Versions: 7

Compression:

Stored size: 1.52 KB

Contents

class ENVied
  # Responsible for anything related to the ENV.
  class EnvProxy
    attr_reader :config, :coercer, :groups
    private :config, :coercer, :groups

    def initialize(config, **options)
      @config = config
      @coercer = options.fetch(:coercer, ENVied::Coercer.new)
      @groups = options.fetch(:groups, [])
    end

    def missing_variables
      variables.select(&method(:missing?))
    end

    def uncoercible_variables
      variables.reject(&method(:coerced?)).reject(&method(:coercible?))
    end

    def [](name)
      coerce(variables_by_name[name.to_sym])
    end

    def has_key?(name)
      variables_by_name[name.to_sym]
    end

    def value_to_coerce(var)
      return env_value_of(var) unless env_value_of(var).nil?
      config.defaults_enabled? ? default_value_of(var) : nil
    end

    private

    def coerce(var)
      coerced?(var) ?
        value_to_coerce(var) :
        coercer.coerce(value_to_coerce(var), var.type)
    end

    def coerced?(var)
      coercer.coerced?(value_to_coerce(var))
    end

    def coercible?(var)
      coercer.coercible?(value_to_coerce(var), var.type)
    end

    def default_value_of(var)
      var.default_value(ENVied, var)
    end

    def env_value_of(var)
      ENV[var.name.to_s]
    end

    def missing?(var)
      value_to_coerce(var).nil?
    end

    def variables
      @variables ||= config.variables.select {|v| groups.include?(v.group) }
    end

    def variables_by_name
      @variables_by_name ||= variables.map {|v| [v.name, v] }.to_h
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
envied-1.0.0 lib/envied/env_proxy.rb
envied-0.11.0 lib/envied/env_proxy.rb
envied-0.10.0 lib/envied/env_proxy.rb
envied-0.9.3 lib/envied/env_proxy.rb
envied-0.9.2 lib/envied/env_proxy.rb
envied-0.9.2.rc2 lib/envied/env_proxy.rb
envied-0.9.2.rc1 lib/envied/env_proxy.rb