class DiceBag::Configuration
This class abstracts access to configuration values, to be used within ERB templates. Currently, the values are read from the environment, as per the Twelve-Factor App principles.
Public Class Methods
new()
click to toggle source
# File lib/dice_bag/configuration.rb, line 8 def initialize @prefixed = {} end
Public Instance Methods
[](prefix)
click to toggle source
Returns a Configuration::PrefixedWithFallback
using the given prefix
.
# File lib/dice_bag/configuration.rb, line 14 def [](prefix) @prefixed[prefix] ||= PrefixedWithFallback.new(prefix, self) end
method_missing(name)
click to toggle source
# File lib/dice_bag/configuration.rb, line 18 def method_missing(name) if name.to_s.end_with?("!") ensured_in_production(name) else ENV[name.to_s.upcase] end end
Private Instance Methods
ensured_in_production(name)
click to toggle source
# File lib/dice_bag/configuration.rb, line 28 def ensured_in_production(name) variable_name = name.to_s.chomp("!").upcase value = ENV[variable_name] if in_production? && value.nil? raise "Environment variable #{variable_name} required in production but it was not provided" end value end
in_production?()
click to toggle source
# File lib/dice_bag/configuration.rb, line 38 def in_production? (defined?(Rails) && Rails.env.production?) || (defined?(Sinatra) && Sinatra::Application.production?) || ENV["RACK_ENV"] == "production" end