]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/blobdiff - lib/rbot/config.rb
refactor: remove global bot instance, closes #42
[user/henk/code/ruby/rbot.git] / lib / rbot / config.rb
index 207264bf5e6ab6f081cd5bee31a903dc72ad4792..ef63a2feb06c36f1372e75d412d199814b633543 100644 (file)
@@ -24,6 +24,7 @@ module Config
     attr_reader :desc
     attr_reader :key
     attr_reader :wizard
+    attr_reader :store_default
     attr_reader :requires_restart
     attr_reader :requires_rescan
     attr_reader :order
@@ -52,6 +53,7 @@ module Config
       @on_change = params[:on_change]
       @validate = params[:validate]
       @wizard = params[:wizard]
+      @store_default = params[:store_default]
       @requires_restart = params[:requires_restart]
       @requires_rescan = params[:requires_rescan]
       @auth_path = "config::key::#{key.sub('.','::')}"
@@ -186,7 +188,7 @@ module Config
       unless newval.include? val
         newval << val
         validate_item(val) or raise ArgumentError, "invalid item: #{val}"
-        validate(newval) or raise ArgumentError, "invalid value: #{newval.to_s}"
+        validate(newval) or raise ArgumentError, "invalid value: #{newval.inspect}"
         set(newval)
       end
     end
@@ -262,9 +264,10 @@ module Config
       return unless @bot
 
       @changed = false
-      if(File.exist?("#{@bot.botclass}/conf.yaml"))
+      conf = @bot.path 'conf.yaml'
+      if File.exist? conf
         begin
-          newconfig = YAML::load_file("#{@bot.botclass}/conf.yaml")
+          newconfig = YAML::load_file conf
           newconfig.each { |key, val|
             @config[key.to_sym] = val
           }
@@ -273,6 +276,14 @@ module Config
           error "failed to read conf.yaml: #{$!}"
         end
       end
+      # config options with :store_default to true should store
+      # their default value at first run.
+      # Some defaults might change anytime the bot starts
+      #  for instance core.db or authpw
+      @items.values.find_all {|i| i.store_default }.each do |value|
+        @config[value.key] = value.default
+      end
+
       # if we got here, we need to run the first-run wizard
       Wizard.new(@bot).run
       # save newly created config
@@ -327,8 +338,10 @@ module Config
         return
       end
       begin
+       conf = @bot.path 'conf.yaml'
+       fnew = conf + '.new'
         debug "Writing new conf.yaml ..."
-        File.open("#{@bot.botclass}/conf.yaml.new", "w") do |file|
+        File.open(fnew, "w") do |file|
           savehash = {}
           @config.each { |key, val|
             savehash[key.to_s] = val
@@ -336,8 +349,7 @@ module Config
           file.puts savehash.to_yaml
         end
         debug "Officializing conf.yaml ..."
-        File.rename("#{@bot.botclass}/conf.yaml.new",
-                    "#{@bot.botclass}/conf.yaml")
+        File.rename(fnew, conf)
         @changed = false
       rescue => e
         error "failed to write configuration file conf.yaml! #{$!}"