summaryrefslogtreecommitdiff
path: root/lib/rbot/dbhash.rb
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-15 01:30:51 +0100
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2009-02-15 02:06:27 +0100
commit91a9024e21ec8b429605a036b5c9193442a580e3 (patch)
treef1524dd5595a80e32d0702c0d8939b1001c695fb /lib/rbot/dbhash.rb
parent38be7f6511447d98780a069bccefecd933238e30 (diff)
+ @bot.path and datafile methods
We provide two methods that make it more simple and elegant for botmodules to define paths relative to the bot's own directory (botclass) and to the BotModule's (assumed) non-registry directory. The first method is Irc::Bot#path(), which joins its arguments with the botclass. This method can be used to access datafiles in the bot directory with a much cleaner syntax; and since it uses File.join, the resulting paths are also properly formatted on each platform, which doesn't hurt. Each BotModule now also carries a dirname() method that should return the directory under botclass that holds the BotModule's datafiles. dirname() defaults to the BotModule's name(), but it can be overridden, e.g. for backwards compatibility (see the patch for the quotes plugin), or for BotModules that share their datafiles. Datafiles can be accessed using the BotModule#datafile() method that joins the botclass, the dirname() and whatever other argument is passed.
Diffstat (limited to 'lib/rbot/dbhash.rb')
-rw-r--r--lib/rbot/dbhash.rb25
1 files changed, 15 insertions, 10 deletions
diff --git a/lib/rbot/dbhash.rb b/lib/rbot/dbhash.rb
index dd61c720..704dafae 100644
--- a/lib/rbot/dbhash.rb
+++ b/lib/rbot/dbhash.rb
@@ -52,18 +52,20 @@ module Irc
def initialize(bot, key, absfilename=false)
@bot = bot
@key = key
+ relfilename = @bot.path key
+ relfilename << '.db'
if absfilename && File.exist?(key)
# db already exists, use it
@db = DBHash.open_db(key)
- elsif File.exist?(@bot.botclass + "/#{key}.db")
- # db already exists, use it
- @db = DBHash.open_db(@bot.botclass + "/#{key}.db")
elsif absfilename
# create empty db
@db = DBHash.create_db(key)
+ elsif File.exist? relfilename
+ # db already exists, use it
+ @db = DBHash.open_db relfilename
else
# create empty db
- @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
+ @db = DBHash.create_db relfilename
end
end
@@ -98,27 +100,30 @@ module Irc
@key = key
if @@env.nil?
begin
- @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
+ @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER, "set_lg_max" => @@lg_max)
debug "DBTree: environment opened with max log size #{@@env.conf['lg_max']}"
rescue => e
debug "DBTree: failed to open environment: #{e.pretty_inspect}. Retrying ..."
- @@env = BDB::Env.open("#{@bot.botclass}", BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
+ @@env = BDB::Env.open(@bot.botclass, BDB::INIT_TRANSACTION | BDB::CREATE | BDB::RECOVER)
end
- #@@env = BDB::Env.open("#{@bot.botclass}", BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
+ #@@env = BDB::Env.open(@bot.botclass, BDB::CREATE | BDB::INIT_MPOOL | BDB::RECOVER)
end
+ relfilename = @bot.path key
+ relfilename << '.db'
+
if absfilename && File.exist?(key)
# db already exists, use it
@db = DBTree.open_db(key)
elsif absfilename
# create empty db
@db = DBTree.create_db(key)
- elsif File.exist?(@bot.botclass + "/#{key}.db")
+ elsif File.exist? relfilename
# db already exists, use it
- @db = DBTree.open_db(@bot.botclass + "/#{key}.db")
+ @db = DBTree.open_db relfilename
else
# create empty db
- @db = DBTree.create_db(@bot.botclass + "/#{key}.db")
+ @db = DBTree.create_db relfilename
end
end