]> git.cworth.org Git - sup/commitdiff
rewrite Singleton to not require i_am_the_instance
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Wed, 12 Aug 2009 17:14:34 +0000 (13:14 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 17 Aug 2009 18:46:49 +0000 (14:46 -0400)
The flip side is that you have to use .init instead of .new.

24 files changed:
bin/sup
bin/sup-add
bin/sup-config
bin/sup-dump
bin/sup-recover-sources
bin/sup-sync
bin/sup-sync-back
bin/sup-tweak-labels
lib/sup.rb
lib/sup/account.rb
lib/sup/buffer.rb
lib/sup/contact.rb
lib/sup/crypto.rb
lib/sup/draft.rb
lib/sup/hook.rb
lib/sup/index.rb
lib/sup/label.rb
lib/sup/poll.rb
lib/sup/sent.rb
lib/sup/source.rb
lib/sup/suicide.rb
lib/sup/undo.rb
lib/sup/update.rb
lib/sup/util.rb

diff --git a/bin/sup b/bin/sup
index 786fba2dadf6f0257b595a51c8a4e9c1d7c249e6..0fb28fa4e3aa7a8657c88c1ab886e5480b049241 100755 (executable)
--- a/bin/sup
+++ b/bin/sup
@@ -129,7 +129,7 @@ def stop_cursing
 end
 module_function :start_cursing, :stop_cursing
 
-Index.new
+Index.init
 begin
   Index.lock
 rescue Index::LockError => e
@@ -178,7 +178,7 @@ begin
   log "starting curses"
   start_cursing
 
-  bm = BufferManager.new
+  bm = BufferManager.init
   Colormap.new.populate_colormap
 
   log "initializing mail index buffer"
index 3ab7c4db0a18bf4bdb07bf2b9b7f9af375172e78..8f7010ee3f4dc06aa9051006ab08a4f45a42bec9 100755 (executable)
@@ -77,7 +77,7 @@ end
 
 $terminal.wrap_at = :auto
 Redwood::start
-index = Redwood::Index.new
+index = Redwood::Index.init
 
 index.lock_or_die
 
index 9fcbee60e527250700b6aaaf0e64a4746b22dda2..bd55fc1825fad84b6cf6cc8c0185d52effbc5adf 100755 (executable)
@@ -151,7 +151,7 @@ end
 
 $terminal.wrap_at = :auto
 Redwood::start
-index = Redwood::Index.new
+index = Redwood::Index.init
 Redwood::SourceManager.load_sources
 
 say <<EOS
index 992fd0bb74cd0aa7da29729ae38b460d0aad8bc8..8b5bf07f07be990e49ffdb96ff90b623e69a1a9a 100755 (executable)
@@ -21,8 +21,8 @@ No options.
 EOS
 end
 
-index = Redwood::Index.new
-Redwood::SourceManager.new
+index = Redwood::Index.init
+Redwood::SourceManager.init
 index.load
 
 index.each_message :load_spam => true, :load_deleted => true, :load_killed => true do |m|
index db75b11a392f8043346d7af4769d6a6244ec3b2b..43fa5f640f0fcd862ceac622388a471a8573edc2 100755 (executable)
@@ -50,7 +50,7 @@ end.parse(ARGV)
 require "sup"
 Redwood::start
 puts "loading index..."
-index = Redwood::Index.new
+index = Redwood::Index.init
 index.load
 puts "loaded index of #{index.size} messages"
 
index d445bd806f16b6254cbb38fce4a662191514b5a0..b743c1c13e3cdf9f73d6d24a5c88882985c30681 100755 (executable)
@@ -95,7 +95,7 @@ target = [:new, :changed, :all, :restored].find { |x| opts[x] } || :new
 op = [:asis, :restore, :discard].find { |x| opts[x] } || :asis
 
 Redwood::start
-index = Redwood::Index.new
+index = Redwood::Index.init
 
 restored_state = if opts[:restore]
   dump = {}
index 7635274a90c759fecd2e3a1eb86e9f0c2a035938..4d76f17f587fb75e4294b4a39c4bf52f0d5280c8 100755 (executable)
@@ -65,7 +65,7 @@ EOS
 end
 
 Redwood::start
-index = Redwood::Index.new
+index = Redwood::Index.init
 index.lock_or_die
 
 deleted_fp, spam_fp = nil
index c9ad8e82d4c945381678a8e2ab1ebb3524cb7356..138f7e1738c7c6029ceb40003716d71763be53ab 100755 (executable)
@@ -61,7 +61,7 @@ Trollop::die "nothing to do: no labels to add or remove" if add_labels.empty? &&
 
 Redwood::start
 begin
-  index = Redwood::Index.new
+  index = Redwood::Index.init
   index.load
 
   source_ids = if opts[:all_sources]
index 710dca1522e2f6651fd4fe2ad0014f72fdbc46a9..00ad87cad7aa6bce62c35cce2eb162fc6287db4b 100644 (file)
@@ -107,17 +107,17 @@ module Redwood
   end
 
   def start
-    Redwood::SentManager.new $config[:sent_source] || 'sup://sent'
-    Redwood::ContactManager.new Redwood::CONTACT_FN
-    Redwood::LabelManager.new Redwood::LABEL_FN
-    Redwood::AccountManager.new $config[:accounts]
-    Redwood::DraftManager.new Redwood::DRAFT_DIR
-    Redwood::UpdateManager.new
-    Redwood::PollManager.new
-    Redwood::SuicideManager.new Redwood::SUICIDE_FN
-    Redwood::CryptoManager.new
-    Redwood::UndoManager.new
-    Redwood::SourceManager.new
+    Redwood::SentManager.init $config[:sent_source] || 'sup://sent'
+    Redwood::ContactManager.init Redwood::CONTACT_FN
+    Redwood::LabelManager.init Redwood::LABEL_FN
+    Redwood::AccountManager.init $config[:accounts]
+    Redwood::DraftManager.init Redwood::DRAFT_DIR
+    Redwood::UpdateManager.init
+    Redwood::PollManager.init
+    Redwood::SuicideManager.init Redwood::SUICIDE_FN
+    Redwood::CryptoManager.init
+    Redwood::UndoManager.init
+    Redwood::SourceManager.init
   end
 
   def finish
@@ -230,7 +230,7 @@ require "sup/hook"
 ## we have to initialize this guy first, because other classes must
 ## reference it in order to register hooks, and they do that at parse
 ## time.
-Redwood::HookManager.new Redwood::HOOK_DIR
+Redwood::HookManager.init Redwood::HOOK_DIR
 
 ## everything we need to get logging working
 require "sup/buffer"
index 6f86129ca5d8997b0572f38512ba84a691ee4a7c..eed2794d5f8562647a52f18686f2f635963d5799 100644 (file)
@@ -25,8 +25,6 @@ class AccountManager
 
     add_account accounts[:default], true
     accounts.each { |k, v| add_account v, false unless k == :default }
-
-    self.class.i_am_the_instance self
   end
 
   def user_accounts; @accounts.keys; end
index 3bd3fe8e96a1563ff3ae4fa748b80203a1320cb8..53479199ce6e06670bbb56cf577937cd5e2b59e8 100644 (file)
@@ -196,8 +196,6 @@ EOS
     @flash = nil
     @shelled = @asking = false
     @in_x = ENV["TERM"] =~ /(xterm|rxvt|screen)/
-
-    self.class.i_am_the_instance self
   end
 
   def buffers; @name_map.to_a; end
index 51fd0e978c902f17485c8fcd81f20cc766b74157..c489aaff5a4d67ce9d7156a10013f329bf790f11 100644 (file)
@@ -22,8 +22,6 @@ class ContactManager
         @a2p[aalias] = p unless aalias.nil? || aalias.empty?
       end
     end
-
-    self.class.i_am_the_instance self
   end
 
   def contacts; @p2a.keys end
index f68009d91bd7b5e42ce6216e4a3bad12335d03f6..a3f7afcd2099214bc674340eaea445a394bd4b98 100644 (file)
@@ -13,10 +13,8 @@ class CryptoManager
 
   def initialize
     @mutex = Mutex.new
-    self.class.i_am_the_instance self
 
     bin = `which gpg`.chomp
-
     @cmd =
       case bin
       when /\S/
index ce8f064aca662df995dc2a21ef2295c4f76fdb73..5ea2935f2180f3cb8a94ac996a50ce97dcc6d1ac 100644 (file)
@@ -7,7 +7,6 @@ class DraftManager
   def initialize dir
     @dir = dir
     @source = nil
-    self.class.i_am_the_instance self
   end
 
   def self.source_name; "sup://drafts"; end
index 0a0a2f672fca775e4bb792896537d2ec2535fac3..a492a07858abbd444916e88b5f39f35867f5e5fd 100644 (file)
@@ -79,8 +79,6 @@ class HookManager
     @tags = {}
 
     Dir.mkdir dir unless File.exists? dir
-
-    self.class.i_am_the_instance self
   end
 
   attr_reader :tags
index 122026a4a1a7ca4e6a20663b71fe76317075c8a9..19d753e321dd5126d665db428fc18e13a7cc80f2 100644 (file)
@@ -26,7 +26,6 @@ class BaseIndex
   def initialize dir=BASE_DIR
     @dir = dir
     @lock = Lockfile.new lockfile, :retries => 0, :max_age => nil
-    self.class.i_am_the_instance self
   end
 
   def lockfile; File.join @dir, "lock" end
index 3e7bacbd795b2c1b9a3d49754d6f41878608f57a..67474c2b79ed3393aa3321e458fdb4675225dfe1 100644 (file)
@@ -22,8 +22,6 @@ class LabelManager
     @new_labels = {}
     @modified = false
     labels.each { |t| @labels[t] = true }
-
-    self.class.i_am_the_instance self
   end
 
   def new_label? l; @new_labels.include?(l) end
index 4374242cefe39af7682b1db80f34f9ac0a06dedf..a36a4c44721dd324d57c72792a15a08247db8f7a 100644 (file)
@@ -35,8 +35,6 @@ EOS
     @thread = nil
     @last_poll = nil
     @polling = false
-
-    self.class.i_am_the_instance self
   end
 
   def buffer
index 74fe1aebbb780a91f234323dd109b847022e51ab..d381395104a5575e1011c88560021cc2c0820764 100644 (file)
@@ -8,7 +8,6 @@ class SentManager
   def initialize source_uri
     @source = nil
     @source_uri = source_uri
-    self.class.i_am_the_instance self
     Redwood::log "SentManager intialized with source uri: #@source_uri"
   end
 
index fb9089fc37dab718eaead5d0aaab65c0a4b3d41d..7c9a2657316752019683780bb14ced56c26b61ba 100644 (file)
@@ -168,7 +168,6 @@ class SourceManager
     @sources = {}
     @sources_dirty = false
     @source_mutex = Monitor.new
-    self.class.i_am_the_instance self
   end
 
   def [](id)
index 98b43462eea250aa6969221d0a8456b9697bd54a..bef63258ed70b57fe2607492671d8a510b3f6bbc 100644 (file)
@@ -9,7 +9,6 @@ class SuicideManager
     @fn = fn
     @die = false
     @thread = nil
-    self.class.i_am_the_instance self
     FileUtils.rm_f @fn
   end
 
index 5a93c31bc1dac91142c9c4bb509cd7ee3649756d..9ccf84a01724af3d7f7a2077e9f774f85d9579a8 100644 (file)
@@ -12,7 +12,6 @@ class UndoManager
 
   def initialize
     @@actionlist = []
-    self.class.i_am_the_instance self
   end
 
   def register desc, *actions, &b
index 021b7e1b09eb93276bc03d659d36fb749f5c9aec..d3868012cd0b3607c17914729a170bf9ff4fbb47 100644 (file)
@@ -16,7 +16,6 @@ class UpdateManager
 
   def initialize
     @targets = {}
-    self.class.i_am_the_instance self
   end
 
   def register o; @targets[o] = true; end
index 5776d7e384d1749e6e6af8c5cd9ed7e10d985763..5d57361e990da24abe6c470ded4fa31a3800f51b 100644 (file)
@@ -495,19 +495,20 @@ class Time
   end
 end
 
-## simple singleton module. far less complete and insane than the ruby
-## standard library one, but automatically forwards methods calls and
-## allows for constructors that take arguments.
+## simple singleton module. far less complete and insane than the ruby standard
+## library one, but it automatically forwards methods calls and allows for
+## constructors that take arguments.
 ##
-## You must have #initialize call "self.class.i_am_the_instance self"
-## at some point or everything will fail horribly.
+## classes that inherit this can define initialize. however, you cannot call
+## .new on the class. To get the instance of the class, call .instance;
+## to create the instance, call init.
 module Singleton
   module ClassMethods
     def instance; @instance; end
     def instantiated?; defined?(@instance) && !@instance.nil?; end
     def deinstantiate!; @instance = nil; end
     def method_missing meth, *a, &b
-      raise "no instance defined!" unless defined? @instance
+      raise "no #{name} instance defined in method call to #{meth}!" unless defined? @instance
 
       ## if we've been deinstantiated, just drop all calls. this is
       ## useful because threads that might be active during the
@@ -517,13 +518,14 @@ module Singleton
 
       @instance.send meth, *a, &b
     end
-    def i_am_the_instance o
+    def init *args
       raise "there can be only one! (instance)" if defined? @instance
-      @instance = o
+      @instance = new(*args)
     end
   end
 
   def self.included klass
+    klass.private_class_method :allocate, :new
     klass.extend ClassMethods
   end
 end