Class: Irc::DBHash

Inherits:
Object show all
Defined in:
lib/rbot/dbhash.rb

Overview

DBHash is for tying a hash to disk (using bdb). Call it with an identifier, for example “mydata”. It’ll look for mydata.db, if it exists, it will load and reference that db. Otherwise it’ll create and empty db called mydata.db

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, key, absfilename = false) ⇒ DBHash

absfilename

use key as an actual filename, don’t prepend the bot’s config path and don’t append “.db”



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/rbot/dbhash.rb', line 52

def initialize(bot, key, absfilename=false)
  @bot = bot
  @key = key
  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)
  else
    # create empty db
    @db = DBHash.create_db(@bot.botclass + "/#{key}.db")
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



70
71
72
# File 'lib/rbot/dbhash.rb', line 70

def method_missing(method, *args, &block)
  return @db.send(method, *args, &block)
end

Class Method Details

.create_db(name) ⇒ Object



74
75
76
77
78
# File 'lib/rbot/dbhash.rb', line 74

def DBHash.create_db(name)
  debug "DBHash: creating empty db #{name}"
  return BDB::Hash.open(name, nil, 
  BDB::CREATE | BDB::EXCL, 0600)
end

.open_db(name) ⇒ Object



80
81
82
83
# File 'lib/rbot/dbhash.rb', line 80

def DBHash.open_db(name)
  debug "DBHash: opening existing db #{name}"
  return BDB::Hash.open(name, nil, "r+", 0600)
end