Class: Irc::Bot::Registry::Accessor

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

Overview

If you don’t need to store objects, and strictly want a persistant hash of strings, you can override the store/restore methods to suit your needs, for example (in your plugin):

def initialize
  class << @registry
    def store(val)
      val
    end
    def restore(val)
      val
    end
  end
end

Your plugins section of the registry is private, it has its own namespace (derived from the plugin’s class name, so change it and lose your data). Calls to registry.each etc, will only iterate over your namespace.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bot, name) ⇒ Accessor

plugins don’t call this - a Registry::Accessor is created for them and is accessible via @registry.



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/rbot/registry.rb', line 126

def initialize(bot, name)
  @bot = bot
  @name = name.downcase
  @filename = "#{@bot.botclass}/registry/#{@name}"
  dirs = File.dirname(@filename).split("/")
  dirs.length.times { |i|
    dir = dirs[0,i+1].join("/")+"/"
    unless File.exist?(dir)
      debug "creating subregistry directory #{dir}"
      Dir.mkdir(dir)
    end
  }
  @filename << ".db"
  @registry = nil
  @default = nil
  @recovery = nil
  # debug "initializing registry accessor with name #{@name}"
end

Instance Attribute Details

#recoveryObject

Returns the value of attribute recovery.



122
123
124
# File 'lib/rbot/registry.rb', line 122

def recovery
  @recovery
end

Instance Method Details

#[](key) ⇒ Object

lookup a key in the registry



199
200
201
202
203
204
205
# File 'lib/rbot/registry.rb', line 199

def [](key)
  if File.exist?(@filename) && registry.has_key?(key)
    return restore(registry[key])
  else
    return default
  end
end

#[]=(key, value) ⇒ Object

set a key in the registry



208
209
210
# File 'lib/rbot/registry.rb', line 208

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

#clearObject Also known as: truncate

empties the registry (restricted to your namespace)



311
312
313
314
# File 'lib/rbot/registry.rb', line 311

def clear
  return true unless File.exist?(@filename)
  registry.clear
end

#closeObject



156
157
158
159
160
# File 'lib/rbot/registry.rb', line 156

def close
  # debug "closing registry #{registry}"
  return if !@registry
  registry.close
end

#defaultObject



218
219
220
# File 'lib/rbot/registry.rb', line 218

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

#delete(key) ⇒ Object

delete a key from the registry



279
280
281
282
# File 'lib/rbot/registry.rb', line 279

def delete(key)
  return default unless File.exist?(@filename)
  return registry.delete(key)
end

#each(&block) ⇒ Object

just like Hash#each



223
224
225
226
227
228
# File 'lib/rbot/registry.rb', line 223

def each(&block)
  return nil unless File.exist?(@filename)
  registry.each {|key,value|
    block.call(key, restore(value))
  }
end

#each_key(&block) ⇒ Object

just like Hash#each_key



231
232
233
234
235
236
# File 'lib/rbot/registry.rb', line 231

def each_key(&block)
  return nil unless File.exist?(@filename)
  registry.each {|key, value|
    block.call(key)
  }
end

#each_value(&block) ⇒ Object

just like Hash#each_value



239
240
241
242
243
244
# File 'lib/rbot/registry.rb', line 239

def each_value(&block)
  return nil unless File.exist?(@filename)
  registry.each {|key, value|
    block.call(restore(value))
  }
end

#flushObject



149
150
151
152
153
154
# File 'lib/rbot/registry.rb', line 149

def flush
  # debug "fushing registry #{registry}"
  return if !@registry
  registry.flush
  registry.sync
end

#has_both?(key, value) ⇒ Boolean

just like Hash#has_both?

Returns:

  • (Boolean)


256
257
258
259
# File 'lib/rbot/registry.rb', line 256

def has_both?(key, value)
  return false unless File.exist?(@filename)
  return registry.has_both?(key, store(value))
end

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

just like Hash#has_key?

Returns:

  • (Boolean)


247
248
249
250
# File 'lib/rbot/registry.rb', line 247

def has_key?(key)
  return false unless File.exist?(@filename)
  return registry.has_key?(key)
end

#has_value?(value) ⇒ Boolean

just like Hash#has_value?

Returns:

  • (Boolean)


262
263
264
265
# File 'lib/rbot/registry.rb', line 262

def has_value?(value)
  return false unless File.exist?(@filename)
  return registry.has_value?(store(value))
end

#index(value) ⇒ Object

just like Hash#index?



268
269
270
271
272
273
274
275
276
# File 'lib/rbot/registry.rb', line 268

def index(value)
  return nil unless File.exist?(@filename)
  ind = registry.index(store(value))
  if ind
    return ind
  else
    return nil
  end
end

#keysObject

returns a list of your keys



285
286
287
288
# File 'lib/rbot/registry.rb', line 285

def keys
  return [] unless File.exist?(@filename)
  return registry.keys
end

#lengthObject Also known as: size

returns the number of keys in your registry namespace



332
333
334
# File 'lib/rbot/registry.rb', line 332

def length
  self.keys.length
end

#registryObject



145
146
147
# File 'lib/rbot/registry.rb', line 145

def registry
    @registry ||= DBTree.new @bot, "registry/#{@name}"
end

#restore(val) ⇒ Object

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


180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/rbot/registry.rb', line 180

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) ⇒ Object

set the default value for registry lookups, if the key sought is not found, the default will be returned. The default default (har) is nil.



214
215
216
# File 'lib/rbot/registry.rb', line 214

def set_default (default)
  @default = default
end

#store(val) ⇒ Object

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


169
170
171
# File 'lib/rbot/registry.rb', line 169

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

#sub_registry(prefix) ⇒ Object



327
328
329
# File 'lib/rbot/registry.rb', line 327

def sub_registry(prefix)
  return Accessor.new(@bot, @name + "/" + prefix.to_s)
end

#to_aObject

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



291
292
293
294
295
296
297
298
# File 'lib/rbot/registry.rb', line 291

def to_a
  return [] unless File.exist?(@filename)
  ret = Array.new
  registry.each {|key, value|
    ret << [key, restore(value)]
  }
  return ret
end

#to_hashObject

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



301
302
303
304
305
306
307
308
# File 'lib/rbot/registry.rb', line 301

def to_hash
  return {} unless File.exist?(@filename)
  ret = Hash.new
  registry.each {|key, value|
    ret[key] = restore(value)
  }
  return ret
end

#valuesObject

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



318
319
320
321
322
323
324
325
# File 'lib/rbot/registry.rb', line 318

def values
  return [] unless File.exist?(@filename)
  ret = Array.new
  self.each {|k,v|
    ret << restore(v)
  }
  return ret
end