What is the correct way to use configurable variables in a Ruby gem to be used in a Rails app? We use timezone in this example which is likely a constant but would also be using installation-specific variables such as home_city
, etc.
Constants
Can set constants in an initializer:config/initializer/mygem.rb TIMEZONE = "Eastern Standard"
However, if used in an class variable declaration, the variable is not defined.
module Foo module Bar class TickTock class_attribute :tz self.tz = "#{TIMEZONE} Time zone" # uninitialized constant Foo::Bar::TickTock::TIMEZONE
Config variables
config/initializer/mygem.rb config.x.timezone = "Eastern Standard"
module Foo module Bar class TickTock self.tz = "#{Rails.configuration.x.timezone} Time zone" # undefined method `configuration` for Foo::Bar::TickTock:Module
Environment variables: We would prefer not use environment variables in the gem itself. We would like to avoid
self.tz = "#{ENV['TIMEZONE']} Time zone"
Other