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.



48
49
50
51
# File 'lib/dice_bag/configuration.rb', line 48

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



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

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