class BeerBot::Config

Config can be loaded with the json from a config file before initialisation of the system.

Config might be a bit of a misnomer, think of this as “an injectable thing that contains a lot of useful information”.

It should be available to things like bot modules eg

BeerBot::Config['datadir']

Attributes

bot[RW]

Should reference the Bot instance.

This will allow modules to inspect the bot module list and to override normal cmd behaviour (using Bot#set_cmd ).

scheduler[RW]

Should point to ‘the’ instance of scheduler used by this bot.

Public Class Methods

new(**kargs) click to toggle source
# File lib/beerbot/config.rb, line 42
def initialize **kargs
  # Defaults
  self['cmd_prefix'] = ','
  self['nick'] = 'beerbot'
  kargs.keys.each {|key|
    self[key.to_s] = kargs[key]
  }
end

Public Instance Methods

load(config) click to toggle source
# File lib/beerbot/config.rb, line 51
def load config
  self.reject!{true}
  self.merge!(config)
end
module_data(name,&block) click to toggle source

Return path for module data dir – a place where the module can stash data.

module_data('foo') => <datadir>/modules/foo
module_data('foo') {
  ... set pwd to this dir...
}
# File lib/beerbot/config.rb, line 79
def module_data name,&block
  self.validate!
  datadir = self['datadir']
  path = File.join(datadir,'modules',name)
  if not File.exists?(path) then
    FileUtils.mkdir_p(path)
  end
  if block_given? then
    Dir.chdir(path) {
      block.call(path)
    }
  else
    path
  end
end
out() click to toggle source

A queue that allows users of config to enqueue outgoing bot msg’s actively.

(passive = “as response to a command via Bot#cmd”).

# File lib/beerbot/config.rb, line 38
def out
  @queue ||= Queue.new
end
validate!() click to toggle source
# File lib/beerbot/config.rb, line 56
def validate!
  if not self['datadir'] then
    raise "'datadir' not set in config."
  end
  if not self['moduledir'] then
    raise "'moduledir' not set in config."
  end
  unless File.exists?(self['datadir']) then
    raise "datadir:'#{self['datadir']}' doesn't exist."
  end
  unless File.exists?(self['moduledir']) then
    raise "config['moduledir']=#{@module_path} doesn't exist, make one (bot modules will go here)!"
  end
end