class Irc::Bot::Registry::AbstractAccessor

Abstract database accessor (a hash-like interface).

Attributes

filename[R]
recovery[RW]

lets the user define a recovery procedure in case the Marshal deserialization fails, it might be manually recover data. NOTE: weird legacy stuff, used by markov plugin (WTH?)

Public Class Methods

get_impl() click to toggle source

Returns all classes from the namespace that implement this interface

# File lib/rbot/registry.rb, line 336
def self.get_impl
  ObjectSpace.each_object(Class).select { |klass| klass.ancestors[1] == self }
end
new(filename) click to toggle source
# File lib/rbot/registry.rb, line 112
def initialize(filename)
  debug "init registry accessor of #{self.class} for: #{filename}"
  @filename = filename
  @name = File.basename filename
  @registry = nil
  @default = nil
  @recovery = nil
  @sub_registries = {}
end

Public Instance Methods

[](key) click to toggle source

lookup a key in the registry

# File lib/rbot/registry.rb, line 214
def [](key)
  if dbexists? and registry.has_key?(key.to_s)
    return restore(registry[key.to_s])
  else
    return default
  end
end
[]=(key,value) click to toggle source

set a key in the registry

# File lib/rbot/registry.rb, line 223
def []=(key,value)
  registry[key.to_s] = store(value)
end
clear() click to toggle source

empties the registry (restricted to your namespace)

# File lib/rbot/registry.rb, line 312
def clear
  return unless dbexists?
  registry.clear
end
Also aliased as: truncate
close() click to toggle source

Closes the database.

# File lib/rbot/registry.rb, line 207
def close
  return unless @registry
  @registry.close
  @registry = nil
end
create_folders() click to toggle source

creates the registry / subregistry folders

# File lib/rbot/registry.rb, line 128
def create_folders
  debug 'create folders for: ' + @filename
  dirs = File.dirname(@filename).split("/")
  dirs.length.times { |i|
    dir = dirs[0,i+1].join("/")+"/"
    unless File.exist?(dir)
      Dir.mkdir(dir)
    end
  }
end
dbexists?() click to toggle source

Will return true if the database file exists.

# File lib/rbot/registry.rb, line 140
def dbexists?
  File.exist? @filename
end
default() click to toggle source
# File lib/rbot/registry.rb, line 185
def default
  @default && (@default.dup rescue @default)
end
delete(key) click to toggle source

delete a key from the registry returns the value in success, nil otherwise

# File lib/rbot/registry.rb, line 277
def delete(key)
  return default unless dbexists?
  value = registry.delete(key.to_s)
  if value
    restore(value)
  end
end
each(&block) click to toggle source

like Hash#each

# File lib/rbot/registry.rb, line 228
def each(&block)
  return nil unless dbexists?
  registry.each do |key, value|
    block.call(key, restore(value))
  end
end
Also aliased as: each_pair
each_key(&block) click to toggle source

like Hash#each_key

# File lib/rbot/registry.rb, line 238
def each_key(&block)
  self.each do |key|
    block.call(key)
  end
end
each_pair(&block)
Alias for: each
each_value(&block) click to toggle source

like Hash#each_value

# File lib/rbot/registry.rb, line 245
def each_value(&block)
  self.each do |key, value|
    block.call(value)
  end
end
flush() click to toggle source

Forces flush/sync the database on disk.

# File lib/rbot/registry.rb, line 195
def flush
  return unless @registry
  # if not supported by the database, close/reopen:
  close
  registry
end
has_key?(key) click to toggle source

just like Hash#has_key?

# File lib/rbot/registry.rb, line 252
def has_key?(key)
  return nil unless dbexists?
  return registry.has_key?(key.to_s)
end
Also aliased as: include?, member?, key?
has_value?(value) click to toggle source

just like Hash#has_value?

# File lib/rbot/registry.rb, line 262
def has_value?(value)
  return nil unless dbexists?
  return registry.has_value?(store(value))
end
include?(key)
Alias for: has_key?
index(value) click to toggle source

just like Hash#index?

# File lib/rbot/registry.rb, line 268
def index(value)
  self.each do |k,v|
    return k if v == value
  end
  return nil
end
key?(key)
Alias for: has_key?
keys() click to toggle source

returns a list of your keys

# File lib/rbot/registry.rb, line 286
def keys
  return [] unless dbexists?
  return registry.keys
end
length() click to toggle source

returns the number of keys in your registry namespace

# File lib/rbot/registry.rb, line 329
def length
  return 0 unless dbexists?
  registry.length
end
Also aliased as: size
member?(key)
Alias for: has_key?
optimize() click to toggle source

Should optimize/vacuum the database. (if supported)

# File lib/rbot/registry.rb, line 203
def optimize
end
registry() click to toggle source

Opens the database (if not already open) for read/write access.

# File lib/rbot/registry.rb, line 190
def registry
  create_folders unless dbexists?
end
restore(val) click to toggle source

restores object from string form, restore(store(val)) must return val. If you override store, you should override restore to reverse the action. For example, if you always just handle strings use:

def restore(val)
  val
end
# File lib/rbot/registry.rb, line 162
def restore(val)
  begin
    Marshal.restore(val)
  rescue Exception => e
    error _("failed to restore marshal data for #{val.inspect}, attempting recovery or fallback to default")
    debug e
    if defined? @recovery and @recovery
      begin
        return @recovery.call(val)
      rescue Exception => ee
        error _("marshal recovery failed, trying default")
        debug ee
      end
    end
    return default
  end
end
set_default(default) click to toggle source

Returned instead of nil if key wasn’t found.

# File lib/rbot/registry.rb, line 181
def set_default (default)
  @default = default
end
size()
Alias for: length
store(val) click to toggle source

convert value to string form for storing in the registry defaults to Marshal.dump(val) but you can override this in your module’s registry object to use any method you like. For example, if you always just handle strings use:

def store(val)
  val
end
# File lib/rbot/registry.rb, line 151
def store(val)
  Marshal.dump(val)
end
sub_registry(prefix) click to toggle source
# File lib/rbot/registry.rb, line 122
def sub_registry(prefix)
  path = File.join(@filename.gsub(/\.[^\/\.]+$/,''), prefix.to_s)
  @sub_registries[path] ||= self.class.new(path)
end
to_a() click to toggle source

Return an array of all associations [key, value] in your namespace

# File lib/rbot/registry.rb, line 292
def to_a
  return [] unless dbexists?
  ret = Array.new
  self.each {|key, value|
    ret << [key, value]
  }
  return ret
end
to_hash() click to toggle source

Return an hash of all associations {key => value} in your namespace

# File lib/rbot/registry.rb, line 302
def to_hash
  return {} unless dbexists?
  ret = Hash.new
  self.each {|key, value|
    ret[key] = value
  }
  return ret
end
truncate()
Alias for: clear
values() click to toggle source

returns an array of the values in your namespace of the registry

# File lib/rbot/registry.rb, line 319
def values
  return [] unless dbexists?
  ret = Array.new
  self.each {|k,v|
    ret << v
  }
  return ret
end