class Irc::Bot::Registry::AbstractAccessor
Abstract database accessor (a hash-like interface).
Attributes
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
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
# 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
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
set a key in the registry
# File lib/rbot/registry.rb, line 223 def []=(key,value) registry[key.to_s] = store(value) end
empties the registry (restricted to your namespace)
# File lib/rbot/registry.rb, line 312 def clear return unless dbexists? registry.clear end
Closes the database.
# File lib/rbot/registry.rb, line 207 def close return unless @registry @registry.close @registry = nil end
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
Will return true if the database file exists.
# File lib/rbot/registry.rb, line 140 def dbexists? File.exist? @filename end
# File lib/rbot/registry.rb, line 185 def default @default && (@default.dup rescue @default) end
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
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
like Hash#each_key
# File lib/rbot/registry.rb, line 238 def each_key(&block) self.each do |key| block.call(key) end end
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
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
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
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
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
returns a list of your keys
# File lib/rbot/registry.rb, line 286 def keys return [] unless dbexists? return registry.keys end
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
Should optimize/vacuum the database. (if supported)
# File lib/rbot/registry.rb, line 203 def optimize end
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
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
Returned instead of nil if key wasn’t found.
# File lib/rbot/registry.rb, line 181 def set_default (default) @default = default end
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
# 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
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
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
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