|
|
@ -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 |
|
|
|