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.

98 lines
2.9 KiB

5 months ago
5 months ago
5 months ago
4 months ago
5 months ago
4 months ago
4 months ago
5 months ago
4 months ago
4 months ago
5 months ago
5 months ago
5 months ago
5 months ago
  1. require 'dotenv'
  2. # Hot Live Constants
  3. module HotConstants
  4. HOTENV = {} # rubocop:disable Style/MutableConstant
  5. LISTENERS = {} # rubocop:disable Style/MutableConstant
  6. LOGGER = SemanticLogger[self]
  7. class << self
  8. def initialize
  9. @old_definitions = {}
  10. @hot_definitions = {}
  11. set_definitions
  12. load_values
  13. end
  14. def set_definitions
  15. LOGGER.info('--set_definitions')
  16. new_definitions = Dotenv.parse('.env.sample')
  17. definitions_to_actualize = {}
  18. @old_definitions.merge(new_definitions) do |key, old_value, new_value|
  19. definitions_to_actualize[key] = new_value if new_value != old_value
  20. end
  21. definitions_to_actualize.each { |key, value| define_method(:actualize, key, value) }
  22. @old_definitions = new_definitions
  23. definitions_to_remove = @hot_definitions.except(*new_definitions.keys)
  24. definitions_to_remove.each_pair do |key, dkey|
  25. LOGGER.info("remove method <#{dkey}>")
  26. singleton_class.undef_method(dkey)
  27. @hot_definitions.delete(key)
  28. end
  29. definitions_to_add = new_definitions.except(*@hot_definitions.keys)
  30. definitions_to_add.each { |key, value| define_method(:add, key, value) }
  31. end
  32. def load_values
  33. LOGGER.info('--load_values')
  34. new_env = Dotenv.parse
  35. constants_to_delete = HOTENV.except(*new_env.keys)
  36. constants_to_delete.each do |constant|
  37. LOGGER.info("constant <#{constant}> reverts to default value <#{}>")
  38. end
  39. constants_to_add = new_env.except(*HOTENV.keys)
  40. constants_to_add.each do |constant|
  41. LOGGER.info("constant to add <#{constant}>")
  42. end
  43. HOTENV.replace new_env
  44. # LISTENERS.each_pair { |k, b| perform_change(k, b) }
  45. end
  46. def on_change(key, &block)
  47. LISTENERS[key.downcase.to_sym] = block
  48. perform_change(key, block)
  49. end
  50. private
  51. def define_method(mode, key, value)
  52. dkey = key.downcase
  53. method = infer_method_from_value(value)
  54. inferred_type = method.name.to_s.split('_')[1]
  55. LOGGER.info("#{mode} method <#{dkey}> of type <#{inferred_type}> with default value <#{value}> ")
  56. singleton_class.define_method(dkey) { method.call(key, value) }
  57. @hot_definitions.store(key, dkey)
  58. end
  59. def perform_change(key, block)
  60. old_value = nil # TODO: remember last previous value
  61. new_value = method(key).call
  62. block.call(new_value, old_value)
  63. end
  64. def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
  65. def load_integer(key, default) = HOTENV.fetch(key, default).to_i
  66. def load_string(key, default) = HOTENV.fetch(key, default)
  67. def infer_method_from_value(value)
  68. case value.downcase
  69. when /^(true|false)$/ then method(:load_boolean)
  70. when /^\d+$/ then method(:load_integer)
  71. else method(:load_string)
  72. end
  73. end
  74. end
  75. initialize # done once on first require, cause this is just a module (not a class!)
  76. end