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 def method_missing m, *a
25 @__locals[m] = @__locals[m].call(*a) # only call the proc once
34 if BufferManager.instantiated?
35 @__say_id = BufferManager.say s, @__say_id
36 BufferManager.draw_screen
43 Redwood::log "hook[#@__name]: #{s}"
47 if BufferManager.instantiated?
48 BufferManager.ask_yes_or_no q
51 gets.chomp.downcase == 'y'
60 BufferManager.clear @__say_id if @__say_id
72 Dir.mkdir dir unless File.exists? dir
74 self.class.i_am_the_instance self
77 def run name, locals={}
78 hook = hook_for(name) or return
79 context = @contexts[hook] ||= HookContext.new(name)
80 context.__locals = locals
84 result = context.instance_eval @hooks[name], fn_for(name)
86 log "error running hook: #{e.message}"
87 log e.backtrace.join("\n")
88 @hooks[name] = nil # disable it
89 BufferManager.flash "Error running hook: #{e.message}" if BufferManager.instantiated?
95 def register name, desc
99 def print_hooks f=$stdout
101 Have #{@descs.size} registered hooks:
105 @descs.sort.each do |name, desc|
115 def enabled? name; !hook_for(name).nil? end
120 unless @hooks.member? name
123 returning IO.read(fn_for(name)) do
124 log "read '#{name}' from #{fn_for(name)}"
126 rescue SystemCallError => e
127 #log "disabled hook for '#{name}': #{e.message}"
136 File.join @dir, "#{name}.rb"
140 Redwood::log("hook: " + m)