Class: UserDataModule
- Defined in:
- lib/rbot/core/userdata.rb
Overview
User data is stored in registries indexed by BotUser name and Irc::User nick. This core module takes care of handling its usage.
Instance Method Summary collapse
-
#event_botuser(action, opts = {}) ⇒ Object
TODO FIXME not yet: are we going to allow non-string values for data? if so, this can’t work …
- #get_data(user, key = nil) ⇒ Object
- #get_data_hash(user, opts = {}) ⇒ Object
- #handle_get(m, params) ⇒ Object
-
#initialize ⇒ UserDataModule
constructor
A new instance of UserDataModule.
- #set_data(user, key, value = nil, &block) ⇒ Object
- #set_data_hash(user, hh) ⇒ Object
- #with_data(user) {|h| ... } ⇒ Object
Constructor Details
#initialize ⇒ UserDataModule
Returns a new instance of UserDataModule.
50 51 52 53 54 55 |
# File 'lib/rbot/core/userdata.rb', line 50 def initialize super @ircuser = @registry.sub_registry('ircuser') @transient = @registry.sub_registry('transient') @botuser = @registry.sub_registry('botuser') end |
Instance Method Details
#event_botuser(action, opts = {}) ⇒ Object
TODO FIXME not yet: are we going to allow non-string values for data? if so, this can’t work …
def handle_set(m, params)
user = m.server.get_user(params[:nick]) || m.source
key = params[:key].intern
data = params[:data].to_s
end
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rbot/core/userdata.rb', line 168 def event_botuser(action, opts={}) case action when :copy, :rename source = opts[:source] return unless @botuser.key?(source) dest = opts[:dest] @botuser[dest] = @botuser[source].dup @botuser.delete(source) if action == :rename when :pre_perm @permification ||= {} k = [opts[:irc_user], opts[:bot_user]] @permification[k] = get_data_hash(opts[:irc_user], :plain => true) when :post_perm @permification ||= {} k = [opts[:irc_user], opts[:bot_user]] if @permification.has_key?(k) @botuser[opts[:bot_user]] = @permification[k] @permification.delete(k) end end end |
#get_data(user, key = nil) ⇒ Object
84 85 86 87 88 89 |
# File 'lib/rbot/core/userdata.rb', line 84 def get_data(user, key=nil) h = get_data_hash(user) debug h return h if key.nil? return h[key] end |
#get_data_hash(user, opts = {}) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/rbot/core/userdata.rb', line 57 def get_data_hash(user, opts={}) plain = opts[:plain] iu = user.to_irc_user bu = iu.botuser ih = @ircuser[iu.nick] || {} if bu.default? return ih elsif bu.transient? bh = @transient[bu.netmasks.first.fullform] || {} else bh = @botuser[bu.username] || {} end ih.merge!(bh) unless plain class << ih alias :single_retrieve :[] alias :single_assign :[]= include DottedIndex end end return ih end |
#handle_get(m, params) ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/rbot/core/userdata.rb', line 141 def handle_get(m, params) user = m.server.get_user(params[:nick]) || m.source key = params[:key].intern data = get_data(user, key) if data m.reply(_("%{key} data for %{user}: %{data}") % { :key => key, :user => user.nick, :data => data }) else m.reply(_("sorry, no %{key} data for %{user}") % { :key => key, :user => user.nick, }) end end |
#set_data(user, key, value = nil, &block) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/rbot/core/userdata.rb', line 110 def set_data(user, key, value=nil, &block) h = get_data_hash(user) debug h ret = value if not block_given? h[key] = value else if value and not h.has_key?(key) h[key] = value end ret = yield h[key] end debug ret set_data_hash(user, h) return ret end |
#set_data_hash(user, hh) ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rbot/core/userdata.rb', line 91 def set_data_hash(user, hh) iu = user.to_irc_user bu = iu.botuser # we .dup the hash to remove singleton methods # and make it dump-able h = hh.dup @ircuser[iu.nick] = h return h if bu.default? if bu.transient? @transient[bu.netmasks.first.fullform] = h else @botuser[bu.username] = h end return h end |