Browse Source

hot_constants reads .env.sample

main
pvincent 5 months ago
parent
commit
b616366ff2
  1. 10
      .env.sample
  2. 1
      app/controllers/scores_controller.rb
  3. 5
      config/environments/development.rb
  4. 8
      config/initializers/hot_constants.rb
  5. 3
      config/initializers/rails_live_reload.rb
  6. 37
      lib/hot_constants/hot_constants.rb
  7. 21
      lib/monkey_patches/rails_live_reload/watcher.rb

10
.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 '?''

1
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

5
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,

8
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

3
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

37
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

21
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
Loading…
Cancel
Save