Browse Source

refactor on_changes

main
pvincent 3 months ago
parent
commit
a0f1e2395a
  1. 7
      app/controllers/scores_controller.rb
  2. 1
      config/initializers/hot_changes.rb
  3. 1
      config/initializers/rails_live_reload.rb
  4. 15
      lib/hot_constants/hot_constants.rb
  5. 24
      lib/monkey_patches/rails_live_reload/watcher.rb

7
app/controllers/scores_controller.rb

@ -7,10 +7,9 @@ class ScoresController < ApplicationController
# GET /scores
def index
@pagy, @scores = pagy(Score.all)
# logger.info("#{HotConstants.boolean}")
logger.info('start', dimensions: AnsiDimensions.start)
logger.info('end', dimensions: AnsiDimensions.end)
logger.info('around', dimensions: AnsiDimensions.around)
# logger.info('start', dimensions: AnsiDimensions.start)
# logger.info('end', dimensions: AnsiDimensions.end)
# logger.info('around', dimensions: AnsiDimensions.around)
end
# GET /scores/1

1
config/initializers/hot_constants.rb → config/initializers/hot_changes.rb

@ -2,6 +2,5 @@ return unless defined?(Rails::Server)
require Rails.root.join('lib', 'hot_constants', 'hot_constants')
HotConstants.initialize
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 }

1
config/initializers/rails_live_reload.rb

@ -5,6 +5,7 @@ if defined?(RailsLiveReload) && Rails.env.development?
# HOT Constants
config.watch(%r{/\.env$}, reload: :always)
config.watch(%r{/\.env\.sample$})
config.watch(%r{/config/initializers/hot_changes.rb$})
# USEFUL for tailwind changes!!!!
config.watch %r{app/assets/builds/tailwind.css}, reload: :always

15
lib/hot_constants/hot_constants.rb

@ -15,24 +15,28 @@ module HotConstants
singleton_class.define_method(dkey) { method.call(key, value) }
LOGGER.info("create method <#{dkey}> performing <#{method.name}> with default value <#{value}> ")
end
reload!
end
def reload!
HOTENV.replace Dotenv.parse # TODO: detect true changes before processing block below (or yield)
LISTENERS.each_pair do |key, block|
old_value = nil # TODO: remember last previous value
new_value = method(key).call
block.call(new_value, old_value)
perform_change(key, block)
end
end
def on_change(key, &block)
LISTENERS[key.downcase.to_sym] = block
perform_change(key, block)
end
private
def perform_change(key, block)
old_value = nil # TODO: remember last previous value
new_value = method(key).call
block.call(new_value, old_value)
end
def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true'
def load_integer(key, default) = HOTENV.fetch(key, default).to_i
def load_string(key, default) = HOTENV.fetch(key, default)
@ -45,4 +49,7 @@ module HotConstants
end
end
end
# initialize on first require
initialize
end

24
lib/monkey_patches/rails_live_reload/watcher.rb

@ -1,6 +1,7 @@
module RailsLiveReload
ENV_FILE = Rails.root.join('.env').to_s
ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s
INITIALIZER = Rails.root.join('config/initializers/hot_changes.rb').to_s
CHECKSUMS = {}
# MonkeyPath Watcher
@ -14,20 +15,19 @@ module RailsLiveReload
private
def before_reload(files)
if files.include?(ENV_SAMPLE_FILE)
current_checksum = files[ENV_SAMPLE_FILE]
if current_checksum != CHECKSUMS[ENV_SAMPLE_FILE]
HotConstants.initialize
CHECKSUMS[ENV_SAMPLE_FILE] = current_checksum
end
end
return unless files.include?(ENV_FILE)
perform_when_change(files, ENV_SAMPLE_FILE) { HotConstants.initialize }
perform_when_change(files, ENV_FILE) { HotConstants.reload! }
perform_when_change(files, INITIALIZER) { load Rails.root.join('config', 'initializers', 'hot_changes.rb') }
end
def perform_when_change(files, key, &)
return unless files.include?(key)
current_checksum = files[ENV_FILE]
return unless current_checksum != CHECKSUMS[ENV_FILE]
current_checksum = files[key]
return unless current_checksum != CHECKSUMS[key]
HotConstants.reload!
CHECKSUMS[ENV_FILE] = current_checksum
yield
CHECKSUMS[key] = current_checksum
end
end
end
Loading…
Cancel
Save