]> git.notmuchmail.org Git - sup/commitdiff
Merge branch 'hook-local-vars' into next
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 24 Aug 2009 22:35:37 +0000 (18:35 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 24 Aug 2009 22:35:37 +0000 (18:35 -0400)
1  2 
lib/sup/hook.rb

diff --combined lib/sup/hook.rb
index 48a26d42207f09eaa58df802a3e585ccf14f4f33,d3d2ba8fc4b6f88619843512c99dec8c0c94c8cb..f2be1dd298392c1f3ec1278f0fe06aee04c12991
@@@ -5,6 -5,7 +5,7 @@@ class HookManage
      def initialize name
        @__say_id = nil
        @__name = name
+       @__cache = {}
      end
  
      def say s
@@@ -17,7 -18,7 +18,7 @@@
      end
  
      def log s
 -      Redwood::log "hook[#@__name]: #{s}"
 +      info "hook[#@__name]: #{s}"
      end
  
      def ask_yes_or_no q
  
      def __run __hook, __filename, __locals
        __binding = binding
-       eval __locals.map { |k, v| "#{k} = __locals[#{k.inspect}];" }.join, __binding
+       __lprocs, __lvars = __locals.partition { |k, v| v.is_a?(Proc) }
+       eval __lvars.map { |k, v| "#{k} = __locals[#{k.inspect}];" }.join, __binding
+       ## we also support closures for delays evaluation. unfortunately
+       ## we have to do this via method calls, so you don't get all the
+       ## semantics of a regular variable. not ideal.
+       __lprocs.each do |k, v|
+         self.class.instance_eval do
+           define_method k do
+             @__cache[k] ||= v.call
+           end
+         end
+       end
        ret = eval __hook, __binding, __filename
        BufferManager.clear @__say_id if @__say_id
        ret
@@@ -56,6 -68,8 +68,6 @@@
      @tags = {}
  
      Dir.mkdir dir unless File.exists? dir
 -
 -    self.class.i_am_the_instance self
    end
  
    attr_reader :tags
@@@ -98,8 -112,6 +110,8 @@@ EO
  
    def enabled? name; !hook_for(name).nil? end
  
 +  def clear; @hooks.clear; end
 +
  private
  
    def hook_for name
    end
  
    def log m
 -    Redwood::log("hook: " + m)
 +    info("hook: " + m)
    end
  end