diff --git a/.env.sample b/.env.sample index 07cbeee..8b6b453 100644 --- a/.env.sample +++ b/.env.sample @@ -11,7 +11,7 @@ #-------------- STIMULUS_DEBUG=false LOG_ACTIVE_RECORD=false -LOG_ACTION_VIEW=true +LOG_ACTION_VIEW=false # INTEGER=2 # STRING= diff --git a/config/initializers/hot_constants.rb b/config/initializers/hot_constants.rb index ac039ae..101f809 100644 --- a/config/initializers/hot_constants.rb +++ b/config/initializers/hot_constants.rb @@ -3,6 +3,5 @@ return unless defined?(Rails::Server) require Rails.root.join('lib', 'hot_constants', 'hot_constants') HotConstants.initialize - -logger.info('hot constants initialized!') -# TODO: provide kind of a callback method which provides new value of hot constant change +HotConstants.on_change(:log_active_record) { |bool| ActiveRecord::Base.logger.level = bool ? :debug : :fatal } +HotConstants.on_change(:log_action_view) { |bool| ActionView::Base.logger.level = bool ? :debug : :fatal } diff --git a/lib/hot_constants/hot_constants.rb b/lib/hot_constants/hot_constants.rb index e7bbdff..84ee5ec 100644 --- a/lib/hot_constants/hot_constants.rb +++ b/lib/hot_constants/hot_constants.rb @@ -7,6 +7,7 @@ module HotConstants 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 @@ -18,10 +19,16 @@ module HotConstants end def reload! - HOTENV.replace Dotenv.parse # 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| + old_value = nil # TODO: remember last previous value + new_value = method(key).call + block.call(new_value) + end + end - # TODO: replace with yield, thus externaliz process_block on behalf of HotEnv#initialize - process_block + def on_change(key, &block) + @on_change_listeners[key.downcase.to_sym] = block end private @@ -37,10 +44,5 @@ module HotConstants else method(:load_string) end end - - def process_block - ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal - ActionView::Base.logger.level = log_action_view ? :debug : :fatal - end end end