- class BotConfig
- # Array of registered BotConfigValues for defaults, types and help
- @@items = Hash.new
- def BotConfig.items
- @@items
- end
- # Hash containing key => value pairs for lookup and serialisation
- @@config = Hash.new(false)
- def BotConfig.config
- @@config
- end
- def BotConfig.bot
- @@bot
- end
-
- def BotConfig.register(item)
- unless item.kind_of?(BotConfigValue)
- raise ArgumentError,"item must be a BotConfigValue"
- end
- @@items[item.key] = item
- end
-
- # 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 []
- def [](key)
- return @@items[key].value if @@items.has_key?(key)
- return @@items[key.intern].value if @@items.has_key?(key.intern)
- # 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.intern)
- warning "Unregistered lookup #{key.intern.inspect}"
- return @@config[key.intern]
- end
- return false
- end
-
- # TODO should I implement this via BotConfigValue or leave it direct?
- # def []=(key, value)
- # end
-
- # pass everything else through to the hash
- def method_missing(method, *args, &block)
- return @@config.send(method, *args, &block)
- end
-
- def handle_list(m, params)
- modules = []
- if params[:module]
- @@items.each_key do |key|
- mod, name = key.split('.')
- next unless mod == params[:module]
- modules.push key unless modules.include?(name)
- end
- if modules.empty?
- m.reply "no such module #{params[:module]}"
- else
- m.reply modules.join(", ")
- end
- else
- @@items.each_key do |key|
- name = key.to_s.split('.').first
- modules.push name unless modules.include?(name)
- end
- m.reply "modules: " + modules.join(", ")
- end
- end