From ac39a3b330cbf7c4b65ba907783364b63fb109b3 Mon Sep 17 00:00:00 2001 From: Giuseppe Bilotta Date: Thu, 12 Apr 2007 10:35:45 +0000 Subject: [PATCH] Module\#define_structure method: define a new Struct only if doesn't exist already or if the attribute list changed --- ChangeLog | 11 ++++++++++- data/rbot/plugins/bans.rb | 7 +++---- data/rbot/plugins/games/quiz.rb | 4 ++-- data/rbot/plugins/games/roulette.rb | 2 +- data/rbot/plugins/quotes.rb | 2 +- data/rbot/plugins/script.rb | 4 +--- data/rbot/plugins/seen.rb | 2 +- data/rbot/plugins/url.rb | 2 +- lib/rbot/core/utils/extends.rb | 24 ++++++++++++++++++++++++ lib/rbot/core/utils/utils.rb | 1 - 10 files changed, 44 insertions(+), 15 deletions(-) diff --git a/ChangeLog b/ChangeLog index aaa5e2e2..06974239 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,12 @@ +2007-04-12 Giuseppe Bilotta + + * Basic class extensions: Module#define_structure() method. Syntax: + define_structure :SomeName, :attr_a, :attr_b + is equivalent to + SomeName = Struct.new("SomeName", :attr_a, :attr_b) + except that the new Struct is not created if it already exists and the + attributes list is the same. + 2007-03-31 Dmitry Kim * HttpUtil: major rework. get_response() method now respects @@ -12,7 +21,7 @@ been removed (mostly because it won't play well with future HTTP encodings support), please use get_partial() or get_request() instead. * Utils: http_get() method has been removed (long obsoleted by - HttpUtil) + HttpUtil) * different plugins: modified to accomodate for HttpUtil changes. 2007-03-24 Giuseppe Bilotta diff --git a/data/rbot/plugins/bans.rb b/data/rbot/plugins/bans.rb index 17281d4c..73121b7e 100644 --- a/data/rbot/plugins/bans.rb +++ b/data/rbot/plugins/bans.rb @@ -32,10 +32,9 @@ # * fixed regexp usage in requirements for plugin map # * add proper auth management -OnJoinAction = Struct.new("OnJoinAction", :host, :action, :channel, :reason) -BadWordAction = Struct.new("BadWordAction", :regexp, :action, :channel, :timer, :reason) -WhitelistEntry = Struct.new("WhitelistEntry", :host, :channel) - +define_structure :OnJoinAction, :host, :action, :channel, :reason +define_structure :BadWordAction, :regexp, :action, :channel, :timer, :reason +define_structure :WhitelistEntry, :host, :channel class BansPlugin < Plugin diff --git a/data/rbot/plugins/games/quiz.rb b/data/rbot/plugins/games/quiz.rb index d73de90e..5f7392da 100644 --- a/data/rbot/plugins/games/quiz.rb +++ b/data/rbot/plugins/games/quiz.rb @@ -27,10 +27,10 @@ # TODO:: when Ruby 2.0 gets out, fix the FIXME 2.0 UTF-8 workarounds # Class for storing question/answer pairs -QuizBundle = Struct.new( "QuizBundle", :question, :answer ) +define_structure :QuizBundle, :question, :answer # Class for storing player stats -PlayerStats = Struct.new( "PlayerStats", :score, :jokers, :jokers_time ) +define_structure :PlayerStats, :score, :jokers, :jokers_time # Why do we still need jokers_time? //Firetech # Maximum number of jokers a player can gain diff --git a/data/rbot/plugins/games/roulette.rb b/data/rbot/plugins/games/roulette.rb index 5c9a86c6..0d2dc4c2 100644 --- a/data/rbot/plugins/games/roulette.rb +++ b/data/rbot/plugins/games/roulette.rb @@ -1,4 +1,4 @@ -RouletteHistory = Struct.new("RouletteHistory", :games, :shots, :deaths, :misses, :wins) +define_structure :RouletteHistory, :games, :shots, :deaths, :misses, :wins class RoulettePlugin < Plugin BotConfig.register BotConfigBooleanValue.new('roulette.autospin', diff --git a/data/rbot/plugins/quotes.rb b/data/rbot/plugins/quotes.rb index 6851b65f..b5017a58 100644 --- a/data/rbot/plugins/quotes.rb +++ b/data/rbot/plugins/quotes.rb @@ -1,6 +1,6 @@ # GB: Ok, we *really* need to switch to db for this plugin too -Quote = Struct.new("Quote", :num, :date, :source, :quote) +define_structure :Quote, :num, :date, :source, :quote class QuotePlugin < Plugin def initialize diff --git a/data/rbot/plugins/script.rb b/data/rbot/plugins/script.rb index 29afa20a..38bb3134 100644 --- a/data/rbot/plugins/script.rb +++ b/data/rbot/plugins/script.rb @@ -13,9 +13,7 @@ # plugin. You can create them directly in an IRC channel, and invoke them just # like normal rbot plugins. - -Command = Struct.new( "Command", :code, :nick, :created, :channel ) - +define_structure :Command, :code, :nick, :created, :channel class ScriptPlugin < Plugin diff --git a/data/rbot/plugins/seen.rb b/data/rbot/plugins/seen.rb index a697d19b..704b8324 100644 --- a/data/rbot/plugins/seen.rb +++ b/data/rbot/plugins/seen.rb @@ -1,4 +1,4 @@ -Saw = Struct.new("Saw", :nick, :time, :type, :where, :message) +define_structure :Saw, :nick, :time, :type, :where, :message class SeenPlugin < Plugin def help(plugin, topic="") diff --git a/data/rbot/plugins/url.rb b/data/rbot/plugins/url.rb index 62fc5588..092ccbf9 100644 --- a/data/rbot/plugins/url.rb +++ b/data/rbot/plugins/url.rb @@ -1,4 +1,4 @@ -Url = Struct.new("Url", :channel, :nick, :time, :url, :info) +define_structure :Url, :channel, :nick, :time, :url, :info class ::UrlLinkError < RuntimeError end diff --git a/lib/rbot/core/utils/extends.rb b/lib/rbot/core/utils/extends.rb index bcea735b..1aa6d457 100644 --- a/lib/rbot/core/utils/extends.rb +++ b/lib/rbot/core/utils/extends.rb @@ -13,6 +13,30 @@ # Please note that global symbols have to be prefixed by :: because this plugin # will be read into an anonymous module +# Extensions to the Module class +# +class ::Module + + # Many plugins define Struct objects to hold their data. On rescans, lots of + # warnings are echoed because of the redefinitions. Using this method solves + # the problem, by checking if the Struct already exists, and if it has the + # same attributes + # + def define_structure(name, *members) + sym = name.to_sym + if Struct.const_defined?(sym) + kl = Struct.const_get(sym) + if kl.new.members.map { |member| member.intern } == members.map + debug "Struct #{sym} previously defined, skipping" + const_set(sym, kl) + return + end + end + debug "Defining struct #{sym} with members #{members.inspect}" + const_set(sym, Struct.new(name.to_s, *members)) + end +end + # Extensions to the Array class # diff --git a/lib/rbot/core/utils/utils.rb b/lib/rbot/core/utils/utils.rb index 71b4c8d4..23d50c31 100644 --- a/lib/rbot/core/utils/utils.rb +++ b/lib/rbot/core/utils/utils.rb @@ -546,7 +546,6 @@ module ::Irc return retval end - end end -- 2.39.2