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 Redwood::log "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
88 self.class.i_am_the_instance self
93 def run name, locals={}
94 hook = hook_for(name) or return
95 context = @contexts[hook] ||= HookContext.new(name)
96 context.__locals = locals
100 result = context.instance_eval @hooks[name], fn_for(name)
101 rescue Exception => e
102 log "error running hook: #{e.message}"
103 log e.backtrace.join("\n")
104 @hooks[name] = nil # disable it
105 BufferManager.flash "Error running hook: #{e.message}" if BufferManager.instantiated?
111 def register name, desc
115 def print_hooks f=$stdout
117 Have #{@descs.size} registered hooks:
121 @descs.sort.each do |name, desc|
131 def enabled? name; !hook_for(name).nil? 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"
156 Redwood::log("hook: " + m)