diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-22 05:06:46 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2014-04-22 05:06:46 -0700 |
commit | 5fecb1fcc8f24bed19521a5034db5756d4b11612 (patch) | |
tree | 71466fd01d2ca45f05373c9b5281617d74c6730f /core/aliaser.rb | |
parent | 7f94ea0c30bee85c0eecbd4ccb4c8e7efb3889bb (diff) | |
download | dinobot-5fecb1fcc8f24bed19521a5034db5756d4b11612.tar.gz dinobot-5fecb1fcc8f24bed19521a5034db5756d4b11612.tar.xz |
Clean up code and add aliaser class.
Diffstat (limited to 'core/aliaser.rb')
-rw-r--r-- | core/aliaser.rb | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/core/aliaser.rb b/core/aliaser.rb new file mode 100644 index 0000000..b867e10 --- /dev/null +++ b/core/aliaser.rb @@ -0,0 +1,53 @@ +require_relative 'store' + +module Dinobot + module Core + class Aliaser + attr_accessor :data + + @@instance = nil + @@mutex = Mutex.new + + def initialize + @store = Dinobot::Core::Store.new('aliaser.db') + @data = @store.data + end + + def add(from, to, channel=:global) + @data[channel] = Hash.new unless @data.key?(channel) + @data[channel][from] = to + + save + end + + def remove(from, channel=:global) + return unless data.key?(channel) + + @data[channel].delete(from) + @data.delete(channel) if @data[channel].empty? + + save + end + + def aliases + @data.dup + end + + def save + @store.save + end + + class << self + def instance + return @@instance if @@instance + + @@mutex.synchronize do + @@instance ||= new + end + end + end + + private_class_method :allocate, :new + end + end +end |