Class: DiceBag::Configuration::PrefixedWithFallback

Inherits:
Object
  • Object
show all
Defined in:
lib/dice_bag/configuration.rb

Overview

This class acts like Configuration but with a prefix applied to the environment variables used to provide values. This is useful for providing per-environment overrides to value in Rails projects. If the prefixed environment variable is not found, the class delegates to the provided fallback Configuration class, without the prefix.

Instance Method Summary collapse

Constructor Details

#initialize(prefix, fallback) ⇒ PrefixedWithFallback

Returns a new instance of PrefixedWithFallback.


29
30
31
32
# File 'lib/dice_bag/configuration.rb', line 29

def initialize(prefix, fallback)
  @prefix = prefix.to_s.upcase
  @fallback = fallback
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object


34
35
36
# File 'lib/dice_bag/configuration.rb', line 34

def method_missing(name)
  ENV["#{ @prefix }_#{ name.to_s.upcase }"] || @fallback.send(name)
end