Class: BeerBot::Config

Inherits:
Hash
  • Object
show all
Defined in:
lib/beerbot/config.rb

Overview

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']

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**kargs) ⇒ Config

Returns a new instance of Config.



42
43
44
45
46
47
48
49
# 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

Instance Attribute Details

#botObject

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 ).



31
32
33
# File 'lib/beerbot/config.rb', line 31

def bot
  @bot
end

#schedulerObject

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



24
25
26
# File 'lib/beerbot/config.rb', line 24

def scheduler
  @scheduler
end

Instance Method Details

#load(config) ⇒ Object



51
52
53
54
# File 'lib/beerbot/config.rb', line 51

def load config
  self.reject!{true}
  self.merge!(config)
end

#module_data(name, &block) ⇒ Object

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...
}


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# 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

#outObject

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

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



38
39
40
# File 'lib/beerbot/config.rb', line 38

def out
  @queue ||= Queue.new
end

#validate!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
# 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