class Irc::Bot::Config::ManagerClass
container for bot configuration
Attributes
bot[R]
changed[RW]
config[R]
items[R]
overrides[R]
Public Class Methods
new()
click to toggle source
# File lib/rbot/config.rb, line 236 def initialize bot_associate(nil,true) end
Public Instance Methods
[](key)
click to toggle source
currently we store values in a hash but this could be changed in the future. We use hash semantics, however. components that register their config keys and setup defaults are supported via []
# File lib/rbot/config.rb, line 305 def [](key) # return @items[key].value if @items.has_key?(key) return @items[key.to_sym].value if @items.has_key?(key.to_sym) # try to still support unregistered lookups # but warn about them # if @config.has_key?(key) # warning "Unregistered lookup #{key.inspect}" # return @config[key] # end if @config.has_key?(key.to_sym) warning _("Unregistered lookup #{key.to_sym.inspect}") return @config[key.to_sym] end return false end
[]=(key, value)
click to toggle source
# File lib/rbot/config.rb, line 321 def []=(key, value) return @items[key.to_sym].set(value) if @items.has_key?(key.to_sym) if @config.has_key?(key.to_sym) warning _("Unregistered lookup #{key.to_sym.inspect}") return @config[key.to_sym] = value end end
bot_associate(bot, reset=false)
click to toggle source
Associate with bot bot
# File lib/rbot/config.rb, line 261 def bot_associate(bot, reset=false) reset_config if reset @bot = bot return unless @bot @changed = false conf = @bot.path 'conf.yaml' if File.exist? conf begin newconfig = YAML::load_file conf newconfig.each { |key, val| @config[key.to_sym] = val } return rescue 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 @changed = true save end
method_missing(method, *args, &block)
click to toggle source
pass everything else through to the hash
# File lib/rbot/config.rb, line 330 def method_missing(method, *args, &block) return @config.send(method, *args, &block) end
register(item)
click to toggle source
# File lib/rbot/config.rb, line 294 def register(item) unless item.kind_of?(Value) raise ArgumentError,"item must be an Irc::Bot::Config::Value" end @items[item.key] = item end
reset_config()
click to toggle source
# File lib/rbot/config.rb, line 240 def reset_config @items = Hash.new @config = Hash.new(false) # We allow default values for config keys to be overridden by # the config file /etc/rbot.conf # The main purpose for this is to allow distro or system-wide # settings such as external program paths (figlet, toilet, ispell) # to be set once for all the bots. @overrides = Hash.new etcfile = "/etc/rbot.conf" if File.exist?(etcfile) log "Loading defaults from #{etcfile}" etcconf = YAML::load_file(etcfile) etcconf.each { |k, v| @overrides[k.to_sym] = v } end end
save()
click to toggle source
write current configuration to #{botclass}/conf.yaml
# File lib/rbot/config.rb, line 335 def save if not @changed debug "Not writing conf.yaml (unchanged)" return end begin conf = @bot.path 'conf.yaml' fnew = conf + '.new' debug "Writing new conf.yaml ..." File.open(fnew, "w") do |file| savehash = {} @config.each { |key, val| savehash[key.to_s] = val } file.puts savehash.to_yaml end debug "Officializing conf.yaml ..." File.rename(fnew, conf) @changed = false rescue => e error "failed to write configuration file conf.yaml! #{$!}" error "#{e.class}: #{e}" error e.backtrace.join("\n") end end