Class: Irc::Bot::Registry::AbstractAccessor

Inherits:
Object
  • Object
show all
Defined in:
/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb

Overview

Abstract database accessor (a hash-like interface).

Direct Known Subclasses

DBMAccessor, DaybreakAccessor, SqliteAccessor, TokyoCabinetAccessor

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (AbstractAccessor) initialize(filename)

Returns a new instance of AbstractAccessor



108
109
110
111
112
113
114
115
116
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 108

def initialize(filename)
  debug 'init registry accessor for: ' + filename
  @filename = filename
  @name = File.basename filename
  @registry = nil
  @default = nil
  @recovery = nil
  @sub_registries = {}
end

Instance Attribute Details

- (Object) filename (readonly)

Returns the value of attribute filename



101
102
103
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 101

def filename
  @filename
end

- (Object) recovery

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?)



106
107
108
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 106

def recovery
  @recovery
end

Class Method Details

+ (Object) get_impl

Returns all classes from the namespace that implement this interface



332
333
334
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 332

def self.get_impl
  ObjectSpace.each_object(Class).select { |klass| klass < self }
end

Instance Method Details

- (Object) [](key)

lookup a key in the registry



210
211
212
213
214
215
216
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 210

def [](key)
  if dbexists? and registry.has_key?(key.to_s)
    return restore(registry[key.to_s])
  else
    return default
  end
end

- (Object) []=(key, value)

set a key in the registry



219
220
221
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 219

def []=(key,value)
  registry[key.to_s] = store(value)
end

- (Object) clear Also known as: truncate

empties the registry (restricted to your namespace)



308
309
310
311
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 308

def clear
  return unless dbexists?
  registry.clear
end

- (Object) close

Closes the database.



203
204
205
206
207
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 203

def close
  return unless @registry
  @registry.close
  @registry = nil
end

- (Object) create_folders

creates the registry / subregistry folders



124
125
126
127
128
129
130
131
132
133
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 124

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

- (Boolean) dbexists?

Will return true if the database file exists.

Returns:

  • (Boolean)


136
137
138
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 136

def dbexists?
  File.exists? @filename
end

- (Object) default



181
182
183
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 181

def default
  @default && (@default.dup rescue @default)
end

- (Object) delete(key)

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



273
274
275
276
277
278
279
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 273

def delete(key)
  return default unless dbexists?
  value = registry.delete(key.to_s)
  if value
    restore(value)
  end
end

- (Object) each(&block) Also known as: each_pair

like Hash#each



224
225
226
227
228
229
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 224

def each(&block)
  return nil unless dbexists?
  registry.each do |key, value|
    block.call(key, restore(value))
  end
end

- (Object) each_key(&block)

like Hash#each_key



234
235
236
237
238
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 234

def each_key(&block)
  self.each do |key|
    block.call(key)
  end
end

- (Object) each_value(&block)

like Hash#each_value



241
242
243
244
245
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 241

def each_value(&block)
  self.each do |key, value|
    block.call(value)
  end
end

- (Object) flush

Forces flush/sync the database on disk.



191
192
193
194
195
196
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 191

def flush
  return unless @registry
  # if not supported by the database, close/reopen:
  close
  registry
end

- (Boolean) has_key?(key) Also known as: include?, member?, key?

just like Hash#has_key?

Returns:

  • (Boolean)


248
249
250
251
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 248

def has_key?(key)
  return nil unless dbexists?
  return registry.has_key?(key.to_s)
end

- (Boolean) has_value?(value)

just like Hash#has_value?

Returns:

  • (Boolean)


258
259
260
261
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 258

def has_value?(value)
  return nil unless dbexists?
  return registry.has_value?(store(value))
end

- (Object) index(value)

just like Hash#index?



264
265
266
267
268
269
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 264

def index(value)
  self.each do |k,v|
    return k if v == value
  end
  return nil
end

- (Object) keys

returns a list of your keys



282
283
284
285
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 282

def keys
  return [] unless dbexists?
  return registry.keys
end

- (Object) length Also known as: size

returns the number of keys in your registry namespace



325
326
327
328
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 325

def length
  return 0 unless dbexists?
  registry.length
end

- (Object) optimize

Should optimize/vacuum the database. (if supported)



199
200
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 199

def optimize
end

- (Object) registry

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



186
187
188
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 186

def registry
  create_folders unless dbexists?
end

- (Object) restore(val)

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


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 158

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

- (Object) set_default(default)

Returned instead of nil if key wasnt found.



177
178
179
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 177

def set_default (default)
  @default = default
end

- (Object) store(val)

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


147
148
149
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 147

def store(val)
  Marshal.dump(val)
end

- (Object) sub_registry(prefix)



118
119
120
121
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 118

def sub_registry(prefix)
  path = File.join(@filename.gsub(/\.[^\/\.]+$/,''), prefix.to_s)
  @sub_registries[path] ||= self.class.new(path)
end

- (Object) to_a

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



288
289
290
291
292
293
294
295
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 288

def to_a
  return [] unless dbexists?
  ret = Array.new
  self.each {|key, value|
    ret << [key, value]
  }
  return ret
end

- (Object) to_hash

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



298
299
300
301
302
303
304
305
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 298

def to_hash
  return {} unless dbexists?
  ret = Hash.new
  self.each {|key, value|
    ret[key] = value
  }
  return ret
end

- (Object) values

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



315
316
317
318
319
320
321
322
# File '/home/apoc/projects/ruby/rbot/lib/rbot/registry.rb', line 315

def values
  return [] unless dbexists?
  ret = Array.new
  self.each {|k,v|
    ret << v
  }
  return ret
end