From 3d85327c1dd8c2dd1b7068f04c9076d7fe991988 Mon Sep 17 00:00:00 2001 From: Matthias Hecker Date: Mon, 20 Apr 2020 19:25:52 +0200 Subject: [PATCH] plugin(alias): use registry for storage see #42 --- data/rbot/plugins/alias.rb | 39 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/data/rbot/plugins/alias.rb b/data/rbot/plugins/alias.rb index 89be0020..67f481dc 100644 --- a/data/rbot/plugins/alias.rb +++ b/data/rbot/plugins/alias.rb @@ -38,32 +38,29 @@ class AliasPlugin < Plugin def initialize super - @data_path = datafile - @data_file = File.join(@data_path, 'aliases.yaml') - # hash of alias => command entries - data = nil - aliases = if File.exist?(@data_file) && - (data = YAML.load_file(@data_file)) && - data.respond_to?(:each_pair) - data - else - warning _("Data file is not found or corrupt, reinitializing data") - Hash.new - end - @aliases = Hash.new - aliases.each_pair do |a, c| - begin - add_alias(a, c) - rescue AliasDefinitionError - warning _("Invalid alias entry %{alias} : %{command} in %{filename}: %{reason}") % - {:alias => a, :command => c, :filename => @data_file, :reason => $1} + @aliases = @registry[:aliases] + unless @aliases + # attempt to load aliases from data file yaml + filename = File.join(datafile, 'aliases.yaml') + if File.exists? filename + begin + @aliases = {} + YAML.load_file(filename).each_pair do |a, c| + add_alias(a, c) + end + rescue + warning _("Data file is not found or corrupt, reinitializing data") + @aliases = {} + end + else + @aliases = {} end end end def save - FileUtils.mkdir_p(@data_path) - Utils.safe_save(@data_file) {|f| f.write @aliases.to_yaml} + @registry[:aliases] = @aliases + @registry.flush end def cmd_add(m, params) -- 2.39.2