]> git.netwichtig.de Git - user/henk/code/ruby/rbot.git/commitdiff
Module\#define_structure method: define a new Struct only if doesn't exist already...
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 12 Apr 2007 10:35:45 +0000 (10:35 +0000)
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>
Thu, 12 Apr 2007 10:35:45 +0000 (10:35 +0000)
ChangeLog
data/rbot/plugins/bans.rb
data/rbot/plugins/games/quiz.rb
data/rbot/plugins/games/roulette.rb
data/rbot/plugins/quotes.rb
data/rbot/plugins/script.rb
data/rbot/plugins/seen.rb
data/rbot/plugins/url.rb
lib/rbot/core/utils/extends.rb
lib/rbot/core/utils/utils.rb

index aaa5e2e2710b0bef78c7cc38446015e296ba1d11..069742397c5366c084317b51b775aaf66f5fd7b6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2007-04-12  Giuseppe Bilotta <giuseppe.bilotta@gmail.com>
+
+       * 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 <dmitry.kim@gmail.com>
 
        * 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 <giuseppe.bilotta@gmail.com>
index 17281d4cc0b83162924303ee61c3a9c516ee36d6..73121b7ef505abd4883d014f21f7b8f5235d78ba 100644 (file)
 #   * fixed regexp usage in requirements for plugin map\r
 #   * add proper auth management\r
 \r
-OnJoinAction = Struct.new("OnJoinAction", :host, :action, :channel, :reason)\r
-BadWordAction = Struct.new("BadWordAction", :regexp, :action, :channel, :timer, :reason)\r
-WhitelistEntry = Struct.new("WhitelistEntry", :host, :channel)\r
-\r
+define_structure :OnJoinAction, :host, :action, :channel, :reason\r
+define_structure :BadWordAction, :regexp, :action, :channel, :timer, :reason\r
+define_structure :WhitelistEntry, :host, :channel\r
 \r
 class BansPlugin < Plugin\r
 \r
index d73de90e0fced58b4dfef798a27c898b7605fc7c..5f7392dad61f0e3391b533d01ff51bf85de7688e 100644 (file)
 # 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
index 5c9a86c6c32834e427a25a7a6c9d171a153c3c78..0d2dc4c27f281cf9e814ef735a7ce8094d3e0acd 100644 (file)
@@ -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',
index 6851b65f2f2c0ff83b463482d5450a300ad2f728..b5017a58852fe121472a7011db787511495e613d 100644 (file)
@@ -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
index 29afa20a62e183fea66450382ef1a91722b1bd21..38bb3134828002cf0e356f9532e70e7f734f1e35 100644 (file)
@@ -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
 
index a697d19b06722743884fe0411a84d177dc10a86e..704b83241a9d5a9ba659f8481643efe6e2eda8e0 100644 (file)
@@ -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="")
index 62fc55888980bb15b5ab9e261b735b6a1f57b087..092ccbf99ca7ebbe0fd425dd7515014a418d7b2e 100644 (file)
@@ -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
index bcea735b25a77e809c7a1a12fae22ab15421341f..1aa6d457a00e4f48fa7ad464128921cf6c0bfe32 100644 (file)
 # 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
 #
index 71b4c8d46de07d5016f67f1dfc78f631535ec53f..23d50c314de645b95d19096833b38f3bf2fba542 100644 (file)
@@ -546,7 +546,6 @@ module ::Irc
       return retval
     end
 
-
   end
 end