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.



51
52
53
54
# File 'lib/dice_bag/configuration.rb', line 51

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



56
57
58
# File 'lib/dice_bag/configuration.rb', line 56

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