You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.5 KiB

3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. require 'dotenv'
  2. # Hot Live Constants
  3. module HotConstants
  4. HOTENV = {} # rubocop:disable Style/MutableConstant
  5. LOGGER = SemanticLogger[to_s]
  6. class << self
  7. def initialize
  8. env_sample = Dotenv.parse('.env.sample') # TODO: able to detect add/remove, then either call define_method or undef_method!
  9. env_sample.each do |key, value|
  10. dkey = key.downcase
  11. method = method_from_value(value)
  12. singleton_class.define_method(dkey) { method.call(key, value) }
  13. LOGGER.info("create method <#{dkey}> performing <#{method.name}> with default value <#{value}> ")
  14. end
  15. reload!
  16. end
  17. def reload!
  18. HOTENV.replace Dotenv.parse # detect true changes before processing block below (or yield)
  19. # TODO: replace with yield, thus externaliz process_block on behalf of HotEnv#initialize
  20. process_block
  21. end
  22. private
  23. def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
  24. def load_integer(key, default) = HOTENV.fetch(key, default).to_i
  25. def load_string(key, default) = HOTENV.fetch(key, default)
  26. def method_from_value(value)
  27. case value.downcase
  28. when /^(true|false)$/ then method(:load_boolean)
  29. when /^\d+$/ then method(:load_integer)
  30. else method(:load_string)
  31. end
  32. end
  33. def process_block
  34. ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal
  35. ActionView::Base.logger.level = log_action_view ? :debug : :fatal
  36. end
  37. end
  38. end