]> git.cworth.org Git - sup/commitdiff
add custom-search hook, for shortcuts for custom search queries
authorEdward Z. Yang <ezyang@MIT.EDU>
Fri, 26 Jun 2009 17:10:01 +0000 (13:10 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 27 Jul 2009 17:48:52 +0000 (13:48 -0400)
Signed-off-by: Edward Z. Yang <ezyang@mit.edu>
lib/sup/hook.rb
lib/sup/index.rb

index 0a0a2f672fca775e4bb792896537d2ec2535fac3..33a97b26063bffceb9f3d600962918b46106aa34 100644 (file)
@@ -19,6 +19,11 @@ class HookManager
 
     attr_writer :__locals
 
+    ## an annoying gotcha here is that if you try something
+    ## like var = var.foo(), var will magically get allocated
+    ## to Nil and method_missing will never get called.  You
+    ## can work around this by calling self.var or simply
+    ## not assigning it to itself.
     def method_missing m, *a
       case @__locals[m]
       when Proc
index ca01ee76cc89c118933c5122b616b4b6f45a59bb..9c985d98588739d2ecc5b1856cdfc5389df39f9c 100644 (file)
@@ -24,6 +24,16 @@ class Index
 
   include Singleton
 
+  HookManager.register "custom-search", <<EOS
+Executes before a string search is applied to the index,
+returning a new search string.
+Variables:
+  subs: The string being searched. Be careful about shadowing:
+    this variable is actually a method, so use a temporary variable
+    or explicitly call self.subs; the substitutions in index.rb
+    don't actually work.
+EOS
+
   ## these two accessors should ONLY be used by single-threaded programs.
   ## otherwise you will have a naughty ferret on your hands.
   attr_reader :index
@@ -507,7 +517,9 @@ protected
   def parse_user_query_string s
     extraopts = {}
 
-    subs = s.gsub(/\b(to|from):(\S+)\b/) do
+    subs = HookManager.run("custom-search", :subs => s) || s
+
+    subs = subs.gsub(/\b(to|from):(\S+)\b/) do
       field, name = $1, $2
       if(p = ContactManager.contact_for(name))
         [field, p.email]