4 ## there's probably a better way to do this, but to evaluate a hook
5 ## with a bunch of pre-set "local variables" i define a function
6 ## per variable and then instance_evaluate the code.
8 ## how does rails do it, when you pass :locals into a partial?
10 ## i don't bother providing setters, since i'm pretty sure the
11 ## charade will fall apart pretty quickly with respect to scoping.
12 ## "fail-fast", we'll call it.
22 ## an annoying gotcha here is that if you try something
23 ## like var = var.foo(), var will magically get allocated
24 ## to Nil and method_missing will never get called. You
25 ## can work around this by calling self.var or simply
26 ## not assigning it to itself.
27 def method_missing m, *a
30 @__locals[m] = @__locals[m].call(*a) # only call the proc once
39 if BufferManager.instantiated?
40 @__say_id = BufferManager.say s, @__say_id
41 BufferManager.draw_screen
48 info "hook[#@__name]: #{s}"
52 if BufferManager.instantiated?
53 BufferManager.ask_yes_or_no q
56 gets.chomp.downcase == 'y'
65 HookManager.tags[tag] = value
73 BufferManager.clear @__say_id if @__say_id
86 Dir.mkdir dir unless File.exists? dir
91 def run name, locals={}
92 hook = hook_for(name) or return
93 context = @contexts[hook] ||= HookContext.new(name)
94 context.__locals = locals
98 result = context.instance_eval @hooks[name], fn_for(name)
100 log "error running hook: #{e.message}"
101 log e.backtrace.join("\n")
102 @hooks[name] = nil # disable it
103 BufferManager.flash "Error running hook: #{e.message}" if BufferManager.instantiated?
109 def register name, desc
113 def print_hooks f=$stdout
115 Have #{@descs.size} registered hooks:
119 @descs.sort.each do |name, desc|
129 def enabled? name; !hook_for(name).nil? end
131 def clear; @hooks.clear; end
136 unless @hooks.member? name
139 returning IO.read(fn_for(name)) do
140 log "read '#{name}' from #{fn_for(name)}"
142 rescue SystemCallError => e
143 #log "disabled hook for '#{name}': #{e.message}"
152 File.join @dir, "#{name}.rb"