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.

100 lines
3.1 KiB

3 months ago
3 months ago
3 months ago
2 months ago
3 months ago
2 months ago
2 months ago
3 months ago
2 months ago
2 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. 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 |name, _|
  37. # FIXME: default should read default and type from @old_definitions
  38. type, default = @old_definitions[name]
  39. LOGGER.info("constant <#{name}> reverts to default value <#{default}> of type <#{type}>")
  40. end
  41. constants_to_add = new_env.except(*HOTENV.keys)
  42. constants_to_add.each do |constant|
  43. LOGGER.info("constant to add <#{constant}>")
  44. end
  45. HOTENV.replace new_env
  46. # LISTENERS.each_pair { |k, b| perform_change(k, b) }
  47. end
  48. def on_change(key, &block)
  49. LISTENERS[key.downcase.to_sym] = block
  50. perform_change(key, block)
  51. end
  52. private
  53. def define_method(mode, key, value)
  54. dkey = key.downcase
  55. method = infer_method_from_value(value)
  56. inferred_type = method.name.to_s.split('_')[1]
  57. LOGGER.info("#{mode} method <#{dkey}> of type <#{inferred_type}> with default value <#{value}> ")
  58. singleton_class.define_method(dkey) { method.call(key, value) }
  59. @hot_definitions.store(key, dkey)
  60. end
  61. def perform_change(key, block)
  62. old_value = nil # TODO: remember last previous value
  63. new_value = method(key).call
  64. block.call(new_value, old_value)
  65. end
  66. def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
  67. def load_integer(key, default) = HOTENV.fetch(key, default).to_i
  68. def load_string(key, default) = HOTENV.fetch(key, default)
  69. def infer_method_from_value(value)
  70. case value.downcase
  71. when /^(true|false)$/ then method(:load_boolean)
  72. when /^\d+$/ then method(:load_integer)
  73. else method(:load_string)
  74. end
  75. end
  76. end
  77. initialize # done once on first require, cause this is just a module (not a class!)
  78. end