]> git.notmuchmail.org Git - sup/commitdiff
Merge branch 'hook-local-vars' into next
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Sun, 23 Aug 2009 17:24:59 +0000 (13:24 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Sun, 23 Aug 2009 17:24:59 +0000 (13:24 -0400)
Conflicts:
lib/sup/hook.rb

1  2 
lib/sup/hook.rb

diff --combined lib/sup/hook.rb
index 9c48bcabea67caa6dc011c030213f0cc6b94026e,0fa4169ba30ac90f56ec8bcd0090e8c62b84e40e..48a26d42207f09eaa58df802a3e585ccf14f4f33
@@@ -1,38 -1,10 +1,10 @@@
  module Redwood
  
  class HookManager
-   ## there's probably a better way to do this, but to evaluate a hook
-   ## with a bunch of pre-set "local variables" i define a function
-   ## per variable and then instance_evaluate the code.
-   ##
-   ## how does rails do it, when you pass :locals into a partial?
-   ##
-   ## i don't bother providing setters, since i'm pretty sure the
-   ## charade will fall apart pretty quickly with respect to scoping.
-   ## "fail-fast", we'll call it.
    class HookContext
      def initialize name
        @__say_id = nil
        @__name = name
-       @__locals = {}
-     end
-     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
-         @__locals[m] = @__locals[m].call(*a) # only call the proc once
-       when nil
-         super
-       else
-         @__locals[m]
-       end
      end
  
      def say s
@@@ -45,7 -17,7 +17,7 @@@
      end
  
      def log s
 -      Redwood::log "hook[#@__name]: #{s}"
 +      info "hook[#@__name]: #{s}"
      end
  
      def ask_yes_or_no q
        HookManager.tags[tag] = value
      end
  
-     def __binding 
-       binding
-     end
-     def __cleanup
+     def __run __hook, __filename, __locals
+       __binding = binding
+       eval __locals.map { |k, v| "#{k} = __locals[#{k.inspect}];" }.join, __binding
+       ret = eval __hook, __binding, __filename
        BufferManager.clear @__say_id if @__say_id
+       ret
      end
    end
  
@@@ -84,6 -56,8 +56,6 @@@
      @tags = {}
  
      Dir.mkdir dir unless File.exists? dir
 -
 -    self.class.i_am_the_instance self
    end
  
    attr_reader :tags
    def run name, locals={}
      hook = hook_for(name) or return
      context = @contexts[hook] ||= HookContext.new(name)
-     context.__locals = locals
  
      result = nil
      begin
-       result = context.instance_eval @hooks[name], fn_for(name)
+       result = context.__run hook, fn_for(name), locals
      rescue Exception => e
        log "error running hook: #{e.message}"
        log e.backtrace.join("\n")
        @hooks[name] = nil # disable it
        BufferManager.flash "Error running hook: #{e.message}" if BufferManager.instantiated?
      end
-     context.__cleanup
      result
    end
  
@@@ -128,21 -100,18 +98,20 @@@ EO
  
    def enabled? name; !hook_for(name).nil? end
  
 +  def clear; @hooks.clear; end
 +
  private
  
    def hook_for name
      unless @hooks.member? name
-       @hooks[name] =
-         begin
-           returning IO.read(fn_for(name)) do
-             log "read '#{name}' from #{fn_for(name)}"
-           end
-         rescue SystemCallError => e
-           #log "disabled hook for '#{name}': #{e.message}"
-           nil
+       @hooks[name] = begin
+         returning IO.read(fn_for(name)) do
+           log "read '#{name}' from #{fn_for(name)}"
          end
+       rescue SystemCallError => e
+         #log "disabled hook for '#{name}': #{e.message}"
+         nil
+       end
      end
  
      @hooks[name]
    end
  
    def log m
 -    Redwood::log("hook: " + m)
 +    info("hook: " + m)
    end
  end