Class: Irc::DBHash

Inherits:
Object show all
Defined in:
lib/rbot/registry/tc.rb,
lib/rbot/registry/bdb.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”



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/rbot/registry/tc.rb', line 40

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 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 relfilename
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



60
61
62
# File 'lib/rbot/registry/tc.rb', line 60

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

Class Method Details

.create_db(name) ⇒ Object



64
65
66
67
68
# File 'lib/rbot/registry/tc.rb', line 64

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



70
71
72
73
# File 'lib/rbot/registry/tc.rb', line 70

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