From b616366ff2224ae50ba082957928bc285ed8ead3 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 11 Jul 2024 22:00:52 +0400 Subject: [PATCH] hot_constants reads .env.sample --- .env.sample | 10 +++-- app/controllers/scores_controller.rb | 1 + config/environments/development.rb | 5 --- config/initializers/hot_constants.rb | 8 ++++ config/initializers/rails_live_reload.rb | 3 +- lib/hot_constants/hot_constants.rb | 37 +++++++++++++------ .../rails_live_reload/watcher.rb | 21 +++++++++-- 7 files changed, 61 insertions(+), 24 deletions(-) create mode 100644 config/initializers/hot_constants.rb diff --git a/.env.sample b/.env.sample index 06fe8d1..07cbeee 100644 --- a/.env.sample +++ b/.env.sample @@ -11,8 +11,10 @@ #-------------- STIMULUS_DEBUG=false LOG_ACTIVE_RECORD=false -LOG_ACTION_VIEW=false +LOG_ACTION_VIEW=true -INTEGER=1 -STRING="" -BOOLEAN=true +# INTEGER=2 +# STRING= +# BOOLEAN=true + +# IDEA: optional nil ends with '?'' \ No newline at end of file diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 6514923..1e62852 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -7,6 +7,7 @@ class ScoresController < ApplicationController # GET /scores def index @pagy, @scores = pagy(Score.all) + # logger.info("#{HotConstants.boolean}") end # GET /scores/1 diff --git a/config/environments/development.rb b/config/environments/development.rb index 12d3909..3369673 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -77,11 +77,6 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength routes.default_url_options[:port] = ARGV[1] # ie: Procfile.dev --port PORT routes.default_url_options[:host] = '127.0.0.1' - config.after_initialize do - require Rails.root.join('lib', 'hot_constants', 'hot_constants') - HotConstants.reload! - end - require Rails.root.join('lib', 'formatters', 'ansi_formatter') formatter = AnsiFormatter.new config.semantic_logger.add_appender(io: $stdout, diff --git a/config/initializers/hot_constants.rb b/config/initializers/hot_constants.rb new file mode 100644 index 0000000..ac039ae --- /dev/null +++ b/config/initializers/hot_constants.rb @@ -0,0 +1,8 @@ +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 diff --git a/config/initializers/rails_live_reload.rb b/config/initializers/rails_live_reload.rb index b8cdfa8..bd48385 100644 --- a/config/initializers/rails_live_reload.rb +++ b/config/initializers/rails_live_reload.rb @@ -3,7 +3,8 @@ if defined?(RailsLiveReload) && Rails.env.development? RailsLiveReload.configure do |config| # HOT Constants - config.watch(%r{/\.env}, reload: :always) + config.watch(%r{/\.env$}, reload: :always) + config.watch(%r{/\.env\.sample$}) # USEFUL for tailwind changes!!!! config.watch %r{app/assets/builds/tailwind.css}, reload: :always diff --git a/lib/hot_constants/hot_constants.rb b/lib/hot_constants/hot_constants.rb index f7ff5ea..2d513be 100644 --- a/lib/hot_constants/hot_constants.rb +++ b/lib/hot_constants/hot_constants.rb @@ -7,25 +7,40 @@ module HotConstants class << self def initialize - LOGGER.info('initializing') + env_sample = Dotenv.parse('.env.sample') + env_sample.each do |key, value| + dkey = key.downcase + method = method_from_value(value) + 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 stimulus_debug = load_boolean('STIMULUS_DEBUG', false) - def log_active_record = load_boolean('LOG_ACTIVE_RECORD', true) - def log_action_view = load_boolean('LOG_ACTION_VIEW', true) - def reload! - HOTENV.replace Dotenv.parse - # LOGGER.warn 'reloaded!' + HOTENV.replace Dotenv.parse # detect true changes before processing block below (or yield) - ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal - ActionView::Base.logger.level = log_action_view ? :debug : :fatal + # TODO: replace with yield, thus externaliz process_block on behalf of HotEnv#initialize + process_block end private def load_boolean(key, default) = HOTENV.fetch(key, default).to_s.downcase == 'true' - end + def load_integer(key, default) = HOTENV.fetch(key, default).to_i + def load_string(key, default) = HOTENV.fetch(key, default) + + def method_from_value(value) + case value.downcase + when /^(true|false)$/ then method(:load_boolean) + when /^\d+$/ then method(:load_integer) + else method(:load_string) + end + end - reload! + def process_block + ActiveRecord::Base.logger.level = log_active_record ? :debug : :fatal + ActionView::Base.logger.level = log_action_view ? :debug : :fatal + end + end end diff --git a/lib/monkey_patches/rails_live_reload/watcher.rb b/lib/monkey_patches/rails_live_reload/watcher.rb index d7b858b..d5def0e 100644 --- a/lib/monkey_patches/rails_live_reload/watcher.rb +++ b/lib/monkey_patches/rails_live_reload/watcher.rb @@ -1,4 +1,8 @@ module RailsLiveReload + ENV_FILE = Rails.root.join('.env').to_s + ENV_SAMPLE_FILE = Rails.root.join('.env.sample').to_s + CHECKSUMS = {} + # MonkeyPath Watcher class Watcher def reload_all @@ -9,10 +13,21 @@ module RailsLiveReload private - ENV_FILE = Rails.root.join('.env').to_s - def before_reload(files) - HotConstants.reload! if files.find { |change| change[0] == ENV_FILE } + 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) + + current_checksum = files[ENV_FILE] + return unless current_checksum != CHECKSUMS[ENV_FILE] + + HotConstants.reload! + CHECKSUMS[ENV_FILE] = current_checksum end end end