Sha256: 87a50241d2e1c6c63949a73d621b21913a8ec487986893dfed31c1f7c16f9b38
Contents?: true
Size: 1.76 KB
Versions: 1
Compression:
Stored size: 1.76 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] = {} unless config.key?(key) config = config[key] end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rubypitaya-1.8.2 | ./lib/rubypitaya/core/config.rb |