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.

101 lines
3.1 KiB

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