Sha256: e8321c1f4f6071923eb17552e667382f68081f9f7c7c84aad4d584cdf4ebcbcb

Contents?: true

Size: 651 Bytes

Versions: 2

Compression:

Stored size: 651 Bytes

Contents

module ServiceConfig
  class Provider
    def initialize(args)
      @raise_if_nil = args[:raise_if_nil]
      @use_env = args[:use_env]
      yield self
    end

    def provides(name, default_value = '')
      guard_against_unset_env(name) if @raise_if_nil

      self.class.send(:define_method, name) do
        lookup_value(name) || default_value
      end
    end

    private

    def guard_against_unset_env(name)
      env_variable_name = name.to_s.upcase

      unless ENV[env_variable_name]
        raise "must set #{env_variable_name}"
      end
    end

    def lookup_value(name)
      ENV[name.to_s.upcase] if @use_env
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
service_config-0.0.2 lib/service_config/provider.rb
service_config-0.0.1 lib/service_config/provider.rb