Browse Source

fix listeners

main
pvincent 4 months ago
parent
commit
22604f8563
  1. 8
      lib/hot_constants/hot_constants.rb

8
lib/hot_constants/hot_constants.rb

@ -3,11 +3,11 @@ require 'dotenv'
# Hot Live Constants
module HotConstants
HOTENV = {} # rubocop:disable Style/MutableConstant
LISTENERS = {} # rubocop:disable Style/MutableConstant
LOGGER = SemanticLogger[to_s]
class << self
def initialize
@on_change_listeners = {}
env_sample = Dotenv.parse('.env.sample') # TODO: able to detect add/remove, then either call define_method or undef_method!
env_sample.each do |key, value|
dkey = key.downcase
@ -20,15 +20,15 @@ module HotConstants
def reload!
HOTENV.replace Dotenv.parse # TODO: detect true changes before processing block below (or yield)
@on_change_listeners.each_pair do |key, block|
LISTENERS.each_pair do |key, block|
old_value = nil # TODO: remember last previous value
new_value = method(key).call
block.call(new_value)
block.call(new_value, old_value)
end
end
def on_change(key, &block)
@on_change_listeners[key.downcase.to_sym] = block
LISTENERS[key.downcase.to_sym] = block
end
private

Loading…
Cancel
Save