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

Loading…
Cancel
Save