Sha256: 9fb22147887708ad80ae55a8e38e28604916dab75e56fbddd921bdb62acf0d1a
Contents?: true
Size: 1.74 KB
Versions: 15
Compression:
Stored size: 1.74 KB
Contents
module RubyPitaya class Config def initialize(configs_folder_path) @configs_folder_path = configs_folder_path path_to_all_files = File.join(@configs_folder_path, '**/*.json') config_files = Dir.glob(path_to_all_files) @config = {} config_files.each do |config_file| load_config_file(config_file) end end def get @config end def [](key) @config[key] end def auto_reload require 'listen' @config_files_listener = Listen.to(@configs_folder_path, only: /\.json$/) do |modified, added, removed| import_added_files(added) reload_modified_files(modified) end @config_files_listener.start end private def load_config_file(file_path) config_text = File.open(file_path, &:read) config_hash = JSON.parse(config_text) path_array = file_path.sub(/^#{@configs_folder_path}/, '')[0..-6] .split('/') set_config_value(path_array, config_hash) rescue Exception => error puts "ERROR: #{error}" puts error.backtrace end def import_added_files(files_path) files_path.each do |path| load_config_file(path) puts "ADDED config: #{path}" end end def reload_modified_files(files_path) files_path.each do |path| load_config_file(path) puts "MODIFIED @config: #{path}" end end def set_config_value(keys, value) config = @config keys.each_with_index do |key, index| is_last_index = index == keys.size - 1 if is_last_index config[key] = value else config[key] = {} config = config[key] end end end end end
Version data entries
15 entries across 15 versions & 1 rubygems