summaryrefslogtreecommitdiff
path: root/lib/rbot
diff options
context:
space:
mode:
authorGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-12 10:35:45 +0000
committerGiuseppe Bilotta <giuseppe.bilotta@gmail.com>2007-04-12 10:35:45 +0000
commitac39a3b330cbf7c4b65ba907783364b63fb109b3 (patch)
treee773d062644ecad55fcd0d48e505a882b0f94a82 /lib/rbot
parentbf03d9f2b695772212abee81d405483c5c374633 (diff)
Module\#define_structure method: define a new Struct only if doesn't exist already or if the attribute list changed
Diffstat (limited to 'lib/rbot')
-rw-r--r--lib/rbot/core/utils/extends.rb24
-rw-r--r--lib/rbot/core/utils/utils.rb1
2 files changed, 24 insertions, 1 deletions
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