X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=lib%2Fsup%2Fhook.rb;h=d3d2ba8fc4b6f88619843512c99dec8c0c94c8cb;hb=dcd7a7e4c33778d38dd8b1c4059d4b6c99add3fe;hp=0fa4169ba30ac90f56ec8bcd0090e8c62b84e40e;hpb=e459fc846862a1499f3acb2f8a2eb098344ee7ac;p=sup diff --git a/lib/sup/hook.rb b/lib/sup/hook.rb index 0fa4169..d3d2ba8 100644 --- a/lib/sup/hook.rb +++ b/lib/sup/hook.rb @@ -5,6 +5,7 @@ class HookManager def initialize name @__say_id = nil @__name = name + @__cache = {} end def say s @@ -39,7 +40,18 @@ class HookManager 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